- Go 工程:cmd/usernode CLI(serve/migrate/admin create/reset-password/user otp)
+ internal/{config,model,service,system,auth,cron,api,router,server,pkg,webui}
- 配置:TOML + 环境变量覆盖(USERNODE_<SEC>_<FIELD>),config.example.toml
- 数据:GORM 双驱动(SQLite/MySQL)8 表模型,migrate 子命令可跑通
- 系统层:system.Manager 接口(useradd/userdel/passwd 白名单,dev dry-run)
- HTTP:Gin 路由骨架(/api/v1 + 501 占位),healthz,slog 结构化日志,优雅启停
- 前端:Vite + Vue3 + TS + Element Plus 最小可运行(web/),go:embed 打通
- 构建:deploy/Containerfile 多阶段单二进制镜像,web/Containerfile.dev 前端 dev
镜像,Makefile(build/test/dev/web-build/image);go.mod 锁定 go 1.22
- 验证:go build/vet/test 通过;podman 镜像构建运行 healthz+embed 通过
58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
// 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})
|
|
}
|