From 34d8a15f0666e35d4f87d5487e75fb47e618f36b Mon Sep 17 00:00:00 2001 From: hupeh Date: Thu, 12 Oct 2023 16:04:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=A7=84=E8=8C=83=20service=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/init.go | 2 -- internal/services/company/service.go | 19 +++++++++------ internal/services/config/service.go | 20 ++++++++------- internal/services/feature/service.go | 17 +++++++------ internal/services/resource/service.go | 17 +++++++------ internal/services/service.go | 35 +++++++++++++++++++++------ internal/services/system/service.go | 17 +++++++------ pkg/app/applet.go | 16 ------------ pkg/app/service.go | 20 +++++++++++++++ 9 files changed, 99 insertions(+), 64 deletions(-) delete mode 100644 pkg/app/applet.go create mode 100644 pkg/app/service.go diff --git a/internal/init.go b/internal/init.go index 871e157..1948a77 100644 --- a/internal/init.go +++ b/internal/init.go @@ -3,7 +3,6 @@ package internal import ( "errors" "github.com/labstack/echo/v4" - echoSwagger "github.com/swaggo/echo-swagger" "gorm.io/gorm" "net/http" "sorbet/internal/entities" @@ -83,7 +82,6 @@ func Start() error { return next(c) } }) - e.GET("/swagger/*", echoSwagger.WrapHandler) e.GET("/", func(c echo.Context) error { repo := repositories.NewCompanyRepository(c.Get("db").(*gorm.DB)) company, err := repo.GetByID(c.Request().Context(), 1) diff --git a/internal/services/company/service.go b/internal/services/company/service.go index fc16e19..5356eaf 100644 --- a/internal/services/company/service.go +++ b/internal/services/company/service.go @@ -7,17 +7,20 @@ import ( type Service struct{} -func (s *Service) Init(ctx *app.Context) error { - ctx.Routes(&controller.CompanyController{}) - ctx.Routes(&controller.CompanyDepartmentController{}) - ctx.Routes(&controller.CompanyStaffController{}) - return nil +func (*Service) Name() string { + return "company" } -func (s *Service) Start() error { - return nil +func (*Service) Priority() int { + return 1 } -func (s *Service) Stop() error { +func (*Service) Init(ctx *app.Context) error { + ctx.Routes(&controller.CompanyController{}) + ctx.Routes(&controller.CompanyDepartmentController{}) + ctx.Routes(&controller.CompanyStaffController{}) return nil } + +func (s *Service) Bootstrap() error { return nil } +func (s *Service) Destroy() error { return nil } diff --git a/internal/services/config/service.go b/internal/services/config/service.go index 896abae..86e168f 100644 --- a/internal/services/config/service.go +++ b/internal/services/config/service.go @@ -5,19 +5,21 @@ import ( "sorbet/pkg/app" ) -type Service struct { -} +type Service struct{} -func (s *Service) Init(ctx *app.Context) error { - ctx.Routes(&controller.ConfigGroupController{}) - ctx.Routes(&controller.ConfigController{}) - return nil +func (*Service) Name() string { + return "config" } -func (s *Service) Start() error { - return nil +func (*Service) Priority() int { + return 0 } -func (s *Service) Stop() error { +func (*Service) Init(ctx *app.Context) error { + ctx.Routes(&controller.ConfigGroupController{}) + ctx.Routes(&controller.ConfigController{}) return nil } + +func (s *Service) Bootstrap() error { return nil } +func (s *Service) Destroy() error { return nil } diff --git a/internal/services/feature/service.go b/internal/services/feature/service.go index 48dcbe2..2462f9c 100644 --- a/internal/services/feature/service.go +++ b/internal/services/feature/service.go @@ -7,6 +7,14 @@ import ( type Service struct{} +func (*Service) Name() string { + return "feature" +} + +func (*Service) Priority() int { + return 3 +} + func (s *Service) Init(ctx *app.Context) error { ctx.Routes(&controller.FeatureCategoryController{}) ctx.Routes(&controller.FeatureConfigController{}) @@ -16,10 +24,5 @@ func (s *Service) Init(ctx *app.Context) error { return nil } -func (s *Service) Start() error { - return nil -} - -func (s *Service) Stop() error { - return nil -} +func (s *Service) Bootstrap() error { return nil } +func (s *Service) Destroy() error { return nil } diff --git a/internal/services/resource/service.go b/internal/services/resource/service.go index 6caff4f..69192be 100644 --- a/internal/services/resource/service.go +++ b/internal/services/resource/service.go @@ -7,16 +7,19 @@ import ( type Service struct{} -func (s *Service) Init(ctx *app.Context) error { - ctx.Routes(&controller.ResourceController{}) - ctx.Routes(&controller.ResourceCategoryController{}) - return nil +func (*Service) Name() string { + return "resource" } -func (s *Service) Start() error { - return nil +func (*Service) Priority() int { + return 2 } -func (s *Service) Stop() error { +func (*Service) Init(ctx *app.Context) error { + ctx.Routes(&controller.ResourceController{}) + ctx.Routes(&controller.ResourceCategoryController{}) return nil } + +func (s *Service) Bootstrap() error { return nil } +func (s *Service) Destroy() error { return nil } diff --git a/internal/services/service.go b/internal/services/service.go index d839f18..3b69d36 100644 --- a/internal/services/service.go +++ b/internal/services/service.go @@ -2,7 +2,9 @@ package services import ( "context" + "errors" "github.com/labstack/echo/v4" + "slices" "sorbet/internal/services/company" "sorbet/internal/services/config" "sorbet/internal/services/feature" @@ -11,26 +13,41 @@ import ( "sorbet/pkg/app" ) -var applets []app.Applet +var services []app.Service + +func Register(service app.Service) error { + for _, applet := range services { + if applet.Name() == service.Name() { + return errors.New("service already registered") + } + } + services = append(services, service) + return nil +} func Init() { - applets = []app.Applet{ + services = []app.Service{ &config.Service{}, &company.Service{}, &resource.Service{}, &feature.Service{}, &system.Service{}, } + + // 按优先级排序 + slices.SortFunc(services, func(a, b app.Service) int { + return b.Priority() - a.Priority() + }) } -func Start(ctx context.Context) error { +func Bootstrap(ctx context.Context) error { e := ctx.Value("echo_framework").(*echo.Echo) - for _, service := range applets { + for _, service := range services { err := service.Init(app.NewContext(ctx, e.Group(""))) if err != nil { return err } - err = service.Start() + err = service.Bootstrap() if err != nil { return err } @@ -38,10 +55,12 @@ func Start(ctx context.Context) error { return nil } -func Stop() error { - for _, service := range applets { - err := service.Stop() +func Destroy() error { + for i := len(services) - 1; i >= 0; i++ { + service := services[i] + err := service.Destroy() if err != nil { + // TODO(hupeh): 是否需要销毁策略,比如可以继续或者中断等行为 return err } } diff --git a/internal/services/system/service.go b/internal/services/system/service.go index 6cb21bc..4b9d620 100644 --- a/internal/services/system/service.go +++ b/internal/services/system/service.go @@ -7,6 +7,14 @@ import ( type Service struct{} +func (*Service) Name() string { + return "system" +} + +func (*Service) Priority() int { + return 3 +} + func (s *Service) Init(ctx *app.Context) error { ctx.Routes(&controller.SystemLogController{}) ctx.Routes(&controller.SystemMenuController{}) @@ -17,10 +25,5 @@ func (s *Service) Init(ctx *app.Context) error { return nil } -func (s *Service) Start() error { - return nil -} - -func (s *Service) Stop() error { - return nil -} +func (s *Service) Bootstrap() error { return nil } +func (s *Service) Destroy() error { return nil } diff --git a/pkg/app/applet.go b/pkg/app/applet.go deleted file mode 100644 index 3ed1d9a..0000000 --- a/pkg/app/applet.go +++ /dev/null @@ -1,16 +0,0 @@ -package app - -import "github.com/labstack/echo/v4" - -type Applet interface { - // Init 初始化服务 - Init(ctx *Context) error - // Start 启动服务 - Start() error - // Stop 停止服务 - Stop() error -} - -type Routable interface { - InitRoutes(r *echo.Group) -} diff --git a/pkg/app/service.go b/pkg/app/service.go new file mode 100644 index 0000000..2b63e16 --- /dev/null +++ b/pkg/app/service.go @@ -0,0 +1,20 @@ +package app + +import "github.com/labstack/echo/v4" + +type Service interface { + // Name 服务名称 + Name() string + // Priority 优先级,用于启动和销毁的执行顺序 + Priority() int + // Init 初始化服务 + Init(ctx *Context) error + // Bootstrap 启动服务 + Bootstrap() error + // Destroy 销毁服务 + Destroy() error +} + +type Routable interface { + InitRoutes(r *echo.Group) +}