// Package api 为 HTTP handler 层(RESTful v1)。 // M0 提供健康检查与模块路由骨架;各模块 handler 在对应里程碑填充。 package api import ( "net/http" "runtime" "time" "github.com/gin-gonic/gin" "ws_usernode/internal/service" ) // Handler 聚合各模块 handler,作为路由注册的挂载点。 type Handler struct { Health *HealthHandler Admin *AdminHandler User *UserHandler // Auth / Keys / Approval / Audit / Settings 等模块在 M1~M4 填充 } // New 创建 handler 集合。M0 阶段部分服务可为 nil,路由只挂已实现模块。 func New(adminSvc *service.AdminService, userSvc *service.UserService, auditSvc *service.AuditService) *Handler { h := &Handler{ Health: &HealthHandler{startedAt: time.Now()}, Admin: &AdminHandler{svc: adminSvc}, User: &UserHandler{svc: userSvc}, } _ = auditSvc return h } // HealthHandler 健康检查。 type HealthHandler struct { startedAt time.Time } func (h *HealthHandler) Healthz(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "status": "ok", "version": "0.1.0-m0", "uptime": time.Since(h.startedAt).String(), "go": runtime.Version(), "timestamp": time.Now().UTC().Format(time.RFC3339), }) } // ok 统一成功响应。 func ok(c *gin.Context, data any) { c.JSON(http.StatusOK, gin.H{"data": data}) } // fail 统一错误响应。 func fail(c *gin.Context, status int, msg string) { c.JSON(status, gin.H{"error": msg}) }