go项目脚手架
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sorbet/internal/services/service.go

69 lines
1.3 KiB

1 year ago
package services
import (
"context"
"errors"
"github.com/labstack/echo/v4"
"slices"
"sorbet/internal/services/company"
"sorbet/internal/services/config"
"sorbet/internal/services/feature"
"sorbet/internal/services/resource"
"sorbet/internal/services/system"
1 year ago
"sorbet/pkg/app"
)
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
}
1 year ago
func Init() {
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()
})
1 year ago
}
func Bootstrap(ctx context.Context) error {
e := ctx.Value("echo_framework").(*echo.Echo)
for _, service := range services {
err := service.Init(app.NewContext(ctx, e.Group("")))
if err != nil {
return err
}
err = service.Bootstrap()
if err != nil {
return err
}
}
return nil
1 year ago
}
func Destroy() error {
for i := len(services) - 1; i >= 0; i++ {
service := services[i]
err := service.Destroy()
if err != nil {
// TODO(hupeh): 是否需要销毁策略,比如可以继续或者中断等行为
return err
}
}
return nil
1 year ago
}