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

@@ -4,10 +4,13 @@ import (
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/gin-gonic/gin"
"billai-server/config"
"billai-server/database"
"billai-server/handler"
)
@@ -33,12 +36,32 @@ func main() {
fmt.Println(" 请在配置文件中指定正确的 Python 路径")
}
// 连接 MongoDB
if err := database.Connect(); err != nil {
fmt.Printf("⚠️ 警告: MongoDB 连接失败: %v\n", err)
fmt.Println(" 账单数据将不会存储到数据库")
os.Exit(1)
} else {
// 优雅关闭时断开连接
defer database.Disconnect()
}
// 创建路由
r := gin.Default()
// 注册路由
setupRoutes(r, outputDirAbs, pythonPathAbs)
// 监听系统信号
go func() {
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
fmt.Println("\n🛑 正在关闭服务...")
database.Disconnect()
os.Exit(0)
}()
// 启动服务
printAPIInfo()
r.Run(":" + config.Global.Port)
@@ -74,6 +97,7 @@ func printBanner(pythonPath, uploadDir, outputDir string) {
fmt.Printf("🐍 Python路径: %s\n", pythonPath)
fmt.Printf("📂 上传目录: %s\n", uploadDir)
fmt.Printf("📂 输出目录: %s\n", outputDir)
fmt.Printf("🍃 MongoDB: %s/%s\n", config.Global.MongoURI, config.Global.MongoDatabase)
fmt.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
}