From e2a99b788842d51a7d3bb653e575fc1222f14f0e Mon Sep 17 00:00:00 2001 From: hxuanyu <2252193204@qq.com> Date: Mon, 26 Jan 2026 23:34:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=80=9A=E8=BF=87=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E8=A1=8C=E5=8F=82=E6=95=B0=E6=8C=87=E5=AE=9A=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84=EF=BC=8C=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ internal/bootstrap/bootstrap.go | 4 ++-- internal/config/config.go | 21 ++++++++++++++++++--- main.go | 9 ++++++++- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index edf239d..8123635 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,10 @@ cp config.example.yaml data/config.yaml ```bash go run . +# 或者指定配置文件路径 +./BingPaper -config /path/to/config.yaml +# 或者使用简写 +./BingPaper -c /path/to/config.yaml ``` 项目启动后会自动执行一次抓取任务,并根据 `cron.daily_spec` 设置定时任务。 diff --git a/internal/bootstrap/bootstrap.go b/internal/bootstrap/bootstrap.go index bfbe6cc..a32476d 100644 --- a/internal/bootstrap/bootstrap.go +++ b/internal/bootstrap/bootstrap.go @@ -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() diff --git a/internal/config/config.go b/internal/config/config.go index a364703..86ce166 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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) } } diff --git a/main.go b/main.go index aa506b1..504c70b 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "embed" + "flag" "fmt" "mime" @@ -26,13 +27,19 @@ var webFS embed.FS // @name Authorization func main() { + // 解析命令行参数 + var configPath string + flag.StringVar(&configPath, "config", "", "配置文件路径") + flag.StringVar(&configPath, "c", "", "配置文件路径 (简写)") + flag.Parse() + // 注册常用 MIME 类型,确保嵌入式资源能被正确识别 mime.AddExtensionType(".js", "application/javascript") mime.AddExtensionType(".css", "text/css") mime.AddExtensionType(".svg", "image/svg+xml") // 1. 初始化 - r := bootstrap.Init(webFS) + r := bootstrap.Init(webFS, configPath) // 2. 输出欢迎信息 bootstrap.LogWelcomeInfo()