optimize #148 lazy initial gorm db in first use

pull/149/head
alimy 2 years ago
parent 65f8bd626e
commit d5b701864b

@ -7,7 +7,7 @@ import (
) )
var ( var (
LoggerSetting *LoggerSettingS loggerSetting *LoggerSettingS
loggerFileSetting *LoggerFileSettingS loggerFileSetting *LoggerFileSettingS
loggerZincSetting *LoggerZincSettingS loggerZincSetting *LoggerZincSettingS
loggerMeiliSetting *LoggerMeiliSettingS loggerMeiliSetting *LoggerMeiliSettingS
@ -57,7 +57,7 @@ func setupSetting(suite []string, noDefault bool) error {
"BigCacheIndex": &BigCacheIndexSetting, "BigCacheIndex": &BigCacheIndexSetting,
"Alipay": &AlipaySetting, "Alipay": &AlipaySetting,
"SmsJuhe": &SmsJuheSetting, "SmsJuhe": &SmsJuheSetting,
"Logger": &LoggerSetting, "Logger": &loggerSetting,
"LoggerFile": &loggerFileSetting, "LoggerFile": &loggerFileSetting,
"LoggerZinc": &loggerZincSetting, "LoggerZinc": &loggerZincSetting,
"LoggerMeili": &loggerMeiliSetting, "LoggerMeili": &loggerMeiliSetting,
@ -97,11 +97,7 @@ func Initialize(suite []string, noDefault bool) {
} }
setupLogger() setupLogger()
setupDBEngine()
err = setupDBEngine()
if err != nil {
log.Fatalf("init.setupDBEngine err: %v", err)
}
} }
// Cfg get value by key if exist // Cfg get value by key if exist

@ -1,6 +1,7 @@
package conf package conf
import ( import (
"sync"
"time" "time"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
@ -15,10 +16,21 @@ import (
) )
var ( var (
DBEngine *gorm.DB db *gorm.DB
Redis *redis.Client Redis *redis.Client
once sync.Once
) )
func MustGormDB() *gorm.DB {
once.Do(func() {
var err error
if db, err = newDBEngine(); err != nil {
logrus.Fatalf("new gorm db failed: %s", err)
}
})
return db
}
func newDBEngine() (*gorm.DB, error) { func newDBEngine() (*gorm.DB, error) {
newLogger := logger.New( newLogger := logger.New(
logrus.StandardLogger(), // io writer日志输出的目标前缀和日志包含的内容 logrus.StandardLogger(), // io writer日志输出的目标前缀和日志包含的内容
@ -69,19 +81,10 @@ func newDBEngine() (*gorm.DB, error) {
return db, err return db, err
} }
// setupDBEngine 暂时只支持MySQL func setupDBEngine() {
func setupDBEngine() error {
var err error
DBEngine, err = newDBEngine()
if err != nil {
return err
}
Redis = redis.NewClient(&redis.Options{ Redis = redis.NewClient(&redis.Options{
Addr: redisSetting.Host, Addr: redisSetting.Host,
Password: redisSetting.Password, Password: redisSetting.Password,
DB: redisSetting.DB, DB: redisSetting.DB,
}) })
return nil
} }

@ -18,7 +18,7 @@ func newFileLogger() io.Writer {
func setupLogger() { func setupLogger() {
logrus.SetFormatter(&logrus.JSONFormatter{}) logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetLevel(LoggerSetting.logLevel()) logrus.SetLevel(loggerSetting.logLevel())
if CfgIf("LoggerFile") { if CfgIf("LoggerFile") {
out := newFileLogger() out := newFileLogger()

@ -39,7 +39,7 @@ func NewDataService() (core.DataService, core.VersionInfo) {
c core.CacheIndexService c core.CacheIndexService
v core.VersionInfo v core.VersionInfo
) )
db := conf.DBEngine db := conf.MustGormDB()
i := newIndexPostsService(db) i := newIndexPostsService(db)
if conf.CfgIf("SimpleCacheIndex") { if conf.CfgIf("SimpleCacheIndex") {
@ -71,7 +71,7 @@ func NewDataService() (core.DataService, core.VersionInfo) {
func NewAuthorizationManageService() core.AuthorizationManageService { func NewAuthorizationManageService() core.AuthorizationManageService {
return &authorizationManageServant{ return &authorizationManageServant{
db: conf.DBEngine, db: conf.MustGormDB(),
} }
} }

@ -12,6 +12,8 @@ import (
) )
func JWT() gin.HandlerFunc { func JWT() gin.HandlerFunc {
// TODO: optimize get user from a simple service that provide fetch a user info interface.
db := conf.MustGormDB()
return func(c *gin.Context) { return func(c *gin.Context) {
var ( var (
token string token string
@ -54,7 +56,7 @@ func JWT() gin.HandlerFunc {
ID: claims.UID, ID: claims.UID,
}, },
} }
user, _ = user.Get(conf.DBEngine) user, _ = user.Get(db)
c.Set("USER", user) c.Set("USER", user)
// 强制下线机制 // 强制下线机制
@ -76,6 +78,8 @@ func JWT() gin.HandlerFunc {
} }
func JwtLoose() gin.HandlerFunc { func JwtLoose() gin.HandlerFunc {
// TODO: optimize get user from a simple service that provide fetch a user info interface.
db := conf.MustGormDB()
return func(c *gin.Context) { return func(c *gin.Context) {
token, exist := c.GetQuery("token") token, exist := c.GetQuery("token")
if !exist { if !exist {
@ -98,7 +102,7 @@ func JwtLoose() gin.HandlerFunc {
ID: claims.UID, ID: claims.UID,
}, },
} }
user, err := user.Get(conf.DBEngine) user, err := user.Get(db)
if err == nil && (conf.JWTSetting.Issuer+":"+user.Salt) == claims.Issuer { if err == nil && (conf.JWTSetting.Issuer+":"+user.Salt) == claims.Issuer {
c.Set("USER", user) c.Set("USER", user)
} }

@ -16,8 +16,6 @@ import (
) )
var ( var (
version, commitID, buildDate string
noDefaultFeatures bool noDefaultFeatures bool
features suites features suites
) )

Loading…
Cancel
Save