1 Star 0 Fork 0

player1 / restful-api-demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
handler_service.go 4.10 KB
一键复制 编辑 原始数据 按行查看 历史
package http
import (
"fmt"
"os"
"encoding/json"
"github.com/infraboard/mcube/logger"
"github.com/infraboard/mcube/logger/zap"
"gitee.com/player1111/restful-api-demo.git/apps"
"gitee.com/player1111/restful-api-demo.git/conf"
)
// 全局Handler管理表
var httpApps = map[string]Handler{}
//var HandlerServiceGlobal HandlerService
// 定义处理程序的构造函数类型
type handlerConstructor func() Handler
// 定义一个处理程序的构造函数映射,用于存储不同服务类型的处理程序构造函数
var handlerConstructors = map[string]handlerConstructor{
"HostService": NewHostHandler,
"ServerService": NewServerHandler,
}
// RegisterHandlerConstructor 注册处理程序的函数
func RegisterHandlerConstructor(svc apps.AppService, constructor handlerConstructor) {
handlerConstructors[svc.ServiceName()] = constructor
}
// HandlerService 是一个管理处理程序(handler)的服务接口
type HandlerService interface {
RegistryHandlers(handler Handler) error
//CreateHandler(service apps.AppService) (Handler, error)
//getSupportedServices() []apps.AppService
Start() error
Stop() error
}
// handlerService 实现了 HandlerService 接口
type handlerService struct {
log logger.Logger
//handlerFactory *HandlerFactory
//httpApps map[string]Handler
}
/*
func (hs *handlerService) getHttpApps() map[string]Handler {
return nil
}*/
/*func (hs *handlerService) getSupportedServices() []apps.AppService {
return apps.SupportedServices
}*/
func (hs *handlerService) CreateHandler(service apps.AppService) (Handler, error) {
constructor, ok := handlerConstructors[service.ServiceName()]
hs.log.Infof("Service type: %s", service.ServiceName())
if !ok {
return nil, fmt.Errorf("no handler constructor found for service type")
}
return constructor(), nil
/*switch svc := service.(type) {
case host.HostService:
return &HostHandler{svc: svc}, nil
case server.ServerService:
return &ServerHandler{svc: svc}, nil
case nil:
return nil, fmt.Errorf("service type is nil")
default:
return nil, fmt.Errorf("no support service type")
}*/
}
// RegistryHandlers 注册处理程序到路由
func (hs *handlerService) RegistryHandlers(handler Handler) error {
if handler == nil {
return fmt.Errorf("handler empty\n")
}
if _, ok := httpApps[handler.Name()]; ok {
return fmt.Errorf("%s has exist\n", handler.Name())
}
httpApps[handler.Name()] = handler
return nil
}
func (hs *handlerService) Start() error {
//apps.RegistryService()
appServices, err := hs.readServiceConfig(conf.AppservicesPath)
if err != nil {
return fmt.Errorf("failed to read service config: %v", err)
}
err = hs.executeEnabledServices(appServices)
if err != nil {
return fmt.Errorf("failed to Enabled Service: %v", err)
}
/*for _, svc := range apps.SupportedServices {
handler, err := hs.CreateHandler(svc)
if err != nil {
return err
}
if err = hs.RegistryHandlers(handler); err != nil {
return err
}
}*/
/* hostHandler, err := hs.NewHandler(apps.HostService)
serverHandler, err := hs.NewHandler(apps.ServerService)*/
return nil
}
func (hs *handlerService) readServiceConfig(fileName string) (*conf.ServiceConfig, error) {
var serviceConfig conf.ServiceConfig
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer file.Close()
if err = json.NewDecoder(file).Decode(&serviceConfig); err != nil {
return nil, err
}
return &serviceConfig, nil
}
func (hs *handlerService) executeEnabledServices(c *conf.ServiceConfig) error {
for _, service := range c.Services {
if service.Enabled {
constructor, ok := handlerConstructors[service.Name]
if !ok {
return fmt.Errorf("no handler constructor found for service type")
}
handler := constructor()
hs.log.Infof("Executing handler for service: %s\n", service.Name)
if err := hs.RegistryHandlers(handler); err != nil {
return fmt.Errorf("failed to registry handler for service: %s, error: %v", service.Name, err)
}
}
}
return nil
}
func (hs *handlerService) Stop() error {
return nil
}
func NewHandlerService() HandlerService {
return &handlerService{
log: zap.L().Named("HandlerService"),
}
}
Go
1
https://gitee.com/player1111/restful-api-demo.git
git@gitee.com:player1111/restful-api-demo.git
player1111
restful-api-demo
restful-api-demo
19fd67857982

搜索帮助