Files
ez-api/internal/api/status_handler.go
zenfun 33838b1e2c feat(api): wrap JSON responses in envelope
Add response envelope middleware to standardize JSON responses as
`{code,data,message}` with consistent business codes across endpoints.
Update Swagger annotations and tests to reflect the new response shape.

BREAKING CHANGE: API responses are now wrapped in a response envelope; clients must read payloads from `data` and handle `code`/`message` fields.
2026-01-10 00:15:08 +08:00

82 lines
2.3 KiB
Go

package api
import (
"net/http"
"time"
"github.com/ez-api/ez-api/internal/service"
"github.com/gin-gonic/gin"
)
// Version is set at build time using ldflags
// e.g., go build -ldflags "-X github.com/ez-api/ez-api/internal/api.Version=v1.0.0"
var Version = "v0.1.0-dev"
// startTime is used to calculate uptime
var startTime = time.Now()
// StatusHandler handles public status endpoints
type StatusHandler struct {
healthService *service.HealthCheckService
}
// NewStatusHandler creates a new StatusHandler
func NewStatusHandler(healthService *service.HealthCheckService) *StatusHandler {
return &StatusHandler{
healthService: healthService,
}
}
// StatusResponse represents the response for /status endpoint
type StatusResponse struct {
Status string `json:"status" example:"ok"`
Uptime string `json:"uptime" example:"72h30m15s"`
Version string `json:"version" example:"0.1.0"`
}
// AboutResponse represents the response for /about endpoint
type AboutResponse struct {
Name string `json:"name" example:"EZ-API Gateway"`
Version string `json:"version" example:"0.1.0"`
Description string `json:"description" example:"High-performance LLM API gateway"`
Repository string `json:"repository" example:"https://github.com/ez-api/ez-api"`
}
// Status godoc
// @Summary Get system status
// @Description Returns public runtime status information without sensitive data
// @Tags Public
// @Produce json
// @Success 200 {object} ResponseEnvelope{data=StatusResponse}
// @Router /status [get]
func (h *StatusHandler) Status(c *gin.Context) {
// Check health status
health := h.healthService.Check(c.Request.Context())
resp := StatusResponse{
Status: health.Status,
Uptime: time.Since(startTime).Round(time.Second).String(),
Version: Version,
}
c.JSON(http.StatusOK, resp)
}
// About godoc
// @Summary Get system information
// @Description Returns system metadata for display on an about page
// @Tags Public
// @Produce json
// @Success 200 {object} ResponseEnvelope{data=AboutResponse}
// @Router /about [get]
func (h *StatusHandler) About(c *gin.Context) {
resp := AboutResponse{
Name: "EZ-API Gateway",
Version: Version,
Description: "High-performance LLM API gateway",
Repository: "https://github.com/ez-api/ez-api",
}
c.JSON(http.StatusOK, resp)
}