mirror of
https://git.fightbot.fun/hxuanyu/BingPaper.git
synced 2026-02-15 04:39:32 +08:00
支持通过命令行参数指定配置文件路径,优化配置文件加载逻辑
This commit is contained in:
@@ -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` 设置定时任务。
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
9
main.go
9
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()
|
||||
|
||||
Reference in New Issue
Block a user