core/conf/new.go

123 lines
3.0 KiB
Go
Raw Normal View History

2025-10-03 19:55:20 +08:00
// Package conf 提供配置管理功能
// 支持YAML配置文件加载、环境变量替换、配置验证等
2025-02-07 13:01:38 +08:00
package conf
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
2025-02-07 19:24:54 +08:00
"math/rand/v2"
2025-02-07 13:01:38 +08:00
"git.apinb.com/bsm-sdk/core/env"
"git.apinb.com/bsm-sdk/core/printer"
2025-03-27 03:08:59 +08:00
"git.apinb.com/bsm-sdk/core/utils"
2025-02-07 13:01:38 +08:00
"git.apinb.com/bsm-sdk/core/vars"
yaml "gopkg.in/yaml.v3"
)
2025-10-03 19:55:20 +08:00
// New 加载配置文件
// srvKey: 服务键名
// cfg: 配置结构体指针
2025-02-07 20:33:27 +08:00
func New(srvKey string, cfg any) {
env.NewEnv()
2025-02-07 13:01:38 +08:00
// 设置服务键
vars.ServiceKey = srvKey
// 获取主机名
vars.HostName, _ = os.Hostname()
2025-10-03 19:55:20 +08:00
// 构造配置文件路径,输出配置文件信息
2025-03-27 02:43:15 +08:00
cfp := fmt.Sprintf("%s_%s.yaml", strings.ToLower(srvKey), env.Runtime.Mode)
2025-02-07 20:33:27 +08:00
cfp = filepath.Join(env.Runtime.Prefix, "etc", cfp)
2025-03-27 03:10:56 +08:00
// 配置文件不存在则读取Workspace配置文件
if !utils.PathExists(cfp) {
cfp = fmt.Sprintf("workspace_%s_%s.yaml", strings.ToLower(env.Runtime.Workspace), env.Runtime.Mode)
cfp = filepath.Join(env.Runtime.Prefix, "etc", cfp)
}
printer.Info("[BSM - %s] Config File: %s", srvKey, cfp)
printer.Info("[BSM - %s] Check Configure ...", vars.ServiceKey)
2025-02-07 13:01:38 +08:00
// 读取配置文件内容
yamlFile, err := os.ReadFile(cfp)
if err != nil {
log.Fatalf("ERROR: %v", err)
}
// 替换环境变量
yamlString := os.ExpandEnv(string(yamlFile))
2025-10-03 19:55:20 +08:00
// 检查配置文件中是否存在Service字段
if !strings.Contains(yamlString, "Service:") {
2025-02-07 13:01:38 +08:00
log.Fatalln("ERROR: Service Not Nil", cfp)
}
2025-10-03 19:55:20 +08:00
// 解析YAML到配置结构体
err = yaml.Unmarshal([]byte(yamlString), cfg)
2025-02-07 13:01:38 +08:00
if err != nil {
log.Fatalf("ERROR: %v", err)
}
}
2025-10-03 19:55:20 +08:00
// NotNil 验证必需配置项不为空
// values: 需要验证的配置值列表
2025-02-07 13:01:38 +08:00
func NotNil(values ...string) {
for _, value := range values {
if strings.TrimSpace(value) == "" {
2025-02-20 17:41:11 +08:00
log.Fatalln("ERROR:Must config key not nil")
2025-02-07 13:01:38 +08:00
}
}
}
2025-10-03 19:55:20 +08:00
// PrintInfo 打印配置信息
// addr: 服务地址
2025-03-30 13:23:46 +08:00
func PrintInfo(addr string) {
printer.Success("[BSM - %s] Config Check Success.", vars.ServiceKey)
printer.Info("[BSM - %s] Service Name: %s", vars.ServiceKey, vars.ServiceKey)
printer.Info("[BSM - %s] Runtime Mode: %s", vars.ServiceKey, env.Runtime.Mode)
2025-03-27 03:08:59 +08:00
}
2025-10-03 19:55:20 +08:00
// CheckPort 检查端口配置,如果为空则生成随机端口
// port: 端口字符串
// 返回: 有效的端口字符串
2025-03-30 13:23:46 +08:00
func CheckPort(port string) string {
if port == "" {
2025-02-07 19:24:54 +08:00
r := rand.New(rand.NewPCG(1000, uint64(time.Now().UnixNano())))
2025-03-30 13:23:46 +08:00
p := r.IntN(65535-1024) + 1024 // 生成1024到65535之间的随机端口
return utils.Int2String(p)
2025-02-07 13:01:38 +08:00
}
return port
}
2025-03-29 15:02:49 +08:00
2025-10-03 19:55:20 +08:00
// CheckIP 检查IP配置如果为空则获取本机IP
// ip: IP地址字符串
// 返回: 有效的IP地址字符串
2025-03-29 15:02:49 +08:00
func CheckIP(ip string) string {
if ip == "" {
return utils.GetLocationIP()
}
return ip
}
2025-10-04 17:42:26 +08:00
// 初始化Logger配置
func InitLoggerConf(cfg *LogConf) *LogConf {
if cfg == nil {
return &LogConf{
Name: strings.ToLower(vars.ServiceKey),
Level: vars.LogLevel(vars.DEBUG),
Dir: cfg.Dir,
Endpoint: cfg.Endpoint,
Console: cfg.Console,
File: cfg.File,
Remote: cfg.Remote,
}
}
return cfg
}