mirror of
https://github.com/tnb-labs/panel.git
synced 2026-10-31 03:28:06 +08:00
依赖注入:
- 移除 google/wire(含 wire.go/wire_gen.go 与全部 ProviderSet),
改用 samber/do v2 单注入器 + 双入口惰性构建
- 贡献模型替代命令式注册:路由 routes:、命令 commands:、
任务 jobs: 前缀经 internal/registry 收集与校验
- 构造函数统一 func NewXxx(i do.Injector) (T, error)
三层架构:
- 补全用例层:每个 biz.XxxRepo 配 XxxUsecase,
service/command/job 表现层只依赖用例,不再直接引用仓储
路由与文档:
- route/http.go 按域拆为声明式 Endpoint 贡献,
Endpoint 承载登录白名单与端点限流语义
- 调试模式下提供 OpenAPI 3.1 文档:/openapi.json 与 /docs(Scalar),
从 validate 标签生成
目录与依赖:
- internal/http/{middleware,request,rule} 拍平至 internal/*
- CLI 命令拆至 internal/command
- 日志轮转 timberjack 换为 libtnb/logrotate
- 升级 validator/cron/sessions/gormstore/sqlite/securecookie
- 数据库迁移保持 gormigrate 不变
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
45 lines
3.9 KiB
Go
45 lines
3.9 KiB
Go
package route
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/samber/do/v2"
|
|
|
|
"github.com/acepanel/panel/v3/internal/biz"
|
|
"github.com/acepanel/panel/v3/internal/request"
|
|
"github.com/acepanel/panel/v3/internal/service"
|
|
)
|
|
|
|
// UserRoutes 用户与通行密钥认证、用户管理路由
|
|
func UserRoutes(i do.Injector) (Endpoints, error) {
|
|
svc := do.MustInvoke[*service.UserService](i)
|
|
passkey := do.MustInvoke[*service.UserPasskeyService](i)
|
|
|
|
return Endpoints{
|
|
// 认证
|
|
{Method: http.MethodGet, Path: "/api/user/key", Handler: svc.GetKey, Summary: "获取登录公钥", Tags: []string{"用户"}, Public: true},
|
|
{Method: http.MethodGet, Path: "/api/user/captcha", Handler: svc.GetCaptcha, Summary: "获取登录验证码", Tags: []string{"用户"}, Public: true},
|
|
{Method: http.MethodPost, Path: "/api/user/login", Handler: svc.Login, Summary: "登录", Tags: []string{"用户"}, Request: request.UserLogin{}, Public: true, Throttle: &ThrottleRule{Tokens: 5, Interval: time.Minute}},
|
|
{Method: http.MethodPost, Path: "/api/user/logout", Handler: svc.Logout, Summary: "登出", Tags: []string{"用户"}, Public: true},
|
|
{Method: http.MethodGet, Path: "/api/user/is_login", Handler: svc.IsLogin, Summary: "是否已登录", Tags: []string{"用户"}, Public: true},
|
|
{Method: http.MethodGet, Path: "/api/user/is_2fa", Handler: svc.IsTwoFA, Summary: "是否开启两步验证", Tags: []string{"用户"}, Request: request.UserIsTwoFA{}, Public: true},
|
|
{Method: http.MethodGet, Path: "/api/user/info", Handler: svc.Info, Summary: "获取当前用户信息", Tags: []string{"用户"}},
|
|
// 通行密钥
|
|
{Method: http.MethodGet, Path: "/api/user/passkey/enabled", Handler: passkey.Enabled, Summary: "是否启用通行密钥", Tags: []string{"通行密钥"}, Public: true},
|
|
{Method: http.MethodPost, Path: "/api/user/passkey/register", Handler: passkey.BeginRegister, Summary: "开始注册通行密钥", Tags: []string{"通行密钥"}},
|
|
{Method: http.MethodPut, Path: "/api/user/passkey/register", Handler: passkey.FinishRegister, Summary: "完成注册通行密钥", Tags: []string{"通行密钥"}, Response: service.Envelope[biz.UserPasskey]{}},
|
|
{Method: http.MethodPost, Path: "/api/user/passkey/login", Handler: passkey.BeginLogin, Summary: "开始通行密钥登录", Tags: []string{"通行密钥"}, Public: true, Throttle: &ThrottleRule{Tokens: 5, Interval: time.Minute}},
|
|
{Method: http.MethodPut, Path: "/api/user/passkey/login", Handler: passkey.FinishLogin, Summary: "完成通行密钥登录", Tags: []string{"通行密钥"}, Public: true},
|
|
// 用户管理
|
|
{Method: http.MethodGet, Path: "/api/users", Handler: svc.List, Summary: "获取用户列表", Tags: []string{"用户"}, Request: request.Paginate{}, Response: service.Envelope[service.Page[*biz.User]]{}},
|
|
{Method: http.MethodPost, Path: "/api/users", Handler: svc.Create, Summary: "创建用户", Tags: []string{"用户"}, Request: request.UserCreate{}, Response: service.Envelope[biz.User]{}},
|
|
{Method: http.MethodPost, Path: "/api/users/{id}/username", Handler: svc.UpdateUsername, Summary: "修改用户名", Tags: []string{"用户"}, Request: request.UserUpdateUsername{}},
|
|
{Method: http.MethodPost, Path: "/api/users/{id}/password", Handler: svc.UpdatePassword, Summary: "修改密码", Tags: []string{"用户"}, Request: request.UserUpdatePassword{}},
|
|
{Method: http.MethodPost, Path: "/api/users/{id}/email", Handler: svc.UpdateEmail, Summary: "修改邮箱", Tags: []string{"用户"}, Request: request.UserUpdateEmail{}},
|
|
{Method: http.MethodGet, Path: "/api/users/{id}/2fa", Handler: svc.GenerateTwoFA, Summary: "生成两步验证密钥", Tags: []string{"用户"}, Request: request.UserID{}},
|
|
{Method: http.MethodPost, Path: "/api/users/{id}/2fa", Handler: svc.UpdateTwoFA, Summary: "更新两步验证", Tags: []string{"用户"}, Request: request.UserUpdateTwoFA{}},
|
|
{Method: http.MethodDelete, Path: "/api/users/{id}", Handler: svc.Delete, Summary: "删除用户", Tags: []string{"用户"}, Request: request.UserID{}},
|
|
}, nil
|
|
}
|