refactor: clean code

This commit is contained in:
Fu Diwei 2025-01-19 03:34:38 +08:00
parent ce4c590b1c
commit c1f77dd92f
17 changed files with 50 additions and 43 deletions

View File

@ -1,4 +1,4 @@
package applicant package applicant_test
import ( import (
"testing" "testing"

View File

@ -10,6 +10,7 @@ import (
"github.com/usual2970/certimate/internal/app" "github.com/usual2970/certimate/internal/app"
"github.com/usual2970/certimate/internal/domain" "github.com/usual2970/certimate/internal/domain"
"github.com/usual2970/certimate/internal/domain/dtos"
"github.com/usual2970/certimate/internal/notify" "github.com/usual2970/certimate/internal/notify"
"github.com/usual2970/certimate/internal/pkg/utils/certs" "github.com/usual2970/certimate/internal/pkg/utils/certs"
"github.com/usual2970/certimate/internal/repository" "github.com/usual2970/certimate/internal/repository"
@ -55,7 +56,7 @@ func (s *CertificateService) InitSchedule(ctx context.Context) error {
return nil return nil
} }
func (s *CertificateService) ArchiveFile(ctx context.Context, req *domain.CertificateArchiveFileReq) ([]byte, error) { func (s *CertificateService) ArchiveFile(ctx context.Context, req *dtos.CertificateArchiveFileReq) ([]byte, error) {
certificate, err := s.repo.GetById(ctx, req.CertificateId) certificate, err := s.repo.GetById(ctx, req.CertificateId)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -27,8 +27,3 @@ const (
CertificateSourceTypeWorkflow = CertificateSourceType("workflow") CertificateSourceTypeWorkflow = CertificateSourceType("workflow")
CertificateSourceTypeUpload = CertificateSourceType("upload") CertificateSourceTypeUpload = CertificateSourceType("upload")
) )
type CertificateArchiveFileReq struct {
CertificateId string `json:"-"`
Format string `json:"format"`
}

View File

@ -0,0 +1,6 @@
package dtos
type CertificateArchiveFileReq struct {
CertificateId string `json:"-"`
Format string `json:"format"`
}

View File

@ -0,0 +1,7 @@
package dtos
import "github.com/usual2970/certimate/internal/domain"
type NotifyTestPushReq struct {
Channel domain.NotifyChannelType `json:"channel"`
}

View File

@ -0,0 +1,8 @@
package dtos
import "github.com/usual2970/certimate/internal/domain"
type WorkflowRunReq struct {
WorkflowId string `json:"-"`
Trigger domain.WorkflowTriggerType `json:"trigger"`
}

View File

@ -18,7 +18,3 @@ const (
NotifyChannelTypeWebhook = NotifyChannelType("webhook") NotifyChannelTypeWebhook = NotifyChannelType("webhook")
NotifyChannelTypeWeCom = NotifyChannelType("wecom") NotifyChannelTypeWeCom = NotifyChannelType("wecom")
) )
type NotifyTestPushReq struct {
Channel string `json:"channel"`
}

View File

@ -149,13 +149,4 @@ type WorkflowNodeIOValueSelector struct {
Name string `json:"name"` Name string `json:"name"`
} }
type WorkflowNodeIONameType = string const WorkflowNodeIONameCertificate string = "certificate"
const (
WorkflowNodeIONameCertificate WorkflowNodeIONameType = "certificate"
)
type WorkflowRunReq struct {
WorkflowId string `json:"-"`
Trigger WorkflowTriggerType `json:"trigger"`
}

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/usual2970/certimate/internal/domain" "github.com/usual2970/certimate/internal/domain"
"github.com/usual2970/certimate/internal/domain/dtos"
) )
const ( const (
@ -26,16 +27,16 @@ func NewNotifyService(settingsRepo settingsRepository) *NotifyService {
} }
} }
func (n *NotifyService) Test(ctx context.Context, req *domain.NotifyTestPushReq) error { func (n *NotifyService) Test(ctx context.Context, req *dtos.NotifyTestPushReq) error {
settings, err := n.settingsRepo.GetByName(ctx, "notifyChannels") settings, err := n.settingsRepo.GetByName(ctx, "notifyChannels")
if err != nil { if err != nil {
return fmt.Errorf("failed to get notify channels settings: %w", err) return fmt.Errorf("failed to get notify channels settings: %w", err)
} }
channelConfig, err := settings.GetNotifyChannelConfig(req.Channel) channelConfig, err := settings.GetNotifyChannelConfig(string(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)
} }
return SendToChannel(notifyTestTitle, notifyTestBody, req.Channel, channelConfig) return SendToChannel(notifyTestTitle, notifyTestBody, string(req.Channel), channelConfig)
} }

View File

