✨ 完成项目配置初始化
This commit is contained in:
11
initialize/clients.go
Normal file
11
initialize/clients.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package initialize
|
||||
|
||||
import "online_code/client"
|
||||
|
||||
// 初始化数据库客户端
|
||||
func initClient() {
|
||||
// 初始化MySQL
|
||||
client.InitMySQLClient()
|
||||
// 初始化Redis
|
||||
client.InitRedisClient()
|
||||
}
|
68
initialize/config.go
Normal file
68
initialize/config.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/logger/log"
|
||||
"online_code/config"
|
||||
|
||||
nr "git.echol.cn/loser/nacos-viper-remote"
|
||||
"github.com/caarlos0/env/v6"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/spf13/viper"
|
||||
_ "github.com/spf13/viper/remote"
|
||||
)
|
||||
|
||||
var vp *viper.Viper
|
||||
|
||||
// 初始化Nacos
|
||||
func initNacos() {
|
||||
// 初始化Nacos配置
|
||||
if err := env.Parse(&config.Nacos); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if config.Nacos.Host == "" {
|
||||
log.Panic("Nacos配置错误")
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化配置文件
|
||||
func initConfig() {
|
||||
vp = viper.New()
|
||||
|
||||
// 配置 Viper for Nacos 的远程仓库参数
|
||||
nr.SetOptions(&nr.Option{
|
||||
Url: config.Nacos.Host, // nacos server 多地址需要地址用;号隔开,如 Url: "loc1;loc2;loc3"
|
||||
Port: config.Nacos.Port, // nacos server端口号
|
||||
NamespaceId: config.Nacos.NamespaceId, // nacos namespace
|
||||
GroupName: "DEFAULT_GROUP", // nacos group
|
||||
Config: nr.Config{DataId: config.Nacos.CenterConfigName}, // nacos DataID
|
||||
Auth: nil, // 如果需要验证登录,需要此参数
|
||||
})
|
||||
err := vp.AddRemoteProvider("nacos", config.Nacos.Host, "")
|
||||
if err != nil {
|
||||
log.Panicf("%s", err)
|
||||
}
|
||||
vp.SetConfigType("yaml")
|
||||
//尝试进行配置读取
|
||||
if err = vp.ReadRemoteConfig(); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
//异步监听Nacos中的配置变化,如发生配置更改,会直接同步到 viper实例中。
|
||||
err = vp.WatchRemoteConfigOnChannel()
|
||||
if err != nil {
|
||||
log.Errorf("监听远程配置变动失败: %v", err)
|
||||
}
|
||||
|
||||
// 解析配置文件为结构体
|
||||
if err = vp.Unmarshal(&config.Scd); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
vp.WatchConfig() // 监听配置文件变化
|
||||
vp.OnConfigChange(func(e fsnotify.Event) {
|
||||
log.Info("配置文件发生变动:", e.Name)
|
||||
if err = vp.Unmarshal(&config.Scd); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
// 配置文件发生变动,重新初始化一下连接信息
|
||||
initClient()
|
||||
})
|
||||
}
|
19
initialize/db_table.go
Normal file
19
initialize/db_table.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/logger/log"
|
||||
"online_code/client"
|
||||
"online_code/models/entity"
|
||||
)
|
||||
|
||||
// 初始化数据库表
|
||||
func databaseTable() {
|
||||
dbs := []any{
|
||||
new(entity.User), // 管理员用户
|
||||
}
|
||||
|
||||
if err := client.MySQL.AutoMigrate(dbs...); err != nil {
|
||||
log.Panicf("数据库表预初始化处理:%s", err.Error())
|
||||
}
|
||||
log.Debugf("数据库表预初始化处理完成")
|
||||
}
|
10
initialize/init.go
Normal file
10
initialize/init.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package initialize
|
||||
|
||||
// InitSystem 初始化系统
|
||||
func InitSystem() {
|
||||
//initNacos() // 初始化nacos
|
||||
//initConfig() // 初始化配置
|
||||
initLocaConfig() // 初始化本地配置
|
||||
initClient() // 初始化连接池等信息
|
||||
databaseTable() // 初始化数据库表信息
|
||||
}
|
33
initialize/locaConfig.go
Normal file
33
initialize/locaConfig.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.echol.cn/loser/logger/log"
|
||||
"online_code/config"
|
||||
|
||||
"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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user