From 0869eaafdd2f9c9abf76c5e58d3ef2db40456b9d Mon Sep 17 00:00:00 2001
From: Fu Diwei <fudiwei@sina.com>
Date: Fri, 17 Jan 2025 18:01:47 +0800
Subject: [PATCH] refactor: clean code

---
 internal/certificate/service.go               |  6 ++--
 .../{acme_accounts.go => acme_account.go}     |  0
 internal/notify/service.go                    | 10 +++---
 internal/repository/workflow.go               |  1 +
 internal/rest/notify.go                       | 13 ++++----
 internal/rest/statistics.go                   | 13 ++++----
 internal/rest/workflow.go                     | 16 +++++-----
 internal/routes/routes.go                     | 31 +++++++------------
 .../workflow/node-processor/notify_node.go    |  2 +-
 internal/workflow/node-processor/processor.go |  2 +-
 internal/workflow/service.go                  | 12 +++----
 main.go                                       |  1 +
 12 files changed, 50 insertions(+), 57 deletions(-)
 rename internal/domain/{acme_accounts.go => acme_account.go} (100%)

diff --git a/internal/certificate/service.go b/internal/certificate/service.go
index af46c202..83c97c83 100644
--- a/internal/certificate/service.go
+++ b/internal/certificate/service.go
@@ -70,11 +70,11 @@ func buildExpireSoonNotification(certificates []*domain.Certificate) *struct {
 	message := defaultExpireMessage
 
 	// 查询模板信息
-	settingRepo := repository.NewSettingsRepository()
-	setting, err := settingRepo.GetByName(context.Background(), "notifyTemplates")
+	settingsRepo := repository.NewSettingsRepository()
+	settings, err := settingsRepo.GetByName(context.Background(), "notifyTemplates")
 	if err == nil {
 		var templates *domain.NotifyTemplatesSettingsContent
-		json.Unmarshal([]byte(setting.Content), &templates)
+		json.Unmarshal([]byte(settings.Content), &templates)
 
 		if templates != nil && len(templates.NotifyTemplates) > 0 {
 			subject = templates.NotifyTemplates[0].Subject
diff --git a/internal/domain/acme_accounts.go b/internal/domain/acme_account.go
similarity index 100%
rename from internal/domain/acme_accounts.go
rename to internal/domain/acme_account.go
diff --git a/internal/notify/service.go b/internal/notify/service.go
index 0e414950..a99d9620 100644
--- a/internal/notify/service.go
+++ b/internal/notify/service.go
@@ -17,22 +17,22 @@ type settingsRepository interface {
 }
 
 type NotifyService struct {
-	settingRepo settingsRepository
+	settingsRepo settingsRepository
 }
 
-func NewNotifyService(settingRepo settingsRepository) *NotifyService {
+func NewNotifyService(settingsRepo settingsRepository) *NotifyService {
 	return &NotifyService{
-		settingRepo: settingRepo,
+		settingsRepo: settingsRepo,
 	}
 }
 
 func (n *NotifyService) Test(ctx context.Context, req *domain.NotifyTestPushReq) error {
-	setting, err := n.settingRepo.GetByName(ctx, "notifyChannels")
+	settings, err := n.settingsRepo.GetByName(ctx, "notifyChannels")
 	if err != nil {
 		return fmt.Errorf("failed to get notify channels settings: %w", err)
 	}
 
-	channelConfig, err := setting.GetNotifyChannelConfig(req.Channel)
+	channelConfig, err := settings.GetNotifyChannelConfig(req.Channel)
 	if err != nil {
 		return fmt.Errorf("failed to get notify channel \"%s\" config: %w", req.Channel, err)
 	}
diff --git a/internal/repository/workflow.go b/internal/repository/workflow.go
index d8a1980f..c4f13ba1 100644
--- a/internal/repository/workflow.go
+++ b/internal/repository/workflow.go
@@ -61,6 +61,7 @@ func (r *WorkflowRepository) Save(ctx context.Context, workflow *domain.Workflow
 	if err != nil {
 		return err
 	}
+
 	var record *models.Record
 	if workflow.Id == "" {
 		record = models.NewRecord(collection)
diff --git a/internal/rest/notify.go b/internal/rest/notify.go
index 2eb3db7b..33a8d461 100644
--- a/internal/rest/notify.go
+++ b/internal/rest/notify.go
@@ -9,25 +9,24 @@ import (
 	"github.com/labstack/echo/v5"
 )
 
-type NotifyService interface {
+type notifyService interface {
 	Test(ctx context.Context, req *domain.NotifyTestPushReq) error
 }
 
-type notifyHandler struct {
-	service NotifyService
+type NotifyHandler struct {
+	service notifyService
 }
 
-func NewNotifyHandler(route *echo.Group, service NotifyService) {
-	handler := &notifyHandler{
+func NewNotifyHandler(route *echo.Group, service notifyService) {
+	handler := &NotifyHandler{
 		service: service,
 	}
 
 	group := route.Group("/notify")
-
 	group.POST("/test", handler.test)
 }
 
-func (handler *notifyHandler) test(c echo.Context) error {
+func (handler *NotifyHandler) test(c echo.Context) error {
 	req := &domain.NotifyTestPushReq{}
 	if err := c.Bind(req); err != nil {
 		return resp.Err(c, err)
diff --git a/internal/rest/statistics.go b/internal/rest/statistics.go
index e3129c50..6db35be4 100644
--- a/internal/rest/statistics.go
+++ b/internal/rest/statistics.go
@@ -8,25 +8,24 @@ import (
 	"github.com/usual2970/certimate/internal/rest/resp"
 )
 
-type StatisticsService interface {
+type statisticsService interface {
 	Get(ctx context.Context) (*domain.Statistics, error)
 }
 
-type statisticsHandler struct {
-	service StatisticsService
+type StatisticsHandler struct {
+	service statisticsService
 }
 
-func NewStatisticsHandler(route *echo.Group, service StatisticsService) {
-	handler := &statisticsHandler{
+func NewStatisticsHandler(route *echo.Group, service statisticsService) {
+	handler := &StatisticsHandler{
 		service: service,
 	}
 
 	group := route.Group("/statistics")
-
 	group.GET("/get", handler.get)
 }
 
-func (handler *statisticsHandler) get(c echo.Context) error {
+func (handler *StatisticsHandler) get(c echo.Context) error {
 	if statistics, err := handler.service.Get(c.Request().Context()); err != nil {
 		return resp.Err(c, err)
 	} else {
diff --git a/internal/rest/workflow.go b/internal/rest/workflow.go
index 8881ee42..2bdf4120 100644
--- a/internal/rest/workflow.go
+++ b/internal/rest/workflow.go
@@ -8,26 +8,25 @@ import (
 	"github.com/usual2970/certimate/internal/rest/resp"
 )
 
-type WorkflowService interface {
+type workflowService interface {
 	Run(ctx context.Context, req *domain.WorkflowRunReq) error
-	Stop()
+	Stop(ctx context.Context)
 }
 
-type workflowHandler struct {
-	service WorkflowService
+type WorkflowHandler struct {
+	service workflowService
 }
 
-func NewWorkflowHandler(route *echo.Group, service WorkflowService) {
-	handler := &workflowHandler{
+func NewWorkflowHandler(route *echo.Group, service workflowService) {
+	handler := &WorkflowHandler{
 		service: service,
 	}
 
 	group := route.Group("/workflow")
-
 	group.POST("/run", handler.run)
 }
 
-func (handler *workflowHandler) run(c echo.Context) error {
+func (handler *WorkflowHandler) run(c echo.Context) error {
 	req := &domain.WorkflowRunReq{}
 	if err := c.Bind(req); err != nil {
 		return resp.Err(c, err)
@@ -36,5 +35,6 @@ func (handler *workflowHandler) run(c echo.Context) error {
 	if err := handler.service.Run(c.Request().Context(), req); err != nil {
 		return resp.Err(c, err)
 	}
+
 	return resp.Ok(c, nil)
 }
diff --git a/internal/routes/routes.go b/internal/routes/routes.go
index 8fab1a15..b85b30a1 100644
--- a/internal/routes/routes.go
+++ b/internal/routes/routes.go
@@ -1,7 +1,7 @@
 package routes
 
 import (
-	"sync"
+	"context"
 
 	"github.com/usual2970/certimate/internal/notify"
 	"github.com/usual2970/certimate/internal/repository"
@@ -14,36 +14,29 @@ import (
 )
 
 var (
-	workflowSvc     rest.WorkflowService
-	workflowSvcOnce sync.Once
+	notifySvc     *notify.NotifyService
+	workflowSvc   *workflow.WorkflowService
+	statisticsSvc *statistics.StatisticsService
 )
 
-func getWorkflowService() rest.WorkflowService {
-	workflowSvcOnce.Do(func() {
-		workflowRepo := repository.NewWorkflowRepository()
-		workflowSvc = workflow.NewWorkflowService(workflowRepo)
-	})
-	return workflowSvc
-}
-
 func Register(e *echo.Echo) {
-	group := e.Group("/api", apis.RequireAdminAuth())
-
 	notifyRepo := repository.NewSettingsRepository()
-	notifySvc := notify.NewNotifyService(notifyRepo)
+	notifySvc = notify.NewNotifyService(notifyRepo)
 
-	workflowSvc := getWorkflowService()
+	workflowRepo := repository.NewWorkflowRepository()
+	workflowSvc = workflow.NewWorkflowService(workflowRepo)
 
 	statisticsRepo := repository.NewStatisticsRepository()
-	statisticsSvc := statistics.NewStatisticsService(statisticsRepo)
+	statisticsSvc = statistics.NewStatisticsService(statisticsRepo)
 
+	group := e.Group("/api", apis.RequireAdminAuth())
 	rest.NewWorkflowHandler(group, workflowSvc)
-
 	rest.NewNotifyHandler(group, notifySvc)
-
 	rest.NewStatisticsHandler(group, statisticsSvc)
 }
 
 func Unregister() {
-	getWorkflowService().Stop()
+	if workflowSvc != nil {
+		workflowSvc.Stop(context.Background())
+	}
 }
diff --git a/internal/workflow/node-processor/notify_node.go b/internal/workflow/node-processor/notify_node.go
index cbc7fa47..8b175cc0 100644
--- a/internal/workflow/node-processor/notify_node.go
+++ b/internal/workflow/node-processor/notify_node.go
@@ -10,7 +10,7 @@ import (
 
 type notifyNode struct {
 	node         *domain.WorkflowNode
-	settingsRepo settingRepository
+	settingsRepo settingsRepository
 	*nodeLogger
 }
 
diff --git a/internal/workflow/node-processor/processor.go b/internal/workflow/node-processor/processor.go
index 128022ac..de16e93a 100644
--- a/internal/workflow/node-processor/processor.go
+++ b/internal/workflow/node-processor/processor.go
@@ -70,6 +70,6 @@ type workflowOutputRepository interface {
 	Save(ctx context.Context, output *domain.WorkflowOutput, certificate *domain.Certificate, cb func(id string) error) error
 }
 
-type settingRepository interface {
+type settingsRepository interface {
 	GetByName(ctx context.Context, name string) (*domain.Settings, error)
 }
diff --git a/internal/workflow/service.go b/internal/workflow/service.go
index 93b4c51a..f0cc8475 100644
--- a/internal/workflow/service.go
+++ b/internal/workflow/service.go
@@ -23,7 +23,7 @@ type workflowRepository interface {
 	ListEnabledAuto(ctx context.Context) ([]*domain.Workflow, error)
 	GetById(ctx context.Context, id string) (*domain.Workflow, error)
 	Save(ctx context.Context, workflow *domain.Workflow) error
-	SaveRun(ctx context.Context, run *domain.WorkflowRun) error
+	SaveRun(ctx context.Context, workflowRun *domain.WorkflowRun) error
 }
 
 type WorkflowService struct {
@@ -91,11 +91,11 @@ func (s *WorkflowService) InitSchedule(ctx context.Context) error {
 	return nil
 }
 
-func (s *WorkflowService) Run(ctx context.Context, options *domain.WorkflowRunReq) error {
+func (s *WorkflowService) Run(ctx context.Context, req *domain.WorkflowRunReq) error {
 	// 查询
-	workflow, err := s.repo.GetById(ctx, options.WorkflowId)
+	workflow, err := s.repo.GetById(ctx, req.WorkflowId)
 	if err != nil {
-		app.GetLogger().Error("failed to get workflow", "id", options.WorkflowId, "err", err)
+		app.GetLogger().Error("failed to get workflow", "id", req.WorkflowId, "err", err)
 		return err
 	}
 
@@ -114,7 +114,7 @@ func (s *WorkflowService) Run(ctx context.Context, options *domain.WorkflowRunRe
 
 	s.ch <- &workflowRunData{
 		Workflow: workflow,
-		Options:  options,
+		Options:  req,
 	}
 
 	return nil
@@ -165,7 +165,7 @@ func (s *WorkflowService) run(ctx context.Context, runData *workflowRunData) err
 	return nil
 }
 
-func (s *WorkflowService) Stop() {
+func (s *WorkflowService) Stop(ctx context.Context) {
 	s.cancel()
 	s.wg.Wait()
 }
diff --git a/main.go b/main.go
index 2db632d6..4bcb025a 100644
--- a/main.go
+++ b/main.go
@@ -43,6 +43,7 @@ func main() {
 		workflow.Register()
 
 		routes.Register(e.Router)
+
 		e.Router.GET(
 			"/*",
 			echo.StaticDirectoryHandler(ui.DistDirFS, false),