:tada:初步完成用户
This commit is contained in:
126
initialize/init.go
Normal file
126
initialize/init.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"gitee.ltd/lxh/logger"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"github.com/coocood/freecache"
|
||||
"github.com/cowardmrx/go_aliyun_oss"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/glebarez/sqlite"
|
||||
"github.com/go-resty/resty/v2"
|
||||
"github.com/spf13/viper"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
gl "gorm.io/gorm/logger"
|
||||
"time"
|
||||
"website-nav/config"
|
||||
"website-nav/global/client"
|
||||
)
|
||||
|
||||
func Init() {
|
||||
initLogger()
|
||||
initHttpClient()
|
||||
initConfig()
|
||||
initDatabase()
|
||||
initCache()
|
||||
initOSS()
|
||||
}
|
||||
|
||||
// initLogger
|
||||
// @description: 初始化日志
|
||||
func initLogger() {
|
||||
logger.InitLogger(logger.LogConfig{
|
||||
Mode: logger.Dev,
|
||||
FileEnable: true,
|
||||
})
|
||||
}
|
||||
|
||||
// initHttpClient
|
||||
// @description: 初始化http客户端
|
||||
func initHttpClient() {
|
||||
client.HttpClient = resty.New()
|
||||
}
|
||||
|
||||
// initConfig
|
||||
// @description: 加载配置文件
|
||||
func initConfig() {
|
||||
vp := viper.New()
|
||||
vp.SetConfigFile("app.yaml")
|
||||
if err := vp.ReadInConfig(); err != nil {
|
||||
log.Panicf("读取配置文件失败: %v", err.Error())
|
||||
}
|
||||
|
||||
if err := vp.Unmarshal(&config.GlobalConfig); err != nil {
|
||||
log.Panicf("解析配置文件失败: %v", err.Error())
|
||||
}
|
||||
|
||||
vp.OnConfigChange(func(in fsnotify.Event) {
|
||||
if err := vp.Unmarshal(&config.GlobalConfig); err != nil {
|
||||
log.Errorf("配置文件变动,读取失败: %v", err.Error())
|
||||
} else {
|
||||
initDatabase()
|
||||
initCache()
|
||||
initOSS()
|
||||
}
|
||||
})
|
||||
|
||||
vp.WatchConfig()
|
||||
}
|
||||
|
||||
// initDatabase
|
||||
// @description: 初始化持久化数据库链接
|
||||
func initDatabase() {
|
||||
// 不同驱动提供
|
||||
var dbDialector gorm.Dialector
|
||||
switch config.GlobalConfig.Database.Driver {
|
||||
case "mysql":
|
||||
dbDialector = mysql.Open(config.GlobalConfig.Database.GetDSN())
|
||||
case "pgsql":
|
||||
dbDialector = postgres.Open(config.GlobalConfig.Database.GetDSN())
|
||||
case "sqlite":
|
||||
dbDialector = sqlite.Open(config.GlobalConfig.Database.GetDSN())
|
||||
}
|
||||
|
||||
logLevel := gl.Info
|
||||
|
||||
db, err := gorm.Open(dbDialector, &gorm.Config{
|
||||
Logger: logger.NewGormLoggerWithConfig(gl.Config{
|
||||
SlowThreshold: time.Second, // Slow SQL threshold
|
||||
IgnoreRecordNotFoundError: false, // 忽略没找到结果的错误
|
||||
LogLevel: logLevel, // Log level
|
||||
Colorful: false, // Disable color
|
||||
}),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Panicf("链接数据库[%s]失败:%v", config.GlobalConfig.Database.Driver, err.Error())
|
||||
}
|
||||
|
||||
client.DB = db
|
||||
}
|
||||
|
||||
// initCache
|
||||
// @description: 初始化缓存
|
||||
func initCache() {
|
||||
cache := freecache.NewCache(1024 * 1024 * 10)
|
||||
client.Cache = cache
|
||||
}
|
||||
|
||||
// initOSS
|
||||
// @description: 初始化oss客户端
|
||||
func initOSS() {
|
||||
if config.GlobalConfig.File.Type != "oss" {
|
||||
return
|
||||
}
|
||||
ossConfig := &go_aliyun_oss.AliOssConfig{
|
||||
EndPoint: config.GlobalConfig.File.Endpoint,
|
||||
AccessKeyId: config.GlobalConfig.File.AccessId,
|
||||
AccessKeySecret: config.GlobalConfig.File.AccessSecret,
|
||||
BucketName: config.GlobalConfig.File.BucketName,
|
||||
OriginalFileName: false,
|
||||
}
|
||||
|
||||
ossClient := ossConfig.CreateOssConnect()
|
||||
client.OSS = ossClient
|
||||
}
|
||||
Reference in New Issue
Block a user