mirror of
https://github.com/usual2970/certimate.git
synced 2025-06-08 13:39:53 +00:00
refactor: clean code
This commit is contained in:
parent
ce4c590b1c
commit
c1f77dd92f
@ -1,4 +1,4 @@
|
||||
package applicant
|
||||
package applicant_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
@ -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
|
||||
|
@ -27,8 +27,3 @@ const (
|
||||
CertificateSourceTypeWorkflow = CertificateSourceType("workflow")
|
||||
CertificateSourceTypeUpload = CertificateSourceType("upload")
|
||||
)
|
||||
|
||||
type CertificateArchiveFileReq struct {
|
||||
CertificateId string `json:"-"`
|
||||
Format string `json:"format"`
|
||||
}
|
||||
|
6
internal/domain/dtos/certificate.go
Normal file
6
internal/domain/dtos/certificate.go
Normal file
@ -0,0 +1,6 @@
|
||||
package dtos
|
||||
|
||||
type CertificateArchiveFileReq struct {
|
||||
CertificateId string `json:"-"`
|
||||
Format string `json:"format"`
|
||||
}
|
7
internal/domain/dtos/notify.go
Normal file
7
internal/domain/dtos/notify.go
Normal file
@ -0,0 +1,7 @@
|
||||
package dtos
|
||||
|
||||
import "github.com/usual2970/certimate/internal/domain"
|
||||
|
||||
type NotifyTestPushReq struct {
|
||||
Channel domain.NotifyChannelType `json:"channel"`
|
||||
}
|
8
internal/domain/dtos/workflow.go
Normal file
8
internal/domain/dtos/workflow.go
Normal 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"`
|
||||
}
|
@ -18,7 +18,3 @@ const (
|
||||
NotifyChannelTypeWebhook = NotifyChannelType("webhook")
|
||||
NotifyChannelTypeWeCom = NotifyChannelType("wecom")
|
||||
)
|
||||
|
||||
type NotifyTestPushReq struct {
|
||||
Channel string `json:"channel"`
|
||||
}
|
||||
|
@ -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"
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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))
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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,
|
||||
})
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user