package api import ( "net/http/pprof" "git.huangwc.com/pig/pig-farm-controller/internal/app/middleware" "github.com/gin-gonic/gin" swaggerFiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" ) // setupRoutes 设置所有 API 路由 // 在此方法中,使用已初始化的控制器实例将其路由注册到 Gin 引擎中。 func (a *API) setupRoutes() { // --- Public Routes --- // 这些路由不需要身份验证 // 用户注册和登录 a.engine.POST("/api/v1/users", a.userController.CreateUser) // 注册新用户 a.engine.POST("/api/v1/users/login", a.userController.Login) // 用户登录 a.logger.Info("公开接口注册成功:用户注册、登录") // 注册 pprof 路由 pprofGroup := a.engine.Group("/debug/pprof") { pprofGroup.GET("/", gin.WrapF(pprof.Index)) // pprof 索引页 pprofGroup.GET("/cmdline", gin.WrapF(pprof.Cmdline)) // pprof 命令行参数 pprofGroup.GET("/profile", gin.WrapF(pprof.Profile)) // pprof CPU profile pprofGroup.POST("/symbol", gin.WrapF(pprof.Symbol)) // pprof 符号查找 (POST) pprofGroup.GET("/symbol", gin.WrapF(pprof.Symbol)) // pprof 符号查找 (GET) pprofGroup.GET("/trace", gin.WrapF(pprof.Trace)) // pprof 跟踪 pprofGroup.GET("/allocs", gin.WrapH(pprof.Handler("allocs"))) // pprof 内存分配 pprofGroup.GET("/block", gin.WrapH(pprof.Handler("block"))) // pprof 阻塞 pprofGroup.GET("/goroutine", gin.WrapH(pprof.Handler("goroutine"))) pprofGroup.GET("/heap", gin.WrapH(pprof.Handler("heap"))) // pprof 堆内存 pprofGroup.GET("/mutex", gin.WrapH(pprof.Handler("mutex"))) // pprof 互斥锁 pprofGroup.GET("/threadcreate", gin.WrapH(pprof.Handler("threadcreate"))) } a.logger.Info("pprof 接口注册成功") // 上行事件监听路由 a.engine.POST("/upstream", gin.WrapH(a.listenHandler.Handler())) // 处理设备上行事件 a.logger.Info("上行事件监听接口注册成功") // 添加 Swagger UI 路由, Swagger UI可在 /swagger/index.html 上找到 a.engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) // Swagger UI 接口 a.logger.Info("Swagger UI 接口注册成功") // --- Authenticated Routes --- // 所有在此注册的路由都需要通过 JWT 身份验证 authGroup := a.engine.Group("/api/v1") authGroup.Use(middleware.AuthMiddleware(a.tokenService, a.userRepo)) // 1. 身份认证中间件 authGroup.Use(middleware.AuditLogMiddleware(a.auditService)) // 2. 审计日志中间件 { // 用户相关路由组 userGroup := authGroup.Group("/users") { userGroup.GET("/:id/history", a.userController.ListUserHistory) // 获取用户操作历史 } a.logger.Info("用户相关接口注册成功 (需要认证和审计)") // 设备相关路由组 deviceGroup := authGroup.Group("/devices") { deviceGroup.POST("", a.deviceController.CreateDevice) // 创建设备 deviceGroup.GET("", a.deviceController.ListDevices) // 获取设备列表 deviceGroup.GET("/:id", a.deviceController.GetDevice) // 获取单个设备 deviceGroup.PUT("/:id", a.deviceController.UpdateDevice) // 更新设备 deviceGroup.DELETE("/:id", a.deviceController.DeleteDevice) // 删除设备 deviceGroup.POST("/manual-control/:id", a.deviceController.ManualControl) // 手动控制设备 } a.logger.Info("设备相关接口注册成功 (需要认证和审计)") // 区域主控相关路由组 areaControllerGroup := authGroup.Group("/area-controllers") { areaControllerGroup.POST("", a.deviceController.CreateAreaController) // 创建区域主控 areaControllerGroup.GET("", a.deviceController.ListAreaControllers) // 获取区域主控列表 areaControllerGroup.GET("/:id", a.deviceController.GetAreaController) // 获取单个区域主控 areaControllerGroup.PUT("/:id", a.deviceController.UpdateAreaController) // 更新区域主控 areaControllerGroup.DELETE("/:id", a.deviceController.DeleteAreaController) // 删除区域主控 } a.logger.Info("区域主控相关接口注册成功 (需要认证和审计)") // 设备模板相关路由组 deviceTemplateGroup := authGroup.Group("/device-templates") { deviceTemplateGroup.POST("", a.deviceController.CreateDeviceTemplate) // 创建设备模板 deviceTemplateGroup.GET("", a.deviceController.ListDeviceTemplates) // 获取设备模板列表 deviceTemplateGroup.GET("/:id", a.deviceController.GetDeviceTemplate) // 获取单个设备模板 deviceTemplateGroup.PUT("/:id", a.deviceController.UpdateDeviceTemplate) // 更新设备模板 deviceTemplateGroup.DELETE("/:id", a.deviceController.DeleteDeviceTemplate) // 删除设备模板 } a.logger.Info("设备模板相关接口注册成功 (需要认证和审计)") // 计划相关路由组 planGroup := authGroup.Group("/plans") { planGroup.POST("", a.planController.CreatePlan) // 创建计划 planGroup.GET("", a.planController.ListPlans) // 获取计划列表 planGroup.GET("/:id", a.planController.GetPlan) // 获取单个计划 planGroup.PUT("/:id", a.planController.UpdatePlan) // 更新计划 planGroup.DELETE("/:id", a.planController.DeletePlan) // 删除计划 planGroup.POST("/:id/start", a.planController.StartPlan) // 启动计划 planGroup.POST("/:id/stop", a.planController.StopPlan) // 停止计划 } a.logger.Info("计划相关接口注册成功 (需要认证和审计)") // 猪舍相关路由组 pigHouseGroup := authGroup.Group("/pig-houses") { pigHouseGroup.POST("", a.pigFarmController.CreatePigHouse) // 创建猪舍 pigHouseGroup.GET("", a.pigFarmController.ListPigHouses) // 获取猪舍列表 pigHouseGroup.GET("/:id", a.pigFarmController.GetPigHouse) // 获取单个猪舍 pigHouseGroup.PUT("/:id", a.pigFarmController.UpdatePigHouse) // 更新猪舍 pigHouseGroup.DELETE("/:id", a.pigFarmController.DeletePigHouse) // 删除猪舍 } a.logger.Info("猪舍相关接口注册成功 (需要认证和审计)") // 猪圈相关路由组 penGroup := authGroup.Group("/pens") { penGroup.POST("", a.pigFarmController.CreatePen) // 创建猪圈 penGroup.GET("", a.pigFarmController.ListPens) // 获取猪圈列表 penGroup.GET("/:id", a.pigFarmController.GetPen) // 获取单个猪圈 penGroup.PUT("/:id", a.pigFarmController.UpdatePen) // 更新猪圈 penGroup.DELETE("/:id", a.pigFarmController.DeletePen) // 删除猪圈 penGroup.PUT("/:id/status", a.pigFarmController.UpdatePenStatus) // 更新猪圈状态 } a.logger.Info("猪圈相关接口注册成功 (需要认证和审计)") // 猪群相关路由组 pigBatchGroup := authGroup.Group("/pig-batches") { pigBatchGroup.POST("", a.pigBatchController.CreatePigBatch) // 创建猪群 pigBatchGroup.GET("", a.pigBatchController.ListPigBatches) // 获取猪群列表 pigBatchGroup.GET("/:id", a.pigBatchController.GetPigBatch) // 获取单个猪群 pigBatchGroup.PUT("/:id", a.pigBatchController.UpdatePigBatch) // 更新猪群 pigBatchGroup.DELETE("/:id", a.pigBatchController.DeletePigBatch) // 删除猪群 pigBatchGroup.POST("/assign-pens/:id", a.pigBatchController.AssignEmptyPensToBatch) // 为猪群分配空栏 pigBatchGroup.POST("/reclassify-pen/:fromBatchID", a.pigBatchController.ReclassifyPenToNewBatch) // 将猪栏划拨到新群 penGroup.DELETE("/remove-pen/:penID/:batchID", a.pigBatchController.RemoveEmptyPenFromBatch) // 从猪群移除空栏 pigBatchGroup.POST("/move-pigs-into-pen/:id", a.pigBatchController.MovePigsIntoPen) // 将猪只从“虚拟库存”移入指定猪栏 pigBatchGroup.POST("/sell-pigs/:id", a.pigBatchController.SellPigs) // 处理卖猪业务 pigBatchGroup.POST("/buy-pigs/:id", a.pigBatchController.BuyPigs) // 处理买猪业务 pigBatchGroup.POST("/transfer-across-batches/:sourceBatchID", a.pigBatchController.TransferPigsAcrossBatches) // 跨猪群调栏 pigBatchGroup.POST("/transfer-within-batch/:id", a.pigBatchController.TransferPigsWithinBatch) // 群内调栏 pigBatchGroup.POST("/record-sick-pigs/:id", a.pigBatchController.RecordSickPigs) // 记录新增病猪事件 pigBatchGroup.POST("/record-sick-pig-recovery/:id", a.pigBatchController.RecordSickPigRecovery) // 记录病猪康复事件 pigBatchGroup.POST("/record-sick-pig-death/:id", a.pigBatchController.RecordSickPigDeath) // 记录病猪死亡事件 pigBatchGroup.POST("/record-sick-pig-cull/:id", a.pigBatchController.RecordSickPigCull) // 记录病猪淘汰事件 pigBatchGroup.POST("/record-death/:id", a.pigBatchController.RecordDeath) // 记录正常猪只死亡事件 pigBatchGroup.POST("/record-cull/:id", a.pigBatchController.RecordCull) // 记录正常猪只淘汰事件 } a.logger.Info("猪群相关接口注册成功 (需要认证和审计)") // 数据监控相关路由组 monitorGroup := authGroup.Group("/monitor") { monitorGroup.GET("/sensor-data", a.monitorController.ListSensorData) monitorGroup.GET("/device-command-logs", a.monitorController.ListDeviceCommandLogs) monitorGroup.GET("/plan-execution-logs", a.monitorController.ListPlanExecutionLogs) monitorGroup.GET("/task-execution-logs", a.monitorController.ListTaskExecutionLogs) monitorGroup.GET("/pending-collections", a.monitorController.ListPendingCollections) monitorGroup.GET("/user-action-logs", a.monitorController.ListUserActionLogs) monitorGroup.GET("/raw-material-purchases", a.monitorController.ListRawMaterialPurchases) monitorGroup.GET("/raw-material-stock-logs", a.monitorController.ListRawMaterialStockLogs) monitorGroup.GET("/feed-usage-records", a.monitorController.ListFeedUsageRecords) monitorGroup.GET("/medication-logs", a.monitorController.ListMedicationLogs) monitorGroup.GET("/pig-batch-logs", a.monitorController.ListPigBatchLogs) monitorGroup.GET("/weighing-batches", a.monitorController.ListWeighingBatches) monitorGroup.GET("/weighing-records", a.monitorController.ListWeighingRecords) monitorGroup.GET("/pig-transfer-logs", a.monitorController.ListPigTransferLogs) monitorGroup.GET("/pig-sick-logs", a.monitorController.ListPigSickLogs) monitorGroup.GET("/pig-purchases", a.monitorController.ListPigPurchases) monitorGroup.GET("/pig-sales", a.monitorController.ListPigSales) } a.logger.Info("数据监控相关接口注册成功 (需要认证和审计)") } }