diff --git a/internal/applicant/acme-ca.go b/internal/applicant/acme_ca.go similarity index 100% rename from internal/applicant/acme-ca.go rename to internal/applicant/acme_ca.go diff --git a/internal/applicant/acme-user.go b/internal/applicant/acme_user.go similarity index 100% rename from internal/applicant/acme-user.go rename to internal/applicant/acme_user.go diff --git a/internal/applicant/applicant_test.go b/internal/applicant/applicant_test.go index 352cea24..98a3e38e 100644 --- a/internal/applicant/applicant_test.go +++ b/internal/applicant/applicant_test.go @@ -1,4 +1,4 @@ -package applicant +package applicant_test import ( "testing" diff --git a/internal/certificate/service.go b/internal/certificate/service.go index 0c518388..1e9dd462 100644 --- a/internal/certificate/service.go +++ b/internal/certificate/service.go @@ -10,6 +10,7 @@ import ( "github.com/usual2970/certimate/internal/app" "github.com/usual2970/certimate/internal/domain" + "github.com/usual2970/certimate/internal/domain/dtos" "github.com/usual2970/certimate/internal/notify" "github.com/usual2970/certimate/internal/pkg/utils/certs" "github.com/usual2970/certimate/internal/repository" @@ -55,7 +56,7 @@ func (s *CertificateService) InitSchedule(ctx context.Context) error { 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) if err != nil { return nil, err diff --git a/internal/domain/certificate.go b/internal/domain/certificate.go index 16fd9435..f0e03711 100644 --- a/internal/domain/certificate.go +++ b/internal/domain/certificate.go @@ -27,8 +27,3 @@ const ( CertificateSourceTypeWorkflow = CertificateSourceType("workflow") CertificateSourceTypeUpload = CertificateSourceType("upload") ) - -type CertificateArchiveFileReq struct { - CertificateId string `json:"-"` - Format string `json:"format"` -} diff --git a/internal/domain/dtos/certificate.go b/internal/domain/dtos/certificate.go new file mode 100644 index 00000000..3fe13f16 --- /dev/null +++ b/internal/domain/dtos/certificate.go @@ -0,0 +1,6 @@ +package dtos + +type CertificateArchiveFileReq struct { + CertificateId string `json:"-"` + Format string `json:"format"` +} diff --git a/internal/domain/dtos/notify.go b/internal/domain/dtos/notify.go new file mode 100644 index 00000000..72c6cc79 --- /dev/null +++ b/internal/domain/dtos/notify.go @@ -0,0 +1,7 @@ +package dtos + +import "github.com/usual2970/certimate/internal/domain" + +type NotifyTestPushReq struct { + Channel domain.NotifyChannelType `json:"channel"` +} diff --git a/internal/domain/dtos/workflow.go b/internal/domain/dtos/workflow.go new file mode 100644 index 00000000..3a760971 --- /dev/null +++ b/internal/domain/dtos/workflow.go @@ -0,0 +1,8 @@ +package dtos + +import "github.com/usual2970/certimate/internal/domain" + +type WorkflowRunReq struct { + WorkflowId string `json:"-"` + Trigger domain.WorkflowTriggerType `json:"trigger"` +} diff --git a/internal/domain/notify.go b/internal/domain/notify.go index 7db79be6..3d71a3a7 100644 --- a/internal/domain/notify.go +++ b/internal/domain/notify.go @@ -18,7 +18,3 @@ const ( NotifyChannelTypeWebhook = NotifyChannelType("webhook") NotifyChannelTypeWeCom = NotifyChannelType("wecom") ) - -type NotifyTestPushReq struct { - Channel string `json:"channel"` -} diff --git a/internal/domain/workflow.go b/internal/domain/workflow.go index 86e9a781..e2454e36 100644 --- a/internal/domain/workflow.go +++ b/internal/domain/workflow.go @@ -149,13 +149,4 @@ type WorkflowNodeIOValueSelector struct { Name string `json:"name"` } -type WorkflowNodeIONameType = string - -const ( - WorkflowNodeIONameCertificate WorkflowNodeIONameType = "certificate" -) - -type WorkflowRunReq struct { - WorkflowId string `json:"-"` - Trigger WorkflowTriggerType `json:"trigger"` -} +const WorkflowNodeIONameCertificate string = "certificate" diff --git a/internal/notify/service.go b/internal/notify/service.go index a99d9620..9b7cc416 100644 --- a/internal/notify/service.go +++ b/internal/notify/service.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/usual2970/certimate/internal/domain" + "github.com/usual2970/certimate/internal/domain/dtos" ) 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") if err != nil { 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 { 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) } diff --git a/internal/repository/workflow.go b/internal/repository/workflow.go index 221a916e..ac9b3e9f 100644 --- a/internal/repository/workflow.go +++ b/internal/repository/workflow.go @@ -97,6 +97,7 @@ func (r *WorkflowRepository) SaveRun(ctx context.Context, workflowRun *domain.Wo err = app.GetApp().RunInTransaction(func(txApp core.App) error { workflowRunRecord := core.NewRecord(collection) + workflowRunRecord.Id = workflowRun.Id workflowRunRecord.Set("workflowId", workflowRun.WorkflowId) workflowRunRecord.Set("trigger", string(workflowRun.Trigger)) workflowRunRecord.Set("status", string(workflowRun.Status)) diff --git a/internal/rest/handlers/certificate.go b/internal/rest/handlers/certificate.go index 730302d5..26788de6 100644 --- a/internal/rest/handlers/certificate.go +++ b/internal/rest/handlers/certificate.go @@ -6,12 +6,12 @@ import ( "github.com/pocketbase/pocketbase/core" "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" ) type certificateService interface { - ArchiveFile(ctx context.Context, req *domain.CertificateArchiveFileReq) ([]byte, error) + ArchiveFile(ctx context.Context, req *dtos.CertificateArchiveFileReq) ([]byte, error) } type CertificateHandler struct { @@ -28,7 +28,7 @@ func NewCertificateHandler(router *router.RouterGroup[*core.RequestEvent], servi } func (handler *CertificateHandler) run(e *core.RequestEvent) error { - req := &domain.CertificateArchiveFileReq{} + req := &dtos.CertificateArchiveFileReq{} req.CertificateId = e.Request.PathValue("id") if err := e.BindBody(req); err != nil { return resp.Err(e, err) diff --git a/internal/rest/handlers/notify.go b/internal/rest/handlers/notify.go index ad7c650f..eed8b8cc 100644 --- a/internal/rest/handlers/notify.go +++ b/internal/rest/handlers/notify.go @@ -6,12 +6,12 @@ import ( "github.com/pocketbase/pocketbase/core" "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" ) type notifyService interface { - Test(ctx context.Context, req *domain.NotifyTestPushReq) error + Test(ctx context.Context, req *dtos.NotifyTestPushReq) error } type NotifyHandler struct { @@ -28,7 +28,7 @@ func NewNotifyHandler(router *router.RouterGroup[*core.RequestEvent], service no } func (handler *NotifyHandler) test(e *core.RequestEvent) error { - req := &domain.NotifyTestPushReq{} + req := &dtos.NotifyTestPushReq{} if err := e.BindBody(req); err != nil { return resp.Err(e, err) } diff --git a/internal/rest/handlers/workflow.go b/internal/rest/handlers/workflow.go index bb3da629..b1ba70e4 100644 --- a/internal/rest/handlers/workflow.go +++ b/internal/rest/handlers/workflow.go @@ -6,12 +6,12 @@ import ( "github.com/pocketbase/pocketbase/core" "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" ) type workflowService interface { - Run(ctx context.Context, req *domain.WorkflowRunReq) error + Run(ctx context.Context, req *dtos.WorkflowRunReq) error Stop(ctx context.Context) } @@ -29,7 +29,7 @@ func NewWorkflowHandler(router *router.RouterGroup[*core.RequestEvent], service } func (handler *WorkflowHandler) run(e *core.RequestEvent) error { - req := &domain.WorkflowRunReq{} + req := &dtos.WorkflowRunReq{} req.WorkflowId = e.Request.PathValue("id") if err := e.BindBody(req); err != nil { return resp.Err(e, err) diff --git a/internal/workflow/event.go b/internal/workflow/event.go index ad417501..9bb3b310 100644 --- a/internal/workflow/event.go +++ b/internal/workflow/event.go @@ -8,6 +8,7 @@ import ( "github.com/usual2970/certimate/internal/app" "github.com/usual2970/certimate/internal/domain" + "github.com/usual2970/certimate/internal/domain/dtos" "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() { - NewWorkflowService(repository.NewWorkflowRepository()).Run(ctx, &domain.WorkflowRunReq{ + NewWorkflowService(repository.NewWorkflowRepository()).Run(ctx, &dtos.WorkflowRunReq{ WorkflowId: workflowId, Trigger: domain.WorkflowTriggerTypeAuto, }) diff --git a/internal/workflow/service.go b/internal/workflow/service.go index 29077745..62f8a888 100644 --- a/internal/workflow/service.go +++ b/internal/workflow/service.go @@ -9,14 +9,15 @@ import ( "github.com/usual2970/certimate/internal/app" "github.com/usual2970/certimate/internal/domain" + "github.com/usual2970/certimate/internal/domain/dtos" processor "github.com/usual2970/certimate/internal/workflow/processor" ) const defaultRoutines = 10 type workflowRunData struct { - Workflow *domain.Workflow - Options *domain.WorkflowRunReq + Workflow *domain.Workflow + RunTrigger domain.WorkflowTriggerType } type workflowRepository interface { @@ -74,7 +75,7 @@ func (s *WorkflowService) InitSchedule(ctx context.Context) error { scheduler := app.GetScheduler() for _, workflow := range workflows { 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, Trigger: domain.WorkflowTriggerTypeAuto, }) @@ -88,7 +89,7 @@ func (s *WorkflowService) InitSchedule(ctx context.Context) error { 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) if err != nil { @@ -110,8 +111,8 @@ func (s *WorkflowService) Run(ctx context.Context, req *domain.WorkflowRunReq) e } s.ch <- &workflowRunData{ - Workflow: workflow, - Options: req, + Workflow: workflow, + RunTrigger: req.Trigger, } 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 { // 执行 workflow := runData.Workflow - options := runData.Options - run := &domain.WorkflowRun{ WorkflowId: workflow.Id, Status: domain.WorkflowRunStatusTypeRunning, - Trigger: options.Trigger, + Trigger: runData.RunTrigger, StartedAt: time.Now(), EndedAt: time.Now(), } + processor := processor.NewWorkflowProcessor(workflow) if err := processor.Run(ctx); err != nil { run.Status = domain.WorkflowRunStatusTypeFailed