refactor: clean code

This commit is contained in:
Fu Diwei 2025-01-17 18:01:47 +08:00
parent dab6ad917c
commit 0869eaafdd
12 changed files with 50 additions and 57 deletions

View File

@ -70,11 +70,11 @@ func buildExpireSoonNotification(certificates []*domain.Certificate) *struct {
message := defaultExpireMessage message := defaultExpireMessage
// 查询模板信息 // 查询模板信息
settingRepo := repository.NewSettingsRepository() settingsRepo := repository.NewSettingsRepository()
setting, err := settingRepo.GetByName(context.Background(), "notifyTemplates") settings, err := settingsRepo.GetByName(context.Background(), "notifyTemplates")
if err == nil { if err == nil {
var templates *domain.NotifyTemplatesSettingsContent var templates *domain.NotifyTemplatesSettingsContent
json.Unmarshal([]byte(setting.Content), &templates) json.Unmarshal([]byte(settings.Content), &templates)
if templates != nil && len(templates.NotifyTemplates) > 0 { if templates != nil && len(templates.NotifyTemplates) > 0 {
subject = templates.NotifyTemplates[0].Subject subject = templates.NotifyTemplates[0].Subject

View File

@ -17,22 +17,22 @@ type settingsRepository interface {
} }
type NotifyService struct { type NotifyService struct {
settingRepo settingsRepository settingsRepo settingsRepository
} }
func NewNotifyService(settingRepo settingsRepository) *NotifyService { func NewNotifyService(settingsRepo settingsRepository) *NotifyService {
return &NotifyService{ return &NotifyService{
settingRepo: settingRepo, settingsRepo: settingsRepo,
} }
} }
func (n *NotifyService) Test(ctx context.Context, req *domain.NotifyTestPushReq) error { 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 { 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 := setting.GetNotifyChannelConfig(req.Channel) channelConfig, err := settings.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)
} }

View File

@ -61,6 +61,7 @@ func (r *WorkflowRepository) Save(ctx context.Context, workflow *domain.Workflow
if err != nil { if err != nil {
return err return err
} }
var record *models.Record var record *models.Record
if workflow.Id == "" { if workflow.Id == "" {
record = models.NewRecord(collection) record = models.NewRecord(collection)

View File

@ -9,25 +9,24 @@ import (
"github.com/labstack/echo/v5" "github.com/labstack/echo/v5"
) )
type NotifyService interface { type notifyService interface {
Test(ctx context.Context, req *domain.NotifyTestPushReq) error Test(ctx context.Context, req *domain.NotifyTestPushReq) error
} }
type notifyHandler struct { type NotifyHandler struct {
service NotifyService service notifyService
} }
func NewNotifyHandler(route *echo.Group, service NotifyService) { func NewNotifyHandler(route *echo.Group, service notifyService) {
handler := &notifyHandler{ handler := &NotifyHandler{
service: service, service: service,
} }
group := route.Group("/notify") group := route.Group("/notify")
group.POST("/test", handler.test) group.POST("/test", handler.test)
} }
func (handler *notifyHandler) test(c echo.Context) error { func (handler *NotifyHandler) test(c echo.Context) error {
req := &domain.NotifyTestPushReq{} req := &domain.NotifyTestPushReq{}
if err := c.Bind(req); err != nil { if err := c.Bind(req); err != nil {
return resp.Err(c, err) return resp.Err(c, err)

View File

@ -8,25 +8,24 @@ import (
"github.com/usual2970/certimate/internal/rest/resp" "github.com/usual2970/certimate/internal/rest/resp"
) )
type StatisticsService interface { type statisticsService interface {
Get(ctx context.Context) (*domain.Statistics, error) Get(ctx context.Context) (*domain.Statistics, error)
} }
type statisticsHandler struct { type StatisticsHandler struct {
service StatisticsService service statisticsService
} }
func NewStatisticsHandler(route *echo.Group, service StatisticsService) { func NewStatisticsHandler(route *echo.Group, service statisticsService) {
handler := &statisticsHandler{ handler := &StatisticsHandler{
service: service, service: service,
} }
group := route.Group("/statistics") group := route.Group("/statistics")
group.GET("/get", handler.get) 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 { if statistics, err := handler.service.Get(c.Request().Context()); err != nil {
return resp.Err(c, err) return resp.Err(c, err)
} else { } else {

View File

@ -8,26 +8,25 @@ import (
"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 *domain.WorkflowRunReq) error
Stop() Stop(ctx context.Context)
} }
type workflowHandler struct { type WorkflowHandler struct {
service WorkflowService service workflowService
} }
func NewWorkflowHandler(route *echo.Group, service WorkflowService) { func NewWorkflowHandler(route *echo.Group, service workflowService) {
handler := &workflowHandler{ handler := &WorkflowHandler{
service: service, service: service,
} }
group := route.Group("/workflow") group := route.Group("/workflow")
group.POST("/run", handler.run) group.POST("/run", handler.run)
} }
func (handler *workflowHandler) run(c echo.Context) error { func (handler *WorkflowHandler) run(c echo.Context) error {
req := &domain.WorkflowRunReq{} req := &domain.WorkflowRunReq{}
if err := c.Bind(req); err != nil { if err := c.Bind(req); err != nil {
return resp.Err(c, err) 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 { if err := handler.service.Run(c.Request().Context(), req); err != nil {
return resp.Err(c, err) return resp.Err(c, err)
} }
return resp.Ok(c, nil) return resp.Ok(c, nil)
} }

View File

@ -1,7 +1,7 @@
package routes package routes
import ( import (
"sync" "context"
"github.com/usual2970/certimate/internal/notify" "github.com/usual2970/certimate/internal/notify"
"github.com/usual2970/certimate/internal/repository" "github.com/usual2970/certimate/internal/repository"
@ -14,36 +14,29 @@ import (
) )
var ( var (
workflowSvc rest.WorkflowService notifySvc *notify.NotifyService
workflowSvcOnce sync.Once 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) { func Register(e *echo.Echo) {
group := e.Group("/api", apis.RequireAdminAuth())
notifyRepo := repository.NewSettingsRepository() notifyRepo := repository.NewSettingsRepository()
notifySvc := notify.NewNotifyService(notifyRepo) notifySvc = notify.NewNotifyService(notifyRepo)
workflowSvc := getWorkflowService() workflowRepo := repository.NewWorkflowRepository()
workflowSvc = workflow.NewWorkflowService(workflowRepo)
statisticsRepo := repository.NewStatisticsRepository() statisticsRepo := repository.NewStatisticsRepository()
statisticsSvc := statistics.NewStatisticsService(statisticsRepo) statisticsSvc = statistics.NewStatisticsService(statisticsRepo)
group := e.Group("/api", apis.RequireAdminAuth())
rest.NewWorkflowHandler(group, workflowSvc) rest.NewWorkflowHandler(group, workflowSvc)
rest.NewNotifyHandler(group, notifySvc) rest.NewNotifyHandler(group, notifySvc)
rest.NewStatisticsHandler(group, statisticsSvc) rest.NewStatisticsHandler(group, statisticsSvc)
} }
func Unregister() { func Unregister() {
getWorkflowService().Stop() if workflowSvc != nil {
workflowSvc.Stop(context.Background())
}
} }

View File

@ -10,7 +10,7 @@ import (
type notifyNode struct { type notifyNode struct {
node *domain.WorkflowNode node *domain.WorkflowNode
settingsRepo settingRepository settingsRepo settingsRepository
*nodeLogger *nodeLogger
} }

View File

@ -70,6 +70,6 @@ type workflowOutputRepository interface {
Save(ctx context.Context, output *domain.WorkflowOutput, certificate *domain.Certificate, cb func(id string) error) error 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) GetByName(ctx context.Context, name string) (*domain.Settings, error)
} }

View File

@ -23,7 +23,7 @@ type workflowRepository interface {
ListEnabledAuto(ctx context.Context) ([]*domain.Workflow, error) ListEnabledAuto(ctx context.Context) ([]*domain.Workflow, error)
GetById(ctx context.Context, id string) (*domain.Workflow, error) GetById(ctx context.Context, id string) (*domain.Workflow, error)
Save(ctx context.Context, workflow *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 { type WorkflowService struct {
@ -91,11 +91,11 @@ func (s *WorkflowService) InitSchedule(ctx context.Context) error {
return nil 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 { 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 return err
} }
@ -114,7 +114,7 @@ func (s *WorkflowService) Run(ctx context.Context, options *domain.WorkflowRunRe
s.ch <- &workflowRunData{ s.ch <- &workflowRunData{
Workflow: workflow, Workflow: workflow,
Options: options, Options: req,
} }
return nil return nil
@ -165,7 +165,7 @@ func (s *WorkflowService) run(ctx context.Context, runData *workflowRunData) err
return nil return nil
} }
func (s *WorkflowService) Stop() { func (s *WorkflowService) Stop(ctx context.Context) {
s.cancel() s.cancel()
s.wg.Wait() s.wg.Wait()
} }

View File

@ -43,6 +43,7 @@ func main() {
workflow.Register() workflow.Register()
routes.Register(e.Router) routes.Register(e.Router)
e.Router.GET( e.Router.GET(
"/*", "/*",
echo.StaticDirectoryHandler(ui.DistDirFS, false), echo.StaticDirectoryHandler(ui.DistDirFS, false),