fix: 修正config路径错误

This commit is contained in:
yarnom 2025-10-14 11:57:33 +08:00
parent e8fcd550c1
commit c421aed925
2 changed files with 29 additions and 9 deletions

View File

@ -84,12 +84,23 @@ func GetConfig() *Config {
// loadConfig 从配置文件加载配置 // loadConfig 从配置文件加载配置
func (c *Config) loadConfig() error { func (c *Config) loadConfig() error {
// 尝试多个位置查找配置文件 // 尝试多个位置查找配置文件兼容从仓库根目录、bin目录、系统安装路径运行
exePath, _ := os.Executable()
exeDir := ""
if exePath != "" {
exeDir = filepath.Dir(exePath)
}
configPaths := []string{ configPaths := []string{
"config.yaml", // 当前目录 // 工作目录及其上级
"../config.yaml", // 上级目录 "config.yaml",
"../../config.yaml", // 项目根目录 "../config.yaml",
filepath.Join(os.Getenv("HOME"), ".weatherstation/config.yaml"), // 用户目录 "../../config.yaml",
// 可执行文件所在目录(用于 /opt/weatherstation/bin 场景)
filepath.Join(exeDir, "config.yaml"),
filepath.Join(exeDir, "..", "config.yaml"),
// 系统级与用户级
"/etc/weatherstation/config.yaml",
filepath.Join(os.Getenv("HOME"), ".weatherstation", "config.yaml"),
} }
var data []byte var data []byte

View File

@ -18,6 +18,13 @@ import (
"weatherstation/internal/database" "weatherstation/internal/database"
) )
func getenvDefault(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}
// Exporter 负责每10分钟导出 CSV含ZTD融合 // Exporter 负责每10分钟导出 CSV含ZTD融合
type Exporter struct { type Exporter struct {
pg *sql.DB pg *sql.DB
@ -47,10 +54,11 @@ func NewExporterWithOptions(opts ExporterOptions) *Exporter {
} }
// 创建导出专用日志文件(追加模式) // 创建导出专用日志文件(追加模式)
if err := os.MkdirAll("export_data", 0o755); err != nil { outBase := getenvDefault("EXPORT_DIR", "export_data")
if err := os.MkdirAll(outBase, 0o755); err != nil {
log.Printf("创建导出日志目录失败: %v", err) log.Printf("创建导出日志目录失败: %v", err)
} }
f, err := os.OpenFile(filepath.Join("export_data", "export.log"), f, err := os.OpenFile(filepath.Join(outBase, "export.log"),
os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644) os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
if err != nil { if err != nil {
log.Printf("创建导出日志文件失败: %v", err) log.Printf("创建导出日志文件失败: %v", err)
@ -112,8 +120,9 @@ func (e *Exporter) Start(ctx context.Context) error {
// exportBucket 导出一个10分钟桶CST // exportBucket 导出一个10分钟桶CST
func (e *Exporter) exportBucket(ctx context.Context, bucketStart, bucketEnd time.Time) error { func (e *Exporter) exportBucket(ctx context.Context, bucketStart, bucketEnd time.Time) error {
utcDay := bucketEnd.UTC().Format("2006-01-02") utcDay := bucketEnd.UTC().Format("2006-01-02")
outDir := filepath.Join("export_data") outBase := getenvDefault("EXPORT_DIR", "export_data")
histDir := filepath.Join("export_data", "history") outDir := filepath.Join(outBase)
histDir := filepath.Join(outBase, "history")
if err := os.MkdirAll(histDir, 0o755); err != nil { if err := os.MkdirAll(histDir, 0o755); err != nil {
return err return err
} }