mirror of
https://github.com/usual2970/certimate.git
synced 2025-06-08 05:29:51 +00:00
refactor: clean code
This commit is contained in:
parent
dab6ad917c
commit
0869eaafdd
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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 := ¬ifyHandler{
|
||||
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)
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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() {
|
||||
func Register(e *echo.Echo) {
|
||||
notifyRepo := repository.NewSettingsRepository()
|
||||
notifySvc = notify.NewNotifyService(notifyRepo)
|
||||
|
||||
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)
|
||||
|
||||
workflowSvc := getWorkflowService()
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
|
||||
type notifyNode struct {
|
||||
node *domain.WorkflowNode
|
||||
settingsRepo settingRepository
|
||||
settingsRepo settingsRepository
|
||||
*nodeLogger
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user