// Package config 提供 Web 服务的基础配置加载(Spring Boot 风格 properties 文件)。 package config import ( "github.com/magiconair/properties" ) // Config 服务配置 type Config struct { Port int // HTTP 监听端口 Dsn string // 数据库路径(相对路径相对服务启动目录) AutoOpen bool // 启动后自动打开浏览器 } // appConfig 当前生效配置(供 api 层读取) var appConfig Config // GetAppConfig 返回当前生效配置 func GetAppConfig() Config { return appConfig } // LoadAppConfig 从 application.properties 读取配置;文件缺失时使用默认值 func LoadAppConfig() { appConfig = Config{Port: 8080, Dsn: "media-info.db", AutoOpen: true} p, err := properties.LoadFile("application.properties", properties.UTF8) if err != nil { return } if port := p.GetInt("server.port", 0); port > 0 { appConfig.Port = port } if v, ok := p.Get("database.dsn"); ok && v != "" { appConfig.Dsn = v } appConfig.AutoOpen = p.GetBool("server.autoOpen", true) }