feature ✨ 优化setup
This commit is contained in:
1 parent
0e3e733745
commit
3aa64d107b
3 files changed
+17
-64
No files matched your search
+9
-47
@@ -14,8 +14,6 @@ import (
|
|||||||
"github.com/go-admin-team/go-admin-core/sdk"
|
"github.com/go-admin-team/go-admin-core/sdk"
|
||||||
"github.com/go-admin-team/go-admin-core/sdk/config"
|
"github.com/go-admin-team/go-admin-core/sdk/config"
|
||||||
"github.com/go-admin-team/go-admin-core/sdk/pkg"
|
"github.com/go-admin-team/go-admin-core/sdk/pkg"
|
||||||
"github.com/go-admin-team/go-admin-core/sdk/pkg/captcha"
|
|
||||||
"github.com/go-admin-team/go-admin-core/sdk/pkg/logger"
|
|
||||||
"github.com/go-admin-team/go-admin-core/sdk/runtime"
|
"github.com/go-admin-team/go-admin-core/sdk/runtime"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
@@ -23,6 +21,7 @@ import (
|
|||||||
"go-admin/app/admin/router"
|
"go-admin/app/admin/router"
|
||||||
"go-admin/common/database"
|
"go-admin/common/database"
|
||||||
"go-admin/common/global"
|
"go-admin/common/global"
|
||||||
|
"go-admin/common/storage"
|
||||||
ext "go-admin/config"
|
ext "go-admin/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -57,57 +56,20 @@ func setup() {
|
|||||||
// 注入配置扩展项
|
// 注入配置扩展项
|
||||||
config.ExtendConfig = &ext.ExtConfig
|
config.ExtendConfig = &ext.ExtConfig
|
||||||
//1. 读取配置
|
//1. 读取配置
|
||||||
config.Setup(file.NewSource(file.WithPath(configYml)))
|
config.Setup(
|
||||||
//2. 设置日志
|
file.NewSource(file.WithPath(configYml)),
|
||||||
sdk.Runtime.SetLogger(
|
database.Setup,
|
||||||
logger.SetupLogger(
|
storage.Setup,
|
||||||
logger.WithType(config.LoggerConfig.Type),
|
)
|
||||||
logger.WithPath(config.LoggerConfig.Path),
|
|
||||||
logger.WithLevel(config.LoggerConfig.Level),
|
|
||||||
logger.WithStdout(config.LoggerConfig.Stdout),
|
|
||||||
logger.WithCap(config.LoggerConfig.Cap),
|
|
||||||
))
|
|
||||||
//3. 初始化数据库链接
|
|
||||||
database.Setup()
|
|
||||||
|
|
||||||
//4. 设置缓存
|
|
||||||
cacheAdapter, err := config.CacheConfig.Setup()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("cache setup error, %s\n", err.Error())
|
|
||||||
}
|
|
||||||
sdk.Runtime.SetCacheAdapter(cacheAdapter)
|
|
||||||
//5. 设置验证码store
|
|
||||||
captcha.SetStore(captcha.NewCacheStore(cacheAdapter, 600))
|
|
||||||
|
|
||||||
//6. 设置队列
|
|
||||||
if !config.QueueConfig.Empty() {
|
|
||||||
queueAdapter, err := config.QueueConfig.Setup()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("queue setup error, %s\n", err.Error())
|
|
||||||
}
|
|
||||||
sdk.Runtime.SetQueueAdapter(queueAdapter)
|
|
||||||
defer func() {
|
|
||||||
go queueAdapter.Run()
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
//7. 设置分布式锁
|
|
||||||
if !config.LockerConfig.Empty() {
|
|
||||||
lockerAdapter, err := config.LockerConfig.Setup()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("locker setup error, %s\n", err.Error())
|
|
||||||
}
|
|
||||||
sdk.Runtime.SetLockerAdapter(lockerAdapter)
|
|
||||||
}
|
|
||||||
|
|
||||||
usageStr := `starting api server...`
|
|
||||||
log.Println(usageStr)
|
|
||||||
//注册监听函数
|
//注册监听函数
|
||||||
queue := sdk.Runtime.GetMemoryQueue("")
|
queue := sdk.Runtime.GetMemoryQueue("")
|
||||||
queue.Register(global.LoginLog, models.SaveLoginLog)
|
queue.Register(global.LoginLog, models.SaveLoginLog)
|
||||||
queue.Register(global.OperateLog, models.SaveOperaLog)
|
queue.Register(global.OperateLog, models.SaveOperaLog)
|
||||||
queue.Register(global.ApiCheck, models.SaveSysApi)
|
queue.Register(global.ApiCheck, models.SaveSysApi)
|
||||||
go queue.Run()
|
go queue.Run()
|
||||||
|
|
||||||
|
usageStr := `starting api server...`
|
||||||
|
log.Println(usageStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func run() error {
|
func run() error {
|
||||||
|
|||||||
+5
-14
@@ -13,7 +13,6 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/go-admin-team/go-admin-core/sdk/config"
|
"github.com/go-admin-team/go-admin-core/sdk/config"
|
||||||
"github.com/go-admin-team/go-admin-core/sdk/pkg/logger"
|
|
||||||
"go-admin/cmd/migrate/migration"
|
"go-admin/cmd/migrate/migration"
|
||||||
_ "go-admin/cmd/migrate/migration/version"
|
_ "go-admin/cmd/migrate/migration/version"
|
||||||
_ "go-admin/cmd/migrate/migration/version-local"
|
_ "go-admin/cmd/migrate/migration/version-local"
|
||||||
@@ -49,17 +48,10 @@ func run() {
|
|||||||
if !generate {
|
if !generate {
|
||||||
fmt.Println(`start init`)
|
fmt.Println(`start init`)
|
||||||
//1. 读取配置
|
//1. 读取配置
|
||||||
config.Setup(file.NewSource(file.WithPath(configYml)))
|
config.Setup(
|
||||||
//2. 设置日志
|
file.NewSource(file.WithPath(configYml)),
|
||||||
sdk.Runtime.SetLogger(
|
initDB,
|
||||||
logger.SetupLogger(
|
)
|
||||||
logger.WithType(config.LoggerConfig.Type),
|
|
||||||
logger.WithPath(config.LoggerConfig.Path),
|
|
||||||
logger.WithLevel(config.LoggerConfig.Level),
|
|
||||||
logger.WithStdout(config.LoggerConfig.Stdout),
|
|
||||||
logger.WithCap(config.LoggerConfig.Cap),
|
|
||||||
))
|
|
||||||
_ = initDB()
|
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(`generate migration file`)
|
fmt.Println(`generate migration file`)
|
||||||
_ = genFile()
|
_ = genFile()
|
||||||
@@ -83,14 +75,13 @@ func migrateModel() error {
|
|||||||
migration.Migrate.Migrate()
|
migration.Migrate.Migrate()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
func initDB() error {
|
func initDB() {
|
||||||
//3. 初始化数据库链接
|
//3. 初始化数据库链接
|
||||||
database.Setup()
|
database.Setup()
|
||||||
//4. 数据库迁移
|
//4. 数据库迁移
|
||||||
fmt.Println("数据库迁移开始")
|
fmt.Println("数据库迁移开始")
|
||||||
_ = migrateModel()
|
_ = migrateModel()
|
||||||
fmt.Println(`数据库基础数据初始化成功`)
|
fmt.Println(`数据库基础数据初始化成功`)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func genFile() error {
|
func genFile() error {
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ require (
|
|||||||
github.com/bytedance/go-tagexpr/v2 v2.7.10
|
github.com/bytedance/go-tagexpr/v2 v2.7.10
|
||||||
github.com/casbin/casbin/v2 v2.25.1
|
github.com/casbin/casbin/v2 v2.25.1
|
||||||
github.com/gin-gonic/gin v1.7.1
|
github.com/gin-gonic/gin v1.7.1
|
||||||
github.com/go-admin-team/go-admin-core v1.3.5-rc.5
|
github.com/go-admin-team/go-admin-core v1.3.5-rc.5.0.20210610090758-13967b971b9f
|
||||||
github.com/go-admin-team/go-admin-core/sdk v1.3.5-rc.5
|
github.com/go-admin-team/go-admin-core/sdk v1.3.5-rc.5.0.20210610090758-13967b971b9f
|
||||||
github.com/google/uuid v1.2.0
|
github.com/google/uuid v1.2.0
|
||||||
github.com/mssola/user_agent v0.5.2
|
github.com/mssola/user_agent v0.5.2
|
||||||
github.com/opentracing/opentracing-go v1.1.0
|
github.com/opentracing/opentracing-go v1.1.0
|
||||||
@@ -29,4 +29,4 @@ require (
|
|||||||
gorm.io/driver/postgres v1.0.6-0.20201208020313-1ed927cfab53
|
gorm.io/driver/postgres v1.0.6-0.20201208020313-1ed927cfab53
|
||||||
gorm.io/driver/sqlite v1.1.5-0.20201206014648-c84401fbe3ba
|
gorm.io/driver/sqlite v1.1.5-0.20201206014648-c84401fbe3ba
|
||||||
gorm.io/gorm v1.21.10
|
gorm.io/gorm v1.21.10
|
||||||
)
|
)
|
||||||
Reference in new issue
Block a user