mirror of
https://github.com/usual2970/certimate.git
synced 2025-06-09 05:59:50 +00:00
refactor: clean code
This commit is contained in:
parent
86133ba52b
commit
416f5e0986
@ -58,7 +58,7 @@ type Certificate struct {
|
||||
|
||||
type ApplyOption struct {
|
||||
Email string `json:"email"`
|
||||
Domain string `json:"subjectAltNames"`
|
||||
SubjectAltNames string `json:"subjectAltNames"`
|
||||
AccessConfig string `json:"accessConfig"`
|
||||
KeyAlgorithm string `json:"keyAlgorithm"`
|
||||
Nameservers string `json:"nameservers"`
|
||||
@ -144,7 +144,7 @@ func Get(record *models.Record) (Applicant, error) {
|
||||
|
||||
option := &ApplyOption{
|
||||
Email: applyConfig.Email,
|
||||
Domain: record.GetString("domain"),
|
||||
SubjectAltNames: record.GetString("domain"),
|
||||
AccessConfig: access.GetString("config"),
|
||||
KeyAlgorithm: applyConfig.KeyAlgorithm,
|
||||
Nameservers: applyConfig.Nameservers,
|
||||
@ -166,7 +166,7 @@ func GetWithApplyNode(node *domain.WorkflowNode) (Applicant, error) {
|
||||
|
||||
applyConfig := &ApplyOption{
|
||||
Email: node.GetConfigString("email"),
|
||||
Domain: node.GetConfigString("domain"),
|
||||
SubjectAltNames: node.GetConfigString("domain"),
|
||||
AccessConfig: access.Config,
|
||||
KeyAlgorithm: node.GetConfigString("keyAlgorithm"),
|
||||
Nameservers: node.GetConfigString("nameservers"),
|
||||
@ -276,7 +276,7 @@ func apply(option *ApplyOption, provider challenge.Provider) (*Certificate, erro
|
||||
myUser.Registration = reg
|
||||
}
|
||||
|
||||
domains := strings.Split(option.Domain, ";")
|
||||
domains := strings.Split(option.SubjectAltNames, ";")
|
||||
request := certificate.ObtainRequest{
|
||||
Domains: domains,
|
||||
Bundle: true,
|
||||
|
@ -18,7 +18,7 @@ const (
|
||||
)
|
||||
|
||||
type CertificateRepository interface {
|
||||
GetExpireSoon(ctx context.Context) ([]domain.Certificate, error)
|
||||
ListExpireSoon(ctx context.Context) ([]domain.Certificate, error)
|
||||
}
|
||||
|
||||
type certificateService struct {
|
||||
@ -35,7 +35,7 @@ func (s *certificateService) InitSchedule(ctx context.Context) error {
|
||||
scheduler := app.GetScheduler()
|
||||
|
||||
err := scheduler.Add("certificate", "0 0 * * *", func() {
|
||||
certs, err := s.repo.GetExpireSoon(context.Background())
|
||||
certs, err := s.repo.ListExpireSoon(context.Background())
|
||||
if err != nil {
|
||||
app.GetApp().Logger().Error("failed to get expire soon certificate", "err", err)
|
||||
return
|
||||
@ -61,14 +61,14 @@ func buildMsg(records []domain.Certificate) *domain.NotifyMessage {
|
||||
}
|
||||
|
||||
// 查询模板信息
|
||||
settingRepo := repository.NewSettingRepository()
|
||||
settingRepo := repository.NewSettingsRepository()
|
||||
setting, err := settingRepo.GetByName(context.Background(), "notifyTemplates")
|
||||
|
||||
subject := defaultExpireSubject
|
||||
message := defaultExpireMessage
|
||||
|
||||
if err == nil {
|
||||
var templates *domain.NotifyTemplates
|
||||
var templates *domain.NotifyTemplatesSettingsContent
|
||||
|
||||
json.Unmarshal([]byte(setting.Content), &templates)
|
||||
|
||||
|
@ -11,10 +11,24 @@ type Settings struct {
|
||||
Content string `json:"content" db:"content"`
|
||||
}
|
||||
|
||||
type NotifyChannelsConfig map[string]map[string]any
|
||||
type NotifyTemplatesSettingsContent struct {
|
||||
NotifyTemplates []NotifyTemplate `json:"notifyTemplates"`
|
||||
}
|
||||
|
||||
func (s *Settings) GetChannelContent(channel string) (map[string]any, error) {
|
||||
conf := &NotifyChannelsConfig{}
|
||||
type NotifyTemplate struct {
|
||||
Subject string `json:"subject"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type NotifyChannelsSettingsContent map[string]map[string]any
|
||||
|
||||
type NotifyMessage struct {
|
||||
Subject string `json:"subject"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func (s *Settings) GetNotifyChannelConfig(channel string) (map[string]any, error) {
|
||||
conf := &NotifyChannelsSettingsContent{}
|
||||
if err := json.Unmarshal([]byte(s.Content), conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -26,17 +40,3 @@ func (s *Settings) GetChannelContent(channel string) (map[string]any, error) {
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
type NotifyTemplates struct {
|
||||
NotifyTemplates []NotifyTemplate `json:"notifyTemplates"`
|
||||
}
|
||||
|
||||
type NotifyTemplate struct {
|
||||
Subject string `json:"subject"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type NotifyMessage struct {
|
||||
Subject string `json:"subject"`
|
||||
Message string `json:"message"`
|
||||
}
|
@ -12,15 +12,15 @@ const (
|
||||
notifyTestBody = "欢迎使用 Certimate ,这是一条测试通知。"
|
||||
)
|
||||
|
||||
type SettingRepository interface {
|
||||
type SettingsRepository interface {
|
||||
GetByName(ctx context.Context, name string) (*domain.Settings, error)
|
||||
}
|
||||
|
||||
type NotifyService struct {
|
||||
settingRepo SettingRepository
|
||||
settingRepo SettingsRepository
|
||||
}
|
||||
|
||||
func NewNotifyService(settingRepo SettingRepository) *NotifyService {
|
||||
func NewNotifyService(settingRepo SettingsRepository) *NotifyService {
|
||||
return &NotifyService{
|
||||
settingRepo: settingRepo,
|
||||
}
|
||||
@ -32,7 +32,7 @@ func (n *NotifyService) Test(ctx context.Context, req *domain.NotifyTestPushReq)
|
||||
return fmt.Errorf("failed to get notify channels settings: %w", err)
|
||||
}
|
||||
|
||||
channelConfig, err := setting.GetChannelContent(req.Channel)
|
||||
channelConfig, err := setting.GetNotifyChannelConfig(req.Channel)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get notify channel \"%s\" config: %w", req.Channel, err)
|
||||
}
|
||||
|
@ -31,8 +31,8 @@ func (a *AccessRepository) GetById(ctx context.Context, id string) (*domain.Acce
|
||||
UpdatedAt: record.GetTime("updated"),
|
||||
},
|
||||
Name: record.GetString("name"),
|
||||
Config: record.GetString("config"),
|
||||
ConfigType: record.GetString("configType"),
|
||||
Config: record.GetString("config"),
|
||||
Usage: record.GetString("usage"),
|
||||
}
|
||||
return rs, nil
|
||||
|
@ -13,7 +13,7 @@ func NewCertificateRepository() *CertificateRepository {
|
||||
return &CertificateRepository{}
|
||||
}
|
||||
|
||||
func (c *CertificateRepository) GetExpireSoon(ctx context.Context) ([]domain.Certificate, error) {
|
||||
func (c *CertificateRepository) ListExpireSoon(ctx context.Context) ([]domain.Certificate, error) {
|
||||
rs := []domain.Certificate{}
|
||||
if err := app.GetApp().Dao().DB().
|
||||
NewQuery("select * from certificate where expireAt > datetime('now') and expireAt < datetime('now', '+20 days')").
|
||||
|
@ -8,13 +8,13 @@ import (
|
||||
"github.com/usual2970/certimate/internal/domain"
|
||||
)
|
||||
|
||||
type SettingRepository struct{}
|
||||
type SettingsRepository struct{}
|
||||
|
||||
func NewSettingRepository() *SettingRepository {
|
||||
return &SettingRepository{}
|
||||
func NewSettingsRepository() *SettingsRepository {
|
||||
return &SettingsRepository{}
|
||||
}
|
||||
|
||||
func (s *SettingRepository) GetByName(ctx context.Context, name string) (*domain.Settings, error) {
|
||||
func (s *SettingsRepository) GetByName(ctx context.Context, name string) (*domain.Settings, error) {
|
||||
resp, err := app.GetApp().Dao().FindFirstRecordByFilter("settings", "name={:name}", dbx.Params{"name": name})
|
||||
if err != nil {
|
||||
return nil, err
|
@ -13,21 +13,21 @@ func NewStatisticsRepository() *StatisticsRepository {
|
||||
return &StatisticsRepository{}
|
||||
}
|
||||
|
||||
type totalResp struct {
|
||||
Total int `json:"total" db:"total"`
|
||||
}
|
||||
|
||||
func (r *StatisticsRepository) Get(ctx context.Context) (*domain.Statistics, error) {
|
||||
rs := &domain.Statistics{}
|
||||
// 所有证书
|
||||
certTotal := totalResp{}
|
||||
certTotal := struct {
|
||||
Total int `db:"total"`
|
||||
}{}
|
||||
if err := app.GetApp().Dao().DB().NewQuery("select count(*) as total from certificate").One(&certTotal); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rs.CertificateTotal = certTotal.Total
|
||||
|
||||
// 即将过期证书
|
||||
certExpireSoonTotal := totalResp{}
|
||||
certExpireSoonTotal := struct {
|
||||
Total int `db:"total"`
|
||||
}{}
|
||||
if err := app.GetApp().Dao().DB().
|
||||
NewQuery("select count(*) as total from certificate where expireAt > datetime('now') and expireAt < datetime('now', '+20 days')").
|
||||
One(&certExpireSoonTotal); err != nil {
|
||||
@ -36,7 +36,9 @@ func (r *StatisticsRepository) Get(ctx context.Context) (*domain.Statistics, err
|
||||
rs.CertificateExpireSoon = certExpireSoonTotal.Total
|
||||
|
||||
// 已过期证书
|
||||
certExpiredTotal := totalResp{}
|
||||
certExpiredTotal := struct {
|
||||
Total int `db:"total"`
|
||||
}{}
|
||||
if err := app.GetApp().Dao().DB().
|
||||
NewQuery("select count(*) as total from certificate where expireAt < datetime('now')").
|
||||
One(&certExpiredTotal); err != nil {
|
||||
@ -45,14 +47,18 @@ func (r *StatisticsRepository) Get(ctx context.Context) (*domain.Statistics, err
|
||||
rs.CertificateExpired = certExpiredTotal.Total
|
||||
|
||||
// 所有工作流
|
||||
workflowTotal := totalResp{}
|
||||
workflowTotal := struct {
|
||||
Total int `db:"total"`
|
||||
}{}
|
||||
if err := app.GetApp().Dao().DB().NewQuery("select count(*) as total from workflow").One(&workflowTotal); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rs.WorkflowTotal = workflowTotal.Total
|
||||
|
||||
// 已启用工作流
|
||||
workflowEnabledTotal := totalResp{}
|
||||
workflowEnabledTotal := struct {
|
||||
Total int `db:"total"`
|
||||
}{}
|
||||
if err := app.GetApp().Dao().DB().NewQuery("select count(*) as total from workflow where enabled is TRUE").One(&workflowEnabledTotal); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ import (
|
||||
func Register(e *echo.Echo) {
|
||||
group := e.Group("/api", apis.RequireAdminAuth())
|
||||
|
||||
notifyRepo := repository.NewSettingRepository()
|
||||
notifyRepo := repository.NewSettingsRepository()
|
||||
notifySvc := notify.NewNotifyService(notifyRepo)
|
||||
|
||||
workflowRepo := repository.NewWorkflowRepository()
|
||||
|
@ -21,7 +21,7 @@ func NewNotifyNode(node *domain.WorkflowNode) *notifyNode {
|
||||
return ¬ifyNode{
|
||||
node: node,
|
||||
Logger: NewLogger(node),
|
||||
settingRepo: repository.NewSettingRepository(),
|
||||
settingRepo: repository.NewSettingsRepository(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,7 +35,7 @@ func (n *notifyNode) Run(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
channelConfig, err := setting.GetChannelContent(n.node.GetConfigString("channel"))
|
||||
channelConfig, err := setting.GetNotifyChannelConfig(n.node.GetConfigString("channel"))
|
||||
if err != nil {
|
||||
n.AddOutput(ctx, n.node.Name, "获取通知渠道配置失败", err.Error())
|
||||
return err
|
||||
|
@ -34,7 +34,7 @@ const CertificateDetailDrawer = ({ data, loading, trigger, ...props }: Certifica
|
||||
open={open}
|
||||
loading={loading}
|
||||
placement="right"
|
||||
title={data?.id}
|
||||
title={`certimate-${data?.id}`}
|
||||
width={640}
|
||||
onClose={() => setOpen(false)}
|
||||
>
|
||||
|
@ -30,7 +30,7 @@ const WorkflowRunDetailDrawer = ({ data, loading, trigger, ...props }: WorkflowR
|
||||
<>
|
||||
{triggerDom}
|
||||
|
||||
<Drawer closable destroyOnClose open={open} loading={loading} placement="right" title={data?.id} width={640} onClose={() => setOpen(false)}>
|
||||
<Drawer closable destroyOnClose open={open} loading={loading} placement="right" title={`runlog-${data?.id}`} width={640} onClose={() => setOpen(false)}>
|
||||
<Show when={!!data}>
|
||||
<Show when={data!.succeed}>
|
||||
<Alert showIcon type="success" message={t("workflow_run.props.status.succeeded")} />
|
||||
|
Loading…
x
Reference in New Issue
Block a user