更新:1、依赖升级

2、module 名称地址
This commit is contained in:
2025-10-06 00:46:51 +08:00
parent 978a268f93
commit 44d4fcdbc4
11 changed files with 402 additions and 325 deletions

View File

@@ -1,40 +1,40 @@
package config
import (
"github.com/spf13/viper"
"log"
"net/url"
"os"
"strings"
"github.com/spf13/viper"
"gopkg.in/ini.v1"
)
type Log struct {
Handlers string
Level string
File string
Handlers string `ini:"handlers"`
Level string `ini:"level"`
File string `ini:"file"`
}
type Config struct {
Log Log
Log Log `ini:"log"`
Service struct {
Watch string
Addr string
Auth bool
}
Watch string `ini:"watch"`
Addr string `ini:"addr"`
Auth bool `ini:"auth"`
} `ini:"service"`
Data struct {
Dir string
}
Dir string `ini:"dir"`
} `ini:"data"`
Ztimer struct {
Db struct {
Addr string
User string
Password string
Database string
Schema string
Type string
}
}
Auth map[string]string
Addr string `ini:"addr"`
User string `ini:"user"`
Password string `ini:"password"`
Database string `ini:"database"`
Schema string `ini:"schema"`
Type string `ini:"type"`
} `ini:"db"`
} `ini:"ztimer"`
Auth map[string]string `ini:"auth"`
}
func ReadConfig() Config {
@@ -61,16 +61,16 @@ func ReadConfig() Config {
}*/
// 尝试从 /etc/ 目录下查找 zhub.ini 配置文件
viper.AddConfigPath("/etc/") // 添加 /etc/ 目录作为配置文件搜索路径
/*viper.AddConfigPath("/etc/") // 添加 /etc/ 目录作为配置文件搜索路径
viper.SetConfigName("zhub") // 指定配置文件名为 zhub
if err := viper.ReadInConfig(); err == nil {
if err := viper.Unmarshal(&conf); err != nil {
log.Fatalf("Failed to unmarshal config: %s", err.Error())
}
return conf
}
}*/
// 如果 /etc/ 目录下未找到配置文件,则尝试从当前程序运行目录下查找 app.ini 配置文件
dir, err := os.Getwd() // 获取程序运行目录
/*dir, err := os.Getwd() // 获取程序运行目录
if err != nil {
log.Fatalf("Failed to get current directory: %s", err.Error())
}
@@ -90,9 +90,15 @@ func ReadConfig() Config {
}
return conf
} else {
log.Fatalf("Config file not found: " + err.Error())
}*/
load, err := ini.Load("app.ini")
if err != nil {
log.Panicf("Failed to load config: %s", err.Error())
}
// 如果在 /etc/ 目录和当前程序所在目录下均未找到配置文件,则报错
log.Fatalf("Config file not found")
load.MapTo(&conf)
return conf
}
func InitLog(logConfig Log) {