| package cmd |
|
|
| import ( |
| goflag "flag" |
| "log" |
| "os" |
| "time" |
|
|
| "github.com/gin-gonic/gin" |
| "github.com/peterbourgon/ff/v3" |
|
|
| "github.com/metatube-community/metatube-sdk-go/database" |
| "github.com/metatube-community/metatube-sdk-go/engine" |
| "github.com/metatube-community/metatube-sdk-go/internal/envconfig" |
| "github.com/metatube-community/metatube-sdk-go/route" |
| "github.com/metatube-community/metatube-sdk-go/route/auth" |
| ) |
|
|
| var Config = &struct { |
| |
| Bind string |
| Port string |
| Token string |
| DSN string |
|
|
| |
| RequestTimeout time.Duration |
|
|
| |
| DBMaxIdleConns int |
| DBMaxOpenConns int |
| DBAutoMigrate bool |
| DBPreparedStmt bool |
|
|
| |
| VersionFlag bool |
| }{} |
|
|
| func init() { |
| |
| gin.DisableConsoleColor() |
|
|
| |
| flag := goflag.NewFlagSet("", goflag.ExitOnError) |
|
|
| |
| flag.StringVar(&Config.Bind, "bind", "", "Bind address of server") |
| flag.StringVar(&Config.Port, "port", "8080", "Port number of server") |
| flag.StringVar(&Config.Token, "token", "", "Token to access server") |
| flag.StringVar(&Config.DSN, "dsn", "", "Database Service Name") |
| flag.DurationVar(&Config.RequestTimeout, "request-timeout", engine.DefaultRequestTimeout, "Timeout per request") |
| flag.IntVar(&Config.DBMaxIdleConns, "db-max-idle-conns", 0, "Database max idle connections") |
| flag.IntVar(&Config.DBMaxOpenConns, "db-max-open-conns", 0, "Database max open connections") |
| flag.BoolVar(&Config.DBAutoMigrate, "db-auto-migrate", false, "Database auto migration") |
| flag.BoolVar(&Config.DBPreparedStmt, "db-prepared-stmt", false, "Database prepared statement") |
| flag.BoolVar(&Config.VersionFlag, "version", false, "Show version") |
| ff.Parse(flag, os.Args[1:], ff.WithEnvVars()) |
| } |
|
|
| func Router(names ...string) *gin.Engine { |
| db, err := database.Open(&database.Config{ |
| DSN: Config.DSN, |
| PreparedStmt: Config.DBPreparedStmt, |
| MaxIdleConns: Config.DBMaxIdleConns, |
| MaxOpenConns: Config.DBMaxOpenConns, |
| DisableAutomaticPing: true, |
| }) |
| if err != nil { |
| log.Fatal(err) |
| } |
|
|
| |
| var opts []engine.Option |
|
|
| |
| if Config.RequestTimeout >= time.Second { |
| opts = append(opts, engine.WithRequestTimeout(Config.RequestTimeout)) |
| } |
|
|
| |
| for _, name := range names { |
| opts = append(opts, engine.WithEngineName(name)) |
| } |
|
|
| |
| for provider, config := range envconfig.ActorProviderConfigs.Iterator() { |
| opts = append(opts, engine.WithActorProviderConfig(provider, config)) |
| } |
|
|
| |
| for provider, config := range envconfig.MovieProviderConfigs.Iterator() { |
| opts = append(opts, engine.WithMovieProviderConfig(provider, config)) |
| } |
|
|
| app := engine.New(db, opts...) |
|
|
| |
| if app.DBDriver() == database.Sqlite { |
| Config.DBAutoMigrate = true |
| } |
| if err = app.DBAutoMigrate(Config.DBAutoMigrate); err != nil { |
| log.Fatal(err) |
| } |
|
|
| var token auth.Validator |
| if Config.Token != "" { |
| token = auth.Token(Config.Token) |
| } |
|
|
| return route.New(app, token) |
| } |
|
|