mirror of
https://github.com/tnb-labs/panel.git
synced 2026-09-21 13:20:10 +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>
91 lines
2.2 KiB
Go
91 lines
2.2 KiB
Go
package biz
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/libtnb/utils/crypt"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/acepanel/panel/v3/internal/app"
|
|
)
|
|
|
|
type UserToken struct {
|
|
ID uint `gorm:"primaryKey" json:"id"`
|
|
UserID uint `gorm:"index" json:"user_id"`
|
|
Token string `gorm:"not null;default:'';unique" json:"-"`
|
|
IPs []string `gorm:"not null;default:'[]';serializer:json" json:"ips"`
|
|
ExpiredAt time.Time `json:"expired_at"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
func (r *UserToken) BeforeSave(tx *gorm.DB) error {
|
|
crypter, err := crypt.NewXChacha20Poly1305([]byte(app.Key))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
r.Token, err = crypter.Encrypt([]byte(r.Token))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *UserToken) AfterFind(tx *gorm.DB) error {
|
|
crypter, err := crypt.NewXChacha20Poly1305([]byte(app.Key))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
token, err := crypter.Decrypt(r.Token)
|
|
if err == nil {
|
|
r.Token = string(token)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type UserTokenRepo interface {
|
|
List(userID, page, limit uint) ([]*UserToken, int64, error)
|
|
Create(userID uint, ips []string, expired time.Time) (*UserToken, error)
|
|
Get(id uint) (*UserToken, error)
|
|
Delete(id uint) error
|
|
Update(id uint, ips []string, expired time.Time) (*UserToken, error)
|
|
ValidateReq(req *http.Request) (uint, error)
|
|
}
|
|
|
|
type UserTokenUsecase struct {
|
|
repo UserTokenRepo
|
|
}
|
|
|
|
func NewUserTokenUsecase(repo UserTokenRepo) *UserTokenUsecase {
|
|
return &UserTokenUsecase{repo: repo}
|
|
}
|
|
|
|
func (uc *UserTokenUsecase) List(userID, page, limit uint) ([]*UserToken, int64, error) {
|
|
return uc.repo.List(userID, page, limit)
|
|
}
|
|
|
|
func (uc *UserTokenUsecase) Create(userID uint, ips []string, expired time.Time) (*UserToken, error) {
|
|
return uc.repo.Create(userID, ips, expired)
|
|
}
|
|
|
|
func (uc *UserTokenUsecase) Get(id uint) (*UserToken, error) {
|
|
return uc.repo.Get(id)
|
|
}
|
|
|
|
func (uc *UserTokenUsecase) Delete(id uint) error {
|
|
return uc.repo.Delete(id)
|
|
}
|
|
|
|
func (uc *UserTokenUsecase) Update(id uint, ips []string, expired time.Time) (*UserToken, error) {
|
|
return uc.repo.Update(id, ips, expired)
|
|
}
|
|
|
|
func (uc *UserTokenUsecase) ValidateReq(req *http.Request) (uint, error) {
|
|
return uc.repo.ValidateReq(req)
|
|
}
|