mirror of
https://git.fightbot.fun/hxuanyu/BingPaper.git
synced 2026-02-15 07:19:33 +08:00
支持通过命令行参数指定配置文件路径,优化配置文件加载逻辑
This commit is contained in:
@@ -23,12 +23,12 @@ import (
|
||||
)
|
||||
|
||||
// Init 初始化应用各项服务
|
||||
func Init(webFS embed.FS) *gin.Engine {
|
||||
func Init(webFS embed.FS, configPath string) *gin.Engine {
|
||||
// 0. 确保数据目录存在
|
||||
_ = os.MkdirAll("data/picture", 0755)
|
||||
|
||||
// 1. 初始化配置
|
||||
if err := config.Init(""); err != nil {
|
||||
if err := config.Init(configPath); err != nil {
|
||||
log.Fatalf("Failed to initialize config: %v", err)
|
||||
}
|
||||
cfg := config.GetConfig()
|
||||
|
||||
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -134,12 +135,26 @@ func Init(configPath string) error {
|
||||
v.SetDefault("admin.password_bcrypt", "$2a$10$fYHPeWHmwObephJvtlyH1O8DIgaLk5TINbi9BOezo2M8cSjmJchka") // 默认密码: admin123
|
||||
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
||||
// 如果指定了配置文件但读取失败(且不是找不到文件的错误),或者没指定但也没找到
|
||||
_, isNotFound := err.(viper.ConfigFileNotFoundError)
|
||||
// 如果显式指定了文件,viper 报错可能不是 ConfigFileNotFoundError 而是 os.PathError
|
||||
if !isNotFound && configPath != "" {
|
||||
if _, statErr := os.Stat(configPath); os.IsNotExist(statErr) {
|
||||
isNotFound = true
|
||||
}
|
||||
}
|
||||
|
||||
if !isNotFound {
|
||||
return err
|
||||
}
|
||||
|
||||
// 如果文件不存在,我们使用默认值并尝试创建一个默认配置文件
|
||||
fmt.Println("Config file not found, creating default config at ./data/config.yaml")
|
||||
if err := v.SafeWriteConfigAs("./data/config.yaml"); err != nil {
|
||||
targetConfigPath := configPath
|
||||
if targetConfigPath == "" {
|
||||
targetConfigPath = "data/config.yaml"
|
||||
}
|
||||
fmt.Printf("Config file not found, creating default config at %s\n", targetConfigPath)
|
||||
if err := v.SafeWriteConfigAs(targetConfigPath); err != nil {
|
||||
fmt.Printf("Warning: Failed to create default config file: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user