156 lines
3.6 KiB
Go
156 lines
3.6 KiB
Go
package initialize
|
|
|
|
import (
|
|
"fmt"
|
|
"gitee.ltd/lxh/logger"
|
|
"gitee.ltd/lxh/logger/log"
|
|
"github.com/cowardmrx/go_aliyun_oss"
|
|
"github.com/fsnotify/fsnotify"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/glebarez/sqlite"
|
|
"github.com/go-resty/resty/v2"
|
|
"github.com/redis/go-redis/v9"
|
|
"github.com/spf13/viper"
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
gl "gorm.io/gorm/logger"
|
|
"os"
|
|
"time"
|
|
"wireguard-ui/config"
|
|
"wireguard-ui/global/client"
|
|
)
|
|
|
|
// Init
|
|
// @description: 初始化
|
|
func Init() {
|
|
initLogger() // 初始化日志
|
|
initConfig() // 读取配置文件
|
|
initWireguard() // 初始化wireguard客户端
|
|
initDatabase() // 初始化数据库
|
|
initRedis() // 初始化redis
|
|
initHttpClient() // 初始化http客户端
|
|
initOSS() // 初始化oss客户端链接
|
|
}
|
|
|
|
// 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.Config); err != nil {
|
|
log.Panicf("解析配置文件失败: %v", err.Error())
|
|
}
|
|
|
|
vp.OnConfigChange(func(in fsnotify.Event) {
|
|
if err := vp.Unmarshal(&config.Config); err != nil {
|
|
log.Errorf("配置文件变动,读取失败: %v", err.Error())
|
|
} else {
|
|
initDatabase()
|
|
initRedis()
|
|
initOSS()
|
|
}
|
|
})
|
|
|
|
vp.WatchConfig()
|
|
}
|
|
|
|
// InitWireguard
|
|
// @description: 初始化wireguard客户端
|
|
func initWireguard() {
|
|
c, err := wgctrl.New()
|
|
if err != nil {
|
|
log.Panicf("初始化wireguard客户端失败: %v", err.Error())
|
|
}
|
|
|
|
client.WireguardClient = c
|
|
}
|
|
|
|
// initDatabase
|
|
// @description: 初始化数据库
|
|
func initDatabase() {
|
|
// 不同驱动提供
|
|
var dbDialector gorm.Dialector
|
|
switch config.Config.Database.Driver {
|
|
case "mysql":
|
|
dbDialector = mysql.Open(config.Config.Database.GetDSN())
|
|
case "pgsql":
|
|
dbDialector = postgres.Open(config.Config.Database.GetDSN())
|
|
case "sqlite":
|
|
dbDialector = sqlite.Open(config.Config.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.Config.Database.Driver, err.Error())
|
|
}
|
|
|
|
client.DB = db
|
|
}
|
|
|
|
// initRedis
|
|
// @description: 初始化redis
|
|
func initRedis() {
|
|
c := redis.NewClient(&redis.Options{
|
|
Addr: fmt.Sprintf("%s:%d", config.Config.Cache.Host, config.Config.Cache.Port),
|
|
Password: config.Config.Cache.Password,
|
|
DB: config.Config.Cache.Db,
|
|
})
|
|
|
|
client.Redis = c
|
|
}
|
|
|
|
// initHttpClient
|
|
// @description: 初始化http客户端
|
|
func initHttpClient() {
|
|
client.HttpClient = resty.New()
|
|
}
|
|
|
|
// initOSS
|
|
// @description: 初始化oss客户端
|
|
func initOSS() {
|
|
if config.Config.File.Type != "oss" {
|
|
return
|
|
}
|
|
ossConfig := &go_aliyun_oss.AliOssConfig{
|
|
EndPoint: config.Config.File.Endpoint,
|
|
AccessKeyId: config.Config.File.AccessId,
|
|
AccessKeySecret: config.Config.File.AccessSecret,
|
|
BucketName: config.Config.File.BucketName,
|
|
OriginalFileName: false,
|
|
}
|
|
|
|
ossClient := ossConfig.CreateOssConnect()
|
|
client.OSS = ossClient
|
|
}
|
|
|
|
// initLogger
|
|
// @description: 初始化日志
|
|
func initLogger() {
|
|
|
|
mode := logger.Dev
|
|
if os.Getenv("GIN_MODE") == gin.ReleaseMode {
|
|
mode = logger.Prod
|
|
}
|
|
|
|
logger.InitLogger(logger.LogConfig{
|
|
Mode: mode,
|
|
FileEnable: true,
|
|
})
|
|
}
|