package api import ( "health-ai/internal/api/handler" "health-ai/internal/api/middleware" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" ) func SetupRouter(mode string) *gin.Engine { gin.SetMode(mode) r := gin.Default() // 跨域配置 r.Use(cors.New(cors.Config{ AllowOrigins: []string{"*"}, AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, AllowHeaders: []string{"Origin", "Content-Type", "Authorization"}, AllowCredentials: true, })) // 健康检查 r.GET("/health", func(c *gin.Context) { c.JSON(200, gin.H{"status": "ok"}) }) // API 路由组 apiGroup := r.Group("/api") { // ===================== // 认证路由(无需登录) // ===================== authHandler := handler.NewAuthHandler() authGroup := apiGroup.Group("/auth") { authGroup.POST("/register", authHandler.Register) authGroup.POST("/login", authHandler.Login) authGroup.POST("/refresh", authHandler.RefreshToken) authGroup.POST("/send-code", authHandler.SendCode) } // ===================== // 产品路由(部分无需登录) // ===================== productHandler := handler.NewProductHandler() productGroup := apiGroup.Group("/products") { productGroup.GET("", productHandler.GetProducts) productGroup.GET("/:id", productHandler.GetProduct) productGroup.GET("/category", productHandler.GetProductsByCategory) productGroup.GET("/search", productHandler.SearchProducts) } // 商城同步接口(无需用户登录,需要API密钥验证) apiGroup.POST("/sync/purchase", productHandler.SyncPurchase) // ===================== // 需要登录的路由 // ===================== authRequired := apiGroup.Group("") authRequired.Use(middleware.AuthRequired()) { // ===================== // 用户相关 // ===================== authRequired.GET("/user/profile", authHandler.GetUserInfo) authRequired.PUT("/user/profile", authHandler.UpdateProfile) // 健康档案 healthHandler := handler.NewHealthHandler() authRequired.GET("/user/health-profile", healthHandler.GetHealthProfile) authRequired.PUT("/user/health-profile", healthHandler.UpdateHealthProfile) authRequired.GET("/user/basic-profile", healthHandler.GetBasicProfile) authRequired.GET("/user/lifestyle", healthHandler.GetLifestyle) authRequired.PUT("/user/lifestyle", healthHandler.UpdateLifestyle) authRequired.GET("/user/medical-history", healthHandler.GetMedicalHistory) authRequired.DELETE("/user/medical-history/:id", healthHandler.DeleteMedicalHistory) authRequired.GET("/user/family-history", healthHandler.GetFamilyHistory) authRequired.DELETE("/user/family-history/:id", healthHandler.DeleteFamilyHistory) authRequired.GET("/user/allergy-records", healthHandler.GetAllergyRecords) authRequired.DELETE("/user/allergy-records/:id", healthHandler.DeleteAllergyRecord) // 购买历史 authRequired.GET("/user/purchase-history", productHandler.GetPurchaseHistory) // 产品推荐(需要登录获取体质) authRequired.GET("/products/recommend", productHandler.GetRecommendedProducts) // ===================== // 健康调查路由 // ===================== surveyHandler := handler.NewSurveyHandler() surveyGroup := authRequired.Group("/survey") { surveyGroup.GET("/status", surveyHandler.GetStatus) surveyGroup.POST("/basic-info", surveyHandler.SubmitBasicInfo) surveyGroup.POST("/lifestyle", surveyHandler.SubmitLifestyle) surveyGroup.POST("/medical-history", surveyHandler.SubmitMedicalHistory) surveyGroup.POST("/medical-history/batch", surveyHandler.SubmitBatchMedicalHistory) surveyGroup.POST("/family-history", surveyHandler.SubmitFamilyHistory) surveyGroup.POST("/family-history/batch", surveyHandler.SubmitBatchFamilyHistory) surveyGroup.POST("/allergy", surveyHandler.SubmitAllergy) surveyGroup.POST("/allergy/batch", surveyHandler.SubmitBatchAllergy) surveyGroup.POST("/complete", surveyHandler.CompleteSurvey) } // ===================== // 体质辨识路由 // ===================== constitutionHandler := handler.NewConstitutionHandler() constitutionGroup := authRequired.Group("/constitution") { constitutionGroup.GET("/questions", constitutionHandler.GetQuestions) constitutionGroup.GET("/questions/grouped", constitutionHandler.GetQuestionsGrouped) constitutionGroup.POST("/submit", constitutionHandler.SubmitAssessment) constitutionGroup.GET("/result", constitutionHandler.GetResult) constitutionGroup.GET("/history", constitutionHandler.GetHistory) constitutionGroup.GET("/recommendations", constitutionHandler.GetRecommendations) } // ===================== // AI对话路由 // ===================== conversationHandler := handler.NewConversationHandler() convGroup := authRequired.Group("/conversations") { convGroup.GET("", conversationHandler.GetConversations) convGroup.POST("", conversationHandler.CreateConversation) convGroup.GET("/:id", conversationHandler.GetConversation) convGroup.DELETE("/:id", conversationHandler.DeleteConversation) convGroup.POST("/:id/messages", conversationHandler.SendMessage) convGroup.POST("/:id/messages/stream", conversationHandler.SendMessageStream) } } } return r }