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