- 将 Python 代码移至 analyzer/ 目录(含 venv) - 拆分 Go 服务器代码为模块化结构: - config/: 配置加载 - model/: 请求/响应模型 - service/: 业务逻辑 - handler/: API处理器 - 添加 .gitignore 文件 - 删除旧的独立脚本文件
90 lines
2.6 KiB
Go
90 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"billai-server/config"
|
|
"billai-server/handler"
|
|
)
|
|
|
|
func main() {
|
|
// 加载配置
|
|
config.Load()
|
|
|
|
// 解析路径
|
|
uploadDirAbs := config.ResolvePath(config.Global.UploadDir)
|
|
outputDirAbs := config.ResolvePath(config.Global.OutputDir)
|
|
pythonPathAbs := config.ResolvePath(config.Global.PythonPath)
|
|
|
|
// 确保目录存在
|
|
os.MkdirAll(uploadDirAbs, 0755)
|
|
os.MkdirAll(outputDirAbs, 0755)
|
|
|
|
// 打印配置信息
|
|
printBanner(pythonPathAbs, uploadDirAbs, outputDirAbs)
|
|
|
|
// 检查 Python 是否存在
|
|
if _, err := os.Stat(pythonPathAbs); os.IsNotExist(err) {
|
|
fmt.Printf("⚠️ 警告: Python 路径不存在: %s\n", pythonPathAbs)
|
|
fmt.Println(" 请在配置文件中指定正确的 Python 路径")
|
|
}
|
|
|
|
// 创建路由
|
|
r := gin.Default()
|
|
|
|
// 注册路由
|
|
setupRoutes(r, outputDirAbs, pythonPathAbs)
|
|
|
|
// 启动服务
|
|
printAPIInfo()
|
|
r.Run(":" + config.Global.Port)
|
|
}
|
|
|
|
// setupRoutes 设置路由
|
|
func setupRoutes(r *gin.Engine, outputDirAbs, pythonPathAbs string) {
|
|
// 健康检查
|
|
r.GET("/health", func(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "ok",
|
|
"python_path": pythonPathAbs,
|
|
})
|
|
})
|
|
|
|
// API 路由
|
|
api := r.Group("/api")
|
|
{
|
|
api.POST("/upload", handler.Upload)
|
|
api.GET("/review", handler.Review)
|
|
}
|
|
|
|
// 静态文件下载
|
|
r.Static("/download", outputDirAbs)
|
|
}
|
|
|
|
// printBanner 打印启动横幅
|
|
func printBanner(pythonPath, uploadDir, outputDir string) {
|
|
fmt.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
|
|
fmt.Println("📦 BillAI 账单分析服务")
|
|
fmt.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
|
|
fmt.Printf("📁 项目根目录: %s\n", config.Global.ProjectRoot)
|
|
fmt.Printf("🐍 Python路径: %s\n", pythonPath)
|
|
fmt.Printf("📂 上传目录: %s\n", uploadDir)
|
|
fmt.Printf("📂 输出目录: %s\n", outputDir)
|
|
fmt.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
|
|
}
|
|
|
|
// printAPIInfo 打印 API 信息
|
|
func printAPIInfo() {
|
|
fmt.Printf("\n🚀 服务已启动: http://localhost:%s\n", config.Global.Port)
|
|
fmt.Println("📝 API 接口:")
|
|
fmt.Println(" POST /api/upload - 上传并分析账单")
|
|
fmt.Println(" GET /api/review - 获取需要复核的记录")
|
|
fmt.Println(" GET /download/* - 下载结果文件")
|
|
fmt.Println(" GET /health - 健康检查")
|
|
fmt.Println()
|
|
}
|