✨ init project
This commit is contained in:
10
initialize/clients.go
Normal file
10
initialize/clients.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package initialize
|
||||
|
||||
import "Lee-WineList/client"
|
||||
|
||||
// 初始化数据库客户端
|
||||
func initClient() {
|
||||
client.InitMySQLClient()
|
||||
client.InitRedisClient()
|
||||
//client.InitPostgreSQLClient()
|
||||
}
|
40
initialize/config.go
Normal file
40
initialize/config.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"Lee-WineList/config"
|
||||
"fmt"
|
||||
"git.echol.cn/loser/logger/log"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var localVp *viper.Viper
|
||||
|
||||
func initLocaConfig() {
|
||||
localVp = viper.New()
|
||||
localVp.SetConfigName("config") // 文件名-无需文件后缀
|
||||
localVp.SetConfigType("yaml") // 文件扩展名
|
||||
localVp.AddConfigPath(".")
|
||||
// 处理读取配置文件的错误
|
||||
if err := localVp.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
panic(fmt.Errorf("未找到配置文件,请检查路径: %s \n", err))
|
||||
} else {
|
||||
panic(fmt.Errorf("文件已找到,但读取文件时出错: %s \n", err))
|
||||
}
|
||||
}
|
||||
// 解析配置文件为结构体
|
||||
if err := localVp.Unmarshal(&config.Scd); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
localVp.WatchConfig() // 监听配置文件变化
|
||||
localVp.OnConfigChange(func(e fsnotify.Event) {
|
||||
log.Info("配置文件发生变动:", e.Name)
|
||||
fmt.Printf("配置文件发生变动:%s", e.Name)
|
||||
if err := localVp.Unmarshal(&config.Scd); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
// 配置文件发生变动,重新初始化一下连接信息
|
||||
initLocaConfig()
|
||||
})
|
||||
}
|
21
initialize/db_table.go
Normal file
21
initialize/db_table.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"Lee-WineList/client"
|
||||
"Lee-WineList/model/entity"
|
||||
"git.echol.cn/loser/logger/log"
|
||||
)
|
||||
|
||||
// 初始化数据库表
|
||||
func databaseTable() {
|
||||
dbs := []any{
|
||||
entity.User{},
|
||||
entity.AdminUser{},
|
||||
entity.OAuth2Client{},
|
||||
}
|
||||
|
||||
if err := client.MySQL.AutoMigrate(dbs...); err != nil {
|
||||
log.Panicf("数据库表预初始化处理:%s", err.Error())
|
||||
}
|
||||
log.Debugf("数据库表预初始化处理完成")
|
||||
}
|
8
initialize/init.go
Normal file
8
initialize/init.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package initialize
|
||||
|
||||
// InitSystem 初始化系统
|
||||
func InitSystem() {
|
||||
initLocaConfig() // 初始化本地配置
|
||||
initClient() // 初始化连接池等信息
|
||||
databaseTable() // 初始化数据库表信息
|
||||
}
|
Reference in New Issue
Block a user