package routes import ( // "fwh-toolbox-server/controllers" _ "fwh-toolbox-server/docs" "time" "github.com/gofiber/fiber/v2" "github.com/gofiber/swagger" "github.com/google/uuid" ) const traceIdHeader string = "X-Trace-Id" type Response[T any] struct { Code int `json:"code"` Message string `json:"message"` Data T `json:"data"` TraceID string `json:"traceId"` Timestamp int64 `json:"timestamp"` } type ResponseNoData struct { Code int `json:"code"` Message string `json:"message"` TraceID string `json:"traceId"` Timestamp int64 `json:"timestamp"` } func JSONResponse[T any](c *fiber.Ctx, code int, message string, data T) error { traceId := c.Get(traceIdHeader) // 从请求头取 if traceId == "" { traceId = uuid.New().String() // 没有就生成 } // 设置到响应头,让前端也能拿到 c.Set(traceIdHeader, traceId) resp := Response[T]{ Code: code, Message: message, Data: data, TraceID: traceId, Timestamp: time.Now().UnixMilli(), } return c.JSON(resp) } func JSONResponseNoData(c *fiber.Ctx, code int, message string) error { traceId := c.Get(traceIdHeader) // 从请求头取 if traceId == "" { traceId = uuid.New().String() // 没有就生成 } // 设置到响应头,让前端也能拿到 c.Set(traceIdHeader, traceId) resp := ResponseNoData{ Code: code, Message: message, TraceID: traceId, Timestamp: time.Now().UnixMilli(), } return c.JSON(resp) } func Setup(app *fiber.App) { api := app.Group("/api") // api.Get("/keyAccounts/sunrun/state", controllers.GetUsers) // api.Get("/keyAccounts/sunrun/abnormalList", controllers.GetUsers) // … SitesMonitorSetup(api) app.Get("/swagger/*", swagger.HandlerDefault) }