wireguard-dashboard/initialize/init.go

139 lines
3.3 KiB
Go
Raw Normal View History

2024-03-05 16:59:37 +08:00
package initialize
import (
"fmt"
2024-03-07 15:11:29 +08:00
"gitee.ltd/lxh/logger"
"github.com/cowardmrx/go_aliyun_oss"
2024-05-08 17:21:52 +08:00
"github.com/glebarez/sqlite"
"github.com/go-resty/resty/v2"
2024-03-05 16:59:37 +08:00
"github.com/redis/go-redis/v9"
"golang.zx2c4.com/wireguard/wgctrl"
"gopkg.in/yaml.v3"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
2024-06-13 15:34:22 +08:00
gl "gorm.io/gorm/logger"
2024-03-05 16:59:37 +08:00
"log"
"os"
2024-06-13 15:34:22 +08:00
"time"
2024-03-05 16:59:37 +08:00
"wireguard-dashboard/client"
"wireguard-dashboard/config"
)
// Init
// @description: 初始化
func Init() {
2024-03-07 15:11:29 +08:00
initLogger() // 初始化日志
initConfig() // 读取配置文件
initWireguard() // 初始化wireguard客户端
initDatabase() // 初始化数据库
initRedis() // 初始化redis
initHttpClient() // 初始化http客户端
initOSS() // 初始化oss客户端链接
2024-03-05 16:59:37 +08:00
}
// initConfig
// @description: 初始化配置
func initConfig() {
2024-05-09 09:39:52 +08:00
configBytes, err := os.ReadFile("app.yaml")
2024-03-05 16:59:37 +08:00
if err != nil {
log.Panicf("读取配置文件失败: %v", err.Error())
}
err = yaml.Unmarshal(configBytes, &config.Config)
if err != nil {
log.Panicf("解析配置文件失败: %v", err.Error())
}
}
// 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())
}
2024-06-13 15:34:22 +08:00
logLevel := gl.Info
2024-06-14 17:23:38 +08:00
//if os.Getenv("GIN_MODE") == "release" {
// logLevel = gl.Error
//}
2024-06-13 15:34:22 +08:00
2024-03-05 16:59:37 +08:00
db, err := gorm.Open(dbDialector, &gorm.Config{
2024-06-13 15:34:22 +08:00
Logger: logger.NewGormLoggerWithConfig(gl.Config{
SlowThreshold: time.Second, // Slow SQL threshold
IgnoreRecordNotFoundError: false, // 忽略没找到结果的错误
LogLevel: logLevel, // Log level
Colorful: false, // Disable color
}),
2024-03-05 16:59:37 +08:00
})
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.Redis.Host, config.Config.Redis.Port),
Password: config.Config.Redis.Password,
DB: config.Config.Redis.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
}
2024-03-07 15:11:29 +08:00
// initLogger
// @description: 初始化日志
func initLogger() {
logger.InitLogger(logger.LogConfig{
Mode: logger.Dev,
FileEnable: true,
})
}