支持通过命令行参数指定配置文件路径,优化配置文件加载逻辑

This commit is contained in:
2026-01-26 23:34:52 +08:00
parent 97df85d4f8
commit e2a99b7888
4 changed files with 32 additions and 6 deletions

View File

@@ -33,6 +33,10 @@ cp config.example.yaml data/config.yaml
```bash ```bash
go run . go run .
# 或者指定配置文件路径
./BingPaper -config /path/to/config.yaml
# 或者使用简写
./BingPaper -c /path/to/config.yaml
``` ```
项目启动后会自动执行一次抓取任务,并根据 `cron.daily_spec` 设置定时任务。 项目启动后会自动执行一次抓取任务,并根据 `cron.daily_spec` 设置定时任务。

View File

@@ -23,12 +23,12 @@ import (
) )
// Init 初始化应用各项服务 // Init 初始化应用各项服务
func Init(webFS embed.FS) *gin.Engine { func Init(webFS embed.FS, configPath string) *gin.Engine {
// 0. 确保数据目录存在 // 0. 确保数据目录存在
_ = os.MkdirAll("data/picture", 0755) _ = os.MkdirAll("data/picture", 0755)
// 1. 初始化配置 // 1. 初始化配置
if err := config.Init(""); err != nil { if err := config.Init(configPath); err != nil {
log.Fatalf("Failed to initialize config: %v", err) log.Fatalf("Failed to initialize config: %v", err)
} }
cfg := config.GetConfig() cfg := config.GetConfig()

View File

@@ -2,6 +2,7 @@ package config
import ( import (
"fmt" "fmt"
"os"
"sync" "sync"
"time" "time"
@@ -134,12 +135,26 @@ func Init(configPath string) error {
v.SetDefault("admin.password_bcrypt", "$2a$10$fYHPeWHmwObephJvtlyH1O8DIgaLk5TINbi9BOezo2M8cSjmJchka") // 默认密码: admin123 v.SetDefault("admin.password_bcrypt", "$2a$10$fYHPeWHmwObephJvtlyH1O8DIgaLk5TINbi9BOezo2M8cSjmJchka") // 默认密码: admin123
if err := v.ReadInConfig(); err != nil { 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 return err
} }
// 如果文件不存在,我们使用默认值并尝试创建一个默认配置文件 // 如果文件不存在,我们使用默认值并尝试创建一个默认配置文件
fmt.Println("Config file not found, creating default config at ./data/config.yaml") targetConfigPath := configPath
if err := v.SafeWriteConfigAs("./data/config.yaml"); err != nil { 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) fmt.Printf("Warning: Failed to create default config file: %v\n", err)
} }
} }

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"embed" "embed"
"flag"
"fmt" "fmt"
"mime" "mime"
@@ -26,13 +27,19 @@ var webFS embed.FS
// @name Authorization // @name Authorization
func main() { func main() {
// 解析命令行参数
var configPath string
flag.StringVar(&configPath, "config", "", "配置文件路径")
flag.StringVar(&configPath, "c", "", "配置文件路径 (简写)")
flag.Parse()
// 注册常用 MIME 类型,确保嵌入式资源能被正确识别 // 注册常用 MIME 类型,确保嵌入式资源能被正确识别
mime.AddExtensionType(".js", "application/javascript") mime.AddExtensionType(".js", "application/javascript")
mime.AddExtensionType(".css", "text/css") mime.AddExtensionType(".css", "text/css")
mime.AddExtensionType(".svg", "image/svg+xml") mime.AddExtensionType(".svg", "image/svg+xml")
// 1. 初始化 // 1. 初始化
r := bootstrap.Init(webFS) r := bootstrap.Init(webFS, configPath)
// 2. 输出欢迎信息 // 2. 输出欢迎信息
bootstrap.LogWelcomeInfo() bootstrap.LogWelcomeInfo()