refactor main
This commit is contained in:
1 parent
f68ef45e39
commit
662e01d9f8
3 files changed
+36
-27
No files matched your search
+4
-1
@@ -17,6 +17,9 @@ ENV GIN_MODE=release
|
|||||||
ENV PORT=8080
|
ENV PORT=8080
|
||||||
ENV TOKEN=""
|
ENV TOKEN=""
|
||||||
ENV DSN=""
|
ENV DSN=""
|
||||||
ENV AUTO_MIGRATE=1
|
ENV DB_MAX_IDLE_CONNS=0
|
||||||
|
ENV DB_MAX_OPEN_CONNS=0
|
||||||
|
ENV DB_PREPARED_STMT=0
|
||||||
|
ENV DB_AUTO_MIGRATE=1
|
||||||
|
|
||||||
ENTRYPOINT ["/javtube-server"]
|
ENTRYPOINT ["/javtube-server"]
|
||||||
+28
-10
@@ -12,23 +12,34 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/peterbourgon/ff/v3"
|
"github.com/peterbourgon/ff/v3"
|
||||||
|
|
||||||
|
"github.com/javtube/javtube-sdk-go/database"
|
||||||
"github.com/javtube/javtube-sdk-go/engine"
|
"github.com/javtube/javtube-sdk-go/engine"
|
||||||
V "github.com/javtube/javtube-sdk-go/internal/constant"
|
V "github.com/javtube/javtube-sdk-go/internal/constant"
|
||||||
"github.com/javtube/javtube-sdk-go/route"
|
"github.com/javtube/javtube-sdk-go/route"
|
||||||
"github.com/javtube/javtube-sdk-go/route/validator"
|
"github.com/javtube/javtube-sdk-go/route/validator"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const defaultRequestTimeout = 2 * time.Minute
|
||||||
|
|
||||||
var (
|
var (
|
||||||
opts = new(options)
|
opts = new(options)
|
||||||
flag = goflag.NewFlagSet("", goflag.ExitOnError)
|
flag = goflag.NewFlagSet("", goflag.ExitOnError)
|
||||||
)
|
)
|
||||||
|
|
||||||
type options struct {
|
type options struct {
|
||||||
bind string
|
// base options
|
||||||
port string
|
bind string
|
||||||
token string
|
port string
|
||||||
dsn string
|
token string
|
||||||
autoMigrate bool
|
dsn string
|
||||||
|
|
||||||
|
// database options
|
||||||
|
dbMaxIdleConns int
|
||||||
|
dbMaxOpenConns int
|
||||||
|
dbAutoMigrate bool
|
||||||
|
dbPreparedStmt bool
|
||||||
|
|
||||||
|
// version flag
|
||||||
versionFlag bool
|
versionFlag bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,7 +52,10 @@ func init() {
|
|||||||
flag.StringVar(&opts.port, "port", "8080", "Port number of server")
|
flag.StringVar(&opts.port, "port", "8080", "Port number of server")
|
||||||
flag.StringVar(&opts.token, "token", "", "Token to access server")
|
flag.StringVar(&opts.token, "token", "", "Token to access server")
|
||||||
flag.StringVar(&opts.dsn, "dsn", "", "Database Service Name")
|
flag.StringVar(&opts.dsn, "dsn", "", "Database Service Name")
|
||||||
flag.BoolVar(&opts.autoMigrate, "auto-migrate", false, "Database auto migration")
|
flag.IntVar(&opts.dbMaxIdleConns, "db-max-idle-conns", 0, "Database max idle connections")
|
||||||
|
flag.IntVar(&opts.dbMaxOpenConns, "db-max-open-conns", 0, "Database max open connections")
|
||||||
|
flag.BoolVar(&opts.dbAutoMigrate, "db-auto-migrate", false, "Database auto migration")
|
||||||
|
flag.BoolVar(&opts.dbPreparedStmt, "db-prepared-stmt", false, "Database prepared statement")
|
||||||
flag.BoolVar(&opts.versionFlag, "v", false, "Show version")
|
flag.BoolVar(&opts.versionFlag, "v", false, "Show version")
|
||||||
ff.Parse(flag, os.Args[1:], ff.WithEnvVarNoPrefix())
|
ff.Parse(flag, os.Args[1:], ff.WithEnvVarNoPrefix())
|
||||||
}
|
}
|
||||||
@@ -57,15 +71,19 @@ func main() {
|
|||||||
showVersionAndExit()
|
showVersionAndExit()
|
||||||
}
|
}
|
||||||
|
|
||||||
app, err := engine.New(&engine.Options{
|
db, err := database.Open(&database.Config{
|
||||||
DSN: opts.dsn,
|
DSN: opts.dsn,
|
||||||
Timeout: 2 * time.Minute,
|
PreparedStmt: opts.dbPreparedStmt,
|
||||||
|
MaxIdleConns: opts.dbMaxIdleConns,
|
||||||
|
MaxOpenConns: opts.dbMaxOpenConns,
|
||||||
|
DisableAutomaticPing: true,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = app.AutoMigrate(opts.autoMigrate); err != nil {
|
app := engine.New(db, defaultRequestTimeout)
|
||||||
|
if err = app.AutoMigrate(opts.dbAutoMigrate); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-16
@@ -35,24 +35,12 @@ type Engine struct {
|
|||||||
actorProviders map[string]javtube.ActorProvider
|
actorProviders map[string]javtube.ActorProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(opts *Options) (engine *Engine, err error) {
|
func New(db *gorm.DB, timeout time.Duration) *Engine {
|
||||||
var db *gorm.DB
|
return &Engine{
|
||||||
if db, err = openDB(opts.DSN); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !opts.DisableAutomaticPing {
|
|
||||||
if pinger, ok := db.ConnPool.(interface{ Ping() error }); ok {
|
|
||||||
go pinger.Ping() // Async ping.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
engine = &Engine{
|
|
||||||
db: db,
|
db: db,
|
||||||
actorProviders: initActorProviders(opts.Timeout),
|
actorProviders: initActorProviders(timeout),
|
||||||
movieProviders: initMovieProviders(opts.Timeout),
|
movieProviders: initMovieProviders(timeout),
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// initActorProviders initializes actor providers.
|
// initActorProviders initializes actor providers.
|
||||||
|
|||||||
Reference in new issue
Block a user