修改api包

This commit is contained in:
2025-11-05 16:10:46 +08:00
parent aaa2f1b22f
commit 4cae93ef34
4 changed files with 40 additions and 36 deletions

View File

@@ -5,6 +5,7 @@ import (
"net/http/pprof"
"git.huangwc.com/pig/pig-farm-controller/internal/app/middleware"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"github.com/labstack/echo/v4"
echoSwagger "github.com/swaggo/echo-swagger"
)
@@ -12,7 +13,7 @@ import (
// setupRoutes 设置所有 API 路由
// 在此方法中,使用已初始化的控制器实例将其路由注册到 Echo 引擎中。
func (a *API) setupRoutes() {
a.logger.Info("开始初始化所有 API 路由")
logger := logs.TraceLogger(a.Ctx, a.Ctx, "SetupRoutes")
// --- Public Routes ---
// 这些路由不需要身份验证
@@ -20,7 +21,7 @@ func (a *API) setupRoutes() {
// 用户注册和登录
a.echo.POST("/api/v1/users", a.userController.CreateUser) // 注册新用户
a.echo.POST("/api/v1/users/login", a.userController.Login) // 用户登录
a.logger.Debug("公开接口注册成功:用户注册、登录")
logger.Debug("公开接口注册成功:用户注册、登录")
// 注册 pprof 路由
pprofGroup := a.echo.Group("/debug/pprof")
@@ -38,15 +39,15 @@ func (a *API) setupRoutes() {
pprofGroup.GET("/mutex", echo.WrapHandler(pprof.Handler("mutex"))) // pprof 互斥锁
pprofGroup.GET("/threadcreate", echo.WrapHandler(pprof.Handler("threadcreate")))
}
a.logger.Debug("pprof 接口注册成功")
logger.Debug("pprof 接口注册成功")
// 上行事件监听路由
a.echo.POST("/upstream", echo.WrapHandler(a.listenHandler.Handler())) // 处理设备上行事件
a.logger.Debug("上行事件监听接口注册成功")
logger.Debug("上行事件监听接口注册成功")
// 添加 Swagger UI 路由, Swagger UI可在 /swagger/index.html 上找到
a.echo.GET("/swagger/*any", echoSwagger.WrapHandler) // Swagger UI 接口
a.logger.Debug("Swagger UI 接口注册成功")
logger.Debug("Swagger UI 接口注册成功")
// --- Authenticated Routes ---
// 所有在此注册的路由都需要通过 JWT 身份验证
@@ -59,7 +60,7 @@ func (a *API) setupRoutes() {
{
userGroup.POST("/:id/notifications/test", a.userController.SendTestNotification)
}
a.logger.Debug("用户相关接口注册成功 (需要认证和审计)")
logger.Debug("用户相关接口注册成功 (需要认证和审计)")
// 设备相关路由组
deviceGroup := authGroup.Group("/devices")
@@ -71,7 +72,7 @@ func (a *API) setupRoutes() {
deviceGroup.DELETE("/:id", a.deviceController.DeleteDevice) // 删除设备
deviceGroup.POST("/manual-control/:id", a.deviceController.ManualControl) // 手动控制设备
}
a.logger.Debug("设备相关接口注册成功 (需要认证和审计)")
logger.Debug("设备相关接口注册成功 (需要认证和审计)")
// 区域主控相关路由组
areaControllerGroup := authGroup.Group("/area-controllers")
@@ -82,7 +83,7 @@ func (a *API) setupRoutes() {
areaControllerGroup.PUT("/:id", a.deviceController.UpdateAreaController) // 更新区域主控
areaControllerGroup.DELETE("/:id", a.deviceController.DeleteAreaController) // 删除区域主控
}
a.logger.Debug("区域主控相关接口注册成功 (需要认证和审计)")
logger.Debug("区域主控相关接口注册成功 (需要认证和审计)")
// 设备模板相关路由组
deviceTemplateGroup := authGroup.Group("/device-templates")
@@ -93,7 +94,7 @@ func (a *API) setupRoutes() {
deviceTemplateGroup.PUT("/:id", a.deviceController.UpdateDeviceTemplate) // 更新设备模板
deviceTemplateGroup.DELETE("/:id", a.deviceController.DeleteDeviceTemplate) // 删除设备模板
}
a.logger.Debug("设备模板相关接口注册成功 (需要认证和审计)")
logger.Debug("设备模板相关接口注册成功 (需要认证和审计)")
// 计划相关路由组
planGroup := authGroup.Group("/plans")
@@ -106,7 +107,7 @@ func (a *API) setupRoutes() {
planGroup.POST("/:id/start", a.planController.StartPlan) // 启动计划
planGroup.POST("/:id/stop", a.planController.StopPlan) // 停止计划
}
a.logger.Debug("计划相关接口注册成功 (需要认证和审计)")
logger.Debug("计划相关接口注册成功 (需要认证和审计)")
// 猪舍相关路由组
pigHouseGroup := authGroup.Group("/pig-houses")
@@ -117,7 +118,7 @@ func (a *API) setupRoutes() {
pigHouseGroup.PUT("/:id", a.pigFarmController.UpdatePigHouse) // 更新猪舍
pigHouseGroup.DELETE("/:id", a.pigFarmController.DeletePigHouse) // 删除猪舍
}
a.logger.Debug("猪舍相关接口注册成功 (需要认证和审计)")
logger.Debug("猪舍相关接口注册成功 (需要认证和审计)")
// 猪圈相关路由组
penGroup := authGroup.Group("/pens")
@@ -129,7 +130,7 @@ func (a *API) setupRoutes() {
penGroup.DELETE("/:id", a.pigFarmController.DeletePen) // 删除猪圈
penGroup.PUT("/:id/status", a.pigFarmController.UpdatePenStatus) // 更新猪圈状态
}
a.logger.Debug("猪圈相关接口注册成功 (需要认证和审计)")
logger.Debug("猪圈相关接口注册成功 (需要认证和审计)")
// 猪群相关路由组
pigBatchGroup := authGroup.Group("/pig-batches")
@@ -154,7 +155,7 @@ func (a *API) setupRoutes() {
pigBatchGroup.POST("/record-death/:id", a.pigBatchController.RecordDeath) // 记录正常猪只死亡事件
pigBatchGroup.POST("/record-cull/:id", a.pigBatchController.RecordCull) // 记录正常猪只淘汰事件
}
a.logger.Debug("猪群相关接口注册成功 (需要认证和审计)")
logger.Debug("猪群相关接口注册成功 (需要认证和审计)")
// 数据监控相关路由组
monitorGroup := authGroup.Group("/monitor")
@@ -178,8 +179,8 @@ func (a *API) setupRoutes() {
monitorGroup.GET("/pig-sales", a.monitorController.ListPigSales)
monitorGroup.GET("/notifications", a.monitorController.ListNotifications)
}
a.logger.Debug("数据监控相关接口注册成功 (需要认证和审计)")
logger.Debug("数据监控相关接口注册成功 (需要认证和审计)")
}
a.logger.Debug("所有接口注册成功")
logger.Debug("所有接口注册成功")
}