refactor: 终端重构、系统参数配置调整

This commit is contained in:
meilin.huang
2023-08-31 21:49:20 +08:00
parent 537b179e78
commit d51cd4b289
27 changed files with 1055 additions and 356 deletions

View File

@@ -3,10 +3,12 @@ package config
import (
"flag"
"fmt"
"log/slog"
"mayfly-go/pkg/utils/assert"
"mayfly-go/pkg/utils/ymlx"
"os"
"path/filepath"
"strconv"
)
// 配置文件映射对象
@@ -21,7 +23,9 @@ func Init() {
// 读取配置文件信息
yc := &Config{}
if err := ymlx.LoadYml(startConfigParam.ConfigFilePath, yc); err != nil {
panic(fmt.Sprintf("读取配置文件[%s]失败: %s", startConfigParam.ConfigFilePath, err.Error()))
slog.Warn(fmt.Sprintf("读取配置文件[%s]失败: %s, 使用系统默认配置或环境变量配置", startConfigParam.ConfigFilePath, err.Error()))
// 设置默认信息,主要方便后续的系统环境变量替换
yc.SetDefaultConfig()
}
// 校验配置文件内容信息
yc.Valid()
@@ -56,6 +60,15 @@ func (c *Config) Valid() {
// 替换系统环境变量,如果环境变量中存在该值,则优秀使用环境变量设定的值
func (c *Config) ReplaceOsEnv() {
serverPort := os.Getenv("MAYFLY_SERVER_PORT")
if serverPort != "" {
if num, err := strconv.Atoi(serverPort); err != nil {
panic("环境变量-[MAYFLY_SERVER_PORT]-服务端口号需为数字")
} else {
c.Server.Port = num
}
}
dbHost := os.Getenv("MAYFLY_DB_HOST")
if dbHost != "" {
c.Mysql.Host = dbHost
@@ -86,3 +99,29 @@ func (c *Config) ReplaceOsEnv() {
c.Jwt.Key = jwtKey
}
}
func (c *Config) SetDefaultConfig() {
c.Server = &Server{
Model: "release",
Port: 8888,
MachineRecPath: "./rec",
}
c.Jwt = &Jwt{
ExpireTime: 1440,
}
c.Aes = &Aes{
Key: "1111111111111111",
}
c.Mysql = &Mysql{
Host: "localhost:3306",
Config: "charset=utf8&loc=Local&parseTime=true",
MaxIdleConns: 5,
}
c.Log = &Log{
Level: "info",
}
}