feat: server connect mongo

This commit is contained in:
CHE LIANG ZHAO
2026-01-08 23:42:01 +08:00
parent ccd2d0386a
commit c1ffe2e822
17 changed files with 1455 additions and 338 deletions

View File

@@ -17,6 +17,12 @@ type Config struct {
CleanScript string // 清理脚本路径
UploadDir string // 上传文件目录
OutputDir string // 输出文件目录
// MongoDB 配置
MongoURI string // MongoDB 连接 URI
MongoDatabase string // 数据库名称
MongoRawCollection string // 原始数据集合名称
MongoCleanedCollection string // 清洗后数据集合名称
}
// configFile YAML 配置文件结构
@@ -32,6 +38,14 @@ type configFile struct {
Upload string `yaml:"upload"`
Output string `yaml:"output"`
} `yaml:"directories"`
MongoDB struct {
URI string `yaml:"uri"`
Database string `yaml:"database"`
Collections struct {
Raw string `yaml:"raw"`
Cleaned string `yaml:"cleaned"`
} `yaml:"collections"`
} `yaml:"mongodb"`
}
// Global 全局配置实例
@@ -102,6 +116,12 @@ func Load() {
Global.UploadDir = "server/uploads"
Global.OutputDir = "server/outputs"
// MongoDB 默认值
Global.MongoURI = getEnvOrDefault("MONGO_URI", "mongodb://localhost:27017")
Global.MongoDatabase = getEnvOrDefault("MONGO_DATABASE", "billai")
Global.MongoRawCollection = getEnvOrDefault("MONGO_RAW_COLLECTION", "bills_raw")
Global.MongoCleanedCollection = getEnvOrDefault("MONGO_CLEANED_COLLECTION", "bills_cleaned")
// 查找配置文件
configPath := configFilePath
if !filepath.IsAbs(configPath) {
@@ -128,6 +148,19 @@ func Load() {
if cfg.Directories.Output != "" {
Global.OutputDir = cfg.Directories.Output
}
// MongoDB 配置
if cfg.MongoDB.URI != "" {
Global.MongoURI = cfg.MongoDB.URI
}
if cfg.MongoDB.Database != "" {
Global.MongoDatabase = cfg.MongoDB.Database
}
if cfg.MongoDB.Collections.Raw != "" {
Global.MongoRawCollection = cfg.MongoDB.Collections.Raw
}
if cfg.MongoDB.Collections.Cleaned != "" {
Global.MongoCleanedCollection = cfg.MongoDB.Collections.Cleaned
}
}
// 环境变量覆盖
@@ -140,6 +173,19 @@ func Load() {
if root := os.Getenv("BILLAI_ROOT"); root != "" {
Global.ProjectRoot = root
}
// MongoDB 环境变量覆盖
if uri := os.Getenv("MONGO_URI"); uri != "" {
Global.MongoURI = uri
}
if db := os.Getenv("MONGO_DATABASE"); db != "" {
Global.MongoDatabase = db
}
if rawColl := os.Getenv("MONGO_RAW_COLLECTION"); rawColl != "" {
Global.MongoRawCollection = rawColl
}
if cleanedColl := os.Getenv("MONGO_CLEANED_COLLECTION"); cleanedColl != "" {
Global.MongoCleanedCollection = cleanedColl
}
}
// ResolvePath 解析路径(相对路径转为绝对路径)