mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-02-23 23:32:15 +08:00
添加 数据库/表结构同步配置
添加 数据库/表结构同步
This commit is contained in:
parent
cd33f1e307
commit
64ac1d1f45
17
app.go
17
app.go
@ -38,8 +38,14 @@ func reloadDataSources(c *config.BasicConfig) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Log.Fatalf("[x] [数据源] 致命错误: %s", err)
|
logger.Log.Fatalf("[x] [数据源] 致命错误: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Log.Infoln("[√] [初始化数据源]")
|
logger.Log.Infoln("[√] [初始化数据源]")
|
||||||
|
|
||||||
global.DataSources = connGroup
|
global.DataSources = connGroup
|
||||||
|
|
||||||
|
if c.Mysql.AutoSync {
|
||||||
|
global.SyncModelToDataSource()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func reloadApp(c *config.BasicConfig) *fiber.App {
|
func reloadApp(c *config.BasicConfig) *fiber.App {
|
||||||
@ -68,12 +74,19 @@ func reloadApp(c *config.BasicConfig) *fiber.App {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
routes(app)
|
if c.Server.EnableRoutesMsg {
|
||||||
|
routes(app)
|
||||||
|
}
|
||||||
|
|
||||||
controller.SwaggerHandler(app)
|
controller.SwaggerHandler(app)
|
||||||
controller.HelloWorld(app)
|
controller.HelloWorld(app)
|
||||||
controller.ErrorHandler(app)
|
controller.ErrorHandler(app)
|
||||||
|
|
||||||
logger.Log.Infoln("[√] [服务启动完成]")
|
app.Hooks().OnListen(func() error {
|
||||||
|
logger.Log.Infoln("[√] [服务启动完成]")
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,10 +2,12 @@ package config
|
|||||||
|
|
||||||
type BasicConfig struct {
|
type BasicConfig struct {
|
||||||
Server ServerConfig `yaml:"server"`
|
Server ServerConfig `yaml:"server"`
|
||||||
|
Mysql MysqlConfig `yaml:"mysql"`
|
||||||
|
}
|
||||||
|
|
||||||
Mysql struct {
|
type MysqlConfig struct {
|
||||||
DataSources []string `yaml:"dataSources"`
|
DataSources []string `yaml:"dataSources"`
|
||||||
} `yaml:"mysql"`
|
AutoSync bool `yaml:"autoSync"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
@ -19,6 +21,8 @@ type ServerConfig struct {
|
|||||||
StrictRouting bool `yaml:"strictRouting"`
|
StrictRouting bool `yaml:"strictRouting"`
|
||||||
// 设置 Http 请求头中的 Server 名称
|
// 设置 Http 请求头中的 Server 名称
|
||||||
ServerHeader string `yaml:"serverHeader"`
|
ServerHeader string `yaml:"serverHeader"`
|
||||||
|
// 是否添加 /routes 路由 用于获取所有路由信息
|
||||||
|
EnableRoutesMsg bool `yaml:"enableRoutesMsg"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RedisConfig struct {
|
type RedisConfig struct {
|
||||||
|
12
config.yaml
12
config.yaml
@ -1,10 +1,20 @@
|
|||||||
server:
|
server:
|
||||||
|
# 监听地址 :3000 或 127.0.0.1:3000
|
||||||
addr: ":3000"
|
addr: ":3000"
|
||||||
preFork: true
|
# 启用多进程
|
||||||
|
preFork: false
|
||||||
|
# 路由大小写严格
|
||||||
caseSensitive: true
|
caseSensitive: true
|
||||||
|
# 严格路由模式
|
||||||
strictRouting: false
|
strictRouting: false
|
||||||
|
# 服务器名称
|
||||||
serverHeader: SkServer
|
serverHeader: SkServer
|
||||||
|
# 启用路由信息 /routes
|
||||||
|
enableRoutesMsg: true
|
||||||
|
|
||||||
mysql:
|
mysql:
|
||||||
|
# 自动同步数据库/表结构
|
||||||
|
autoSync: true
|
||||||
|
# 数据源 参考 xorm 数据源配置
|
||||||
dataSources:
|
dataSources:
|
||||||
- root:12341234@tcp(10.10.10.100:3306)/gofiber?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai
|
- root:12341234@tcp(10.10.10.100:3306)/gofiber?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai
|
@ -1,5 +1,24 @@
|
|||||||
package global
|
package global
|
||||||
|
|
||||||
import "xorm.io/xorm"
|
import (
|
||||||
|
"gofiber.study.skcks.cn/common/logger"
|
||||||
|
"gofiber.study.skcks.cn/model/user/models"
|
||||||
|
"xorm.io/xorm"
|
||||||
|
"xorm.io/xorm/names"
|
||||||
|
)
|
||||||
|
|
||||||
var DataSources *xorm.EngineGroup
|
var DataSources *xorm.EngineGroup
|
||||||
|
|
||||||
|
func SyncModelToDataSource() {
|
||||||
|
modelArr := []interface{ names.TableName }{&models.User{}}
|
||||||
|
|
||||||
|
logger.Log.Infof("[*] 同步数据库/表结构")
|
||||||
|
for _, model := range modelArr {
|
||||||
|
err := DataSources.Sync(model)
|
||||||
|
logger.Log.Infof("[√] 同步 %s 表 数据结构 成功", model.TableName())
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
logger.Log.Fatalf("[x] 同步 %s 表 数据结构时出错: %s", model.TableName(), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user