|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- package config
-
- import (
- "encoding/json"
- "fmt"
- "github.com/imdario/mergo"
- "os"
- "path/filepath"
- "strings"
- )
-
- // Load 加载配置文件
- func Load(filePath string, cfg interface{}) error {
- fileData, err := os.ReadFile(filePath)
- if err != nil {
- return err
- }
-
- return json.Unmarshal(fileData, cfg)
- }
-
- // DefaultLoad 默认的加载配置的方式:
- // 从应用程序上上级的conf目录中读取,文件名:<moduleName>.config.json
- func DefaultLoad(modeulName string, defCfg interface{}) error {
- execPath, err := os.Executable()
- if err != nil {
- return err
- }
-
- if strings.Contains(execPath, "scheduler") {
- execPath = "D:\\Work\\Codes\\new\\workspace\\workspace\\scheduler\\common\\assets\\confs\\"
- }
-
- if strings.Contains(execPath, "storage") {
- execPath = "D:\\Work\\Codes\\new\\workspace\\workspace\\storage\\common\\assets\\confs\\"
- }
-
- if strings.Contains(execPath, "gateway") {
- execPath = "D:\\Work\\Codes\\new\\workspace\\workspace\\gateway\\assets\\confs\\"
- }
-
- // TODO 可以考虑根据环境变量读取不同的配置
- configFilePath := filepath.Join(filepath.Dir(execPath), "..", "confs", fmt.Sprintf("%s.config.json", modeulName))
- return Load(configFilePath, defCfg)
- }
-
- // Merge 合并两个配置结构体。会将src中的非空字段覆盖到dst的同名字段中。两个结构的类型必须相同
- func Merge(dst interface{}, src interface{}) error {
- return mergo.Merge(dst, src)
- }
|