Files
panel/internal/biz/cert.go
T
耗子andClaude Opus 4.8 443516a2cf refactor!: 迁移至 samber/do 依赖注入与三层架构
依赖注入:
- 移除 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>
2026-07-09 21:54:22 +08:00

121 lines
5.0 KiB
Go

package biz
import (
"context"
"time"
mholtacme "github.com/mholt/acmez/v3/acme"
"github.com/acepanel/panel/v3/internal/request"
"github.com/acepanel/panel/v3/pkg/acme"
"github.com/acepanel/panel/v3/pkg/types"
)
type Cert struct {
ID uint `gorm:"primaryKey" json:"id"`
AccountID uint `gorm:"not null;default:0" json:"account_id"` // 关联的 ACME 账户 ID
WebsiteID uint `gorm:"not null;default:0" json:"website_id"` // 关联的网站 ID
DNSID uint `gorm:"not null;default:0" json:"dns_id"` // 关联的 DNS ID
Type string `gorm:"not null;default:''" json:"type"` // 证书类型 (P256, P384, 2048, 3072, 4096)
Domains []string `gorm:"not null;default:'[]';serializer:json" json:"domains"` // 域名
Alias map[string]string `gorm:"not null;default:'{}';serializer:json" json:"alias"` // DNS 验证别名
AutoRenewal bool `gorm:"not null;default:false" json:"auto_renewal"` // 自动续签
RenewalInfo mholtacme.RenewalInfo `gorm:"not null;default:'{}';serializer:json" json:"renewal_info"` // 续签信息
CertURL string `gorm:"not null;default:''" json:"cert_url"` // 证书 URL (续签时使用)
Cert string `gorm:"not null;default:''" json:"cert"` // 证书内容
Key string `gorm:"not null;default:''" json:"key"` // 私钥内容
Script string `gorm:"not null;default:''" json:"script"` // 部署脚本
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Website *Website `gorm:"foreignKey:WebsiteID" json:"website"`
Account *CertAccount `gorm:"foreignKey:AccountID" json:"account"`
DNS *CertDNS `gorm:"foreignKey:DNSID" json:"dns"`
}
type CertRepo interface {
List(page, limit uint) ([]*types.CertList, int64, error)
Get(id uint) (*Cert, error)
GetByWebsite(WebsiteID uint) (*Cert, error)
Upload(ctx context.Context, req *request.CertUpload) (*Cert, error)
Create(ctx context.Context, req *request.CertCreate) (*Cert, error)
Update(ctx context.Context, req *request.CertUpdate) error
Delete(ctx context.Context, id uint) error
ObtainAuto(id uint) (*acme.Certificate, error)
ObtainAutoWithProgressCallback(ctx context.Context, id uint, progressCallback func(string)) (*acme.Certificate, error)
ObtainPanel(account *CertAccount, ips []string) ([]byte, []byte, error)
ObtainSelfSigned(id uint) error
Renew(id uint) (*acme.Certificate, error)
RenewWithProgressCallback(ctx context.Context, id uint, progressCallback func(string)) (*acme.Certificate, error)
RefreshRenewalInfo(id uint) (mholtacme.RenewalInfo, error)
Deploy(ID, WebsiteID uint, enableHTTPS bool) error
}
type CertUsecase struct {
repo CertRepo
}
func NewCertUsecase(repo CertRepo) *CertUsecase {
return &CertUsecase{repo: repo}
}
func (uc *CertUsecase) List(page, limit uint) ([]*types.CertList, int64, error) {
return uc.repo.List(page, limit)
}
func (uc *CertUsecase) Get(id uint) (*Cert, error) {
return uc.repo.Get(id)
}
func (uc *CertUsecase) GetByWebsite(WebsiteID uint) (*Cert, error) {
return uc.repo.GetByWebsite(WebsiteID)
}
func (uc *CertUsecase) Upload(ctx context.Context, req *request.CertUpload) (*Cert, error) {
return uc.repo.Upload(ctx, req)
}
func (uc *CertUsecase) Create(ctx context.Context, req *request.CertCreate) (*Cert, error) {
return uc.repo.Create(ctx, req)
}
func (uc *CertUsecase) Update(ctx context.Context, req *request.CertUpdate) error {
return uc.repo.Update(ctx, req)
}
func (uc *CertUsecase) Delete(ctx context.Context, id uint) error {
return uc.repo.Delete(ctx, id)
}
func (uc *CertUsecase) ObtainAuto(id uint) (*acme.Certificate, error) {
return uc.repo.ObtainAuto(id)
}
func (uc *CertUsecase) ObtainAutoWithProgressCallback(ctx context.Context, id uint, progressCallback func(string)) (*acme.Certificate, error) {
return uc.repo.ObtainAutoWithProgressCallback(ctx, id, progressCallback)
}
func (uc *CertUsecase) ObtainPanel(account *CertAccount, ips []string) ([]byte, []byte, error) {
return uc.repo.ObtainPanel(account, ips)
}
func (uc *CertUsecase) ObtainSelfSigned(id uint) error {
return uc.repo.ObtainSelfSigned(id)
}
func (uc *CertUsecase) Renew(id uint) (*acme.Certificate, error) {
return uc.repo.Renew(id)
}
func (uc *CertUsecase) RenewWithProgressCallback(ctx context.Context, id uint, progressCallback func(string)) (*acme.Certificate, error) {
return uc.repo.RenewWithProgressCallback(ctx, id, progressCallback)
}
func (uc *CertUsecase) RefreshRenewalInfo(id uint) (mholtacme.RenewalInfo, error) {
return uc.repo.RefreshRenewalInfo(id)
}
func (uc *CertUsecase) Deploy(ID, WebsiteID uint, enableHTTPS bool) error {
return uc.repo.Deploy(ID, WebsiteID, enableHTTPS)
}