@ -97,6 +97,7 @@ func (r *WorkflowRepository) SaveRun(ctx context.Context, workflowRun *domain.Wo
err = app.GetApp().RunInTransaction(func(txApp core.App) error { err = app.GetApp().RunInTransaction(func(txApp core.App) error {
workflowRunRecord := core.NewRecord(collection) workflowRunRecord := core.NewRecord(collection)
workflowRunRecord.Id = workflowRun.Id
workflowRunRecord.Set("workflowId", workflowRun.WorkflowId) workflowRunRecord.Set("workflowId", workflowRun.WorkflowId)
workflowRunRecord.Set("trigger", string(workflowRun.Trigger)) workflowRunRecord.Set("trigger", string(workflowRun.Trigger))
workflowRunRecord.Set("status", string(workflowRun.Status)) workflowRunRecord.Set("status", string(workflowRun.Status))

View File

@ -6,12 +6,12 @@ import (
"github.com/pocketbase/pocketbase/core" "github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/router" "github.com/pocketbase/pocketbase/tools/router"
"github.com/usual2970/certimate/internal/domain" "github.com/usual2970/certimate/internal/domain/dtos"
"github.com/usual2970/certimate/internal/rest/resp" "github.com/usual2970/certimate/internal/rest/resp"
) )
type certificateService interface { type certificateService interface {
ArchiveFile(ctx context.Context, req *domain.CertificateArchiveFileReq) ([]byte, error) ArchiveFile(ctx context.Context, req *dtos.CertificateArchiveFileReq) ([]byte, error)
} }
type CertificateHandler struct { type CertificateHandler struct {
@ -28,7 +28,7 @@ func NewCertificateHandler(router *router.RouterGroup[*core.RequestEvent], servi
} }
func (handler *CertificateHandler) run(e *core.RequestEvent) error { func (handler *CertificateHandler) run(e *core.RequestEvent) error {
req := &domain.CertificateArchiveFileReq{} req := &dtos.CertificateArchiveFileReq{}
req.CertificateId = e.Request.PathValue("id") req.CertificateId = e.Request.PathValue("id")
if err := e.BindBody(req); err != nil { if err := e.BindBody(req); err != nil {
return resp.Err(e, err) return resp.Err(e, err)

View File

@ -6,12 +6,12 @@ import (
"github.com/pocketbase/pocketbase/core" "github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/router" "github.com/pocketbase/pocketbase/tools/router"
"github.com/usual2970/certimate/internal/domain" "github.com/usual2970/certimate/internal/domain/dtos"
"github.com/usual2970/certimate/internal/rest/resp" "github.com/usual2970/certimate/internal/rest/resp"
) )
type notifyService interface { type notifyService interface {
Test(ctx context.Context, req *domain.NotifyTestPushReq) error Test(ctx context.Context, req *dtos.NotifyTestPushReq) error
} }
type NotifyHandler struct { type NotifyHandler struct {
@ -28,7 +28,7 @@ func NewNotifyHandler(router *router.RouterGroup[*core.RequestEvent], service no
} }
func (handler *NotifyHandler) test(e *core.RequestEvent) error { func (handler *NotifyHandler) test(e *core.RequestEvent) error {
req := &domain.NotifyTestPushReq{} req := &dtos.NotifyTestPushReq{}
if err := e.BindBody(req); err != nil { if err := e.BindBody(req); err != nil {
return resp.Err(e, err) return resp.Err(e, err)
} }

View File

@ -6,12 +6,12 @@ import (
"github.com/pocketbase/pocketbase/core" "github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/router" "github.com/pocketbase/pocketbase/tools/router"
"github.com/usual2970/certimate/internal/domain" "github.com/usual2970/certimate/internal/domain/dtos"
"github.com/usual2970/certimate/internal/rest/resp" "github.com/usual2970/certimate/internal/rest/resp"
) )
type workflowService interface { type workflowService interface {
Run(ctx context.Context, req *domain.WorkflowRunReq) error Run(ctx context.Context, req *dtos.WorkflowRunReq) error
Stop(ctx context.Context) Stop(ctx context.Context)
} }
@ -29,7 +29,7 @@ func NewWorkflowHandler(router *router.RouterGroup[*core.RequestEvent], service
} }
func (handler *WorkflowHandler) run(e *core.RequestEvent) error { func (handler *WorkflowHandler) run(e *core.RequestEvent) error {
req := &domain.WorkflowRunReq{} req := &dtos.WorkflowRunReq{}
req.WorkflowId = e.Request.PathValue("id") req.WorkflowId = e.Request.PathValue("id")
if err := e.BindBody(req); err != nil { if err := e.BindBody(req); err != nil {
return resp.Err(e, err) return resp.Err(e, err)

View File

@ -8,6 +8,7 @@ import (
"github.com/usual2970/certimate/internal/app" "github.com/usual2970/certimate/internal/app"
"github.com/usual2970/certimate/internal/domain" "github.com/usual2970/certimate/internal/domain"
"github.com/usual2970/certimate/internal/domain/dtos"
"github.com/usual2970/certimate/internal/repository" "github.com/usual2970/certimate/internal/repository"
) )
@ -64,7 +65,7 @@ func onWorkflowRecordCreateOrUpdate(ctx context.Context, record *core.Record) er
// 反之,重新添加定时任务 // 反之,重新添加定时任务
err := scheduler.Add(fmt.Sprintf("workflow#%s", workflowId), record.GetString("triggerCron"), func() { err := scheduler.Add(fmt.Sprintf("workflow#%s", workflowId), record.GetString("triggerCron"), func() {
NewWorkflowService(repository.NewWorkflowRepository()).Run(ctx, &domain.WorkflowRunReq{ NewWorkflowService(repository.NewWorkflowRepository()).Run(ctx, &dtos.WorkflowRunReq{
WorkflowId: workflowId, WorkflowId: workflowId,
Trigger: domain.WorkflowTriggerTypeAuto, Trigger: domain.WorkflowTriggerTypeAuto,
}) })

View File

@ -9,6 +9,7 @@ import (
"github.com/usual2970/certimate/internal/app" "github.com/usual2970/certimate/internal/app"
"github.com/usual2970/certimate/internal/domain" "github.com/usual2970/certimate/internal/domain"
"github.com/usual2970/certimate/internal/domain/dtos"
processor "github.com/usual2970/certimate/internal/workflow/processor" processor "github.com/usual2970/certimate/internal/workflow/processor"
) )
@ -16,7 +17,7 @@ const defaultRoutines = 10
type workflowRunData struct { type workflowRunData struct {
Workflow *domain.Workflow Workflow *domain.Workflow
Options *domain.WorkflowRunReq RunTrigger domain.WorkflowTriggerType
} }
type workflowRepository interface { type workflowRepository interface {
@ -74,7 +75,7 @@ func (s *WorkflowService) InitSchedule(ctx context.Context) error {
scheduler := app.GetScheduler() scheduler := app.GetScheduler()
for _, workflow := range workflows { for _, workflow := range workflows {
err := scheduler.Add(fmt.Sprintf("workflow#%s", workflow.Id), workflow.TriggerCron, func() { err := scheduler.Add(fmt.Sprintf("workflow#%s", workflow.Id), workflow.TriggerCron, func() {
s.Run(ctx, &domain.WorkflowRunReq{ s.Run(ctx, &dtos.WorkflowRunReq{
WorkflowId: workflow.Id, WorkflowId: workflow.Id,
Trigger: domain.WorkflowTriggerTypeAuto, Trigger: domain.WorkflowTriggerTypeAuto,
}) })
@ -88,7 +89,7 @@ func (s *WorkflowService) InitSchedule(ctx context.Context) error {
return nil return nil
} }
func (s *WorkflowService) Run(ctx context.Context, req *domain.WorkflowRunReq) error { func (s *WorkflowService) Run(ctx context.Context, req *dtos.WorkflowRunReq) error {
// 查询 // 查询
workflow, err := s.repo.GetById(ctx, req.WorkflowId) workflow, err := s.repo.GetById(ctx, req.WorkflowId)
if err != nil { if err != nil {
@ -111,7 +112,7 @@ func (s *WorkflowService) Run(ctx context.Context, req *domain.WorkflowRunReq) e
s.ch <- &workflowRunData{ s.ch <- &workflowRunData{
Workflow: workflow, Workflow: workflow,
Options: req, RunTrigger: req.Trigger,
} }
return nil return nil
@ -120,15 +121,14 @@ func (s *WorkflowService) Run(ctx context.Context, req *domain.WorkflowRunReq) e
func (s *WorkflowService) run(ctx context.Context, runData *workflowRunData) error { func (s *WorkflowService) run(ctx context.Context, runData *workflowRunData) error {
// 执行 // 执行
workflow := runData.Workflow workflow := runData.Workflow
options := runData.Options
run := &domain.WorkflowRun{ run := &domain.WorkflowRun{
WorkflowId: workflow.Id, WorkflowId: workflow.Id,
Status: domain.WorkflowRunStatusTypeRunning, Status: domain.WorkflowRunStatusTypeRunning,
Trigger: options.Trigger, Trigger: runData.RunTrigger,
StartedAt: time.Now(), StartedAt: time.Now(),
EndedAt: time.Now(), EndedAt: time.Now(),
} }
processor := processor.NewWorkflowProcessor(workflow) processor := processor.NewWorkflowProcessor(workflow)
if err := processor.Run(ctx); err != nil { if err := processor.Run(ctx); err != nil {
run.Status = domain.WorkflowRunStatusTypeFailed run.Status = domain.WorkflowRunStatusTypeFailed