106 lines
2.4 KiB
Go
106 lines
2.4 KiB
Go
|
package service
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
intcfg "go.unistack.org/unistack-org/pkgdash/config"
|
||
|
"go.unistack.org/unistack-org/pkgdash/handler"
|
||
|
pbmicro "go.unistack.org/unistack-org/pkgdash/proto/micro"
|
||
|
|
||
|
cmsservice "go.unistack.org/cms-service"
|
||
|
grpcsrv "go.unistack.org/micro-server-grpc/v3"
|
||
|
"go.unistack.org/micro/v3"
|
||
|
"go.unistack.org/micro/v3/config"
|
||
|
"go.unistack.org/micro/v3/logger"
|
||
|
"go.unistack.org/micro/v3/register"
|
||
|
microuter "go.unistack.org/micro/v3/router"
|
||
|
"go.unistack.org/micro/v3/server"
|
||
|
)
|
||
|
|
||
|
func NewService(ctx context.Context) (micro.Service, error) {
|
||
|
var reg register.Register
|
||
|
var router microuter.Router
|
||
|
|
||
|
if ctx == nil {
|
||
|
ctx = context.Background()
|
||
|
}
|
||
|
|
||
|
cfg := intcfg.NewConfig()
|
||
|
cs := cmsservice.NewConfigLocal(cfg)
|
||
|
|
||
|
if r, ok := register.FromContext(ctx); ok && r != nil {
|
||
|
reg = r
|
||
|
} else {
|
||
|
reg = register.NewRegister()
|
||
|
}
|
||
|
if r, ok := microuter.FromContext(ctx); ok && r != nil {
|
||
|
router = r
|
||
|
} else {
|
||
|
router = microuter.NewRouter()
|
||
|
}
|
||
|
|
||
|
mgsrv := grpcsrv.NewServer(
|
||
|
server.Register(reg),
|
||
|
)
|
||
|
|
||
|
svc := micro.NewService(
|
||
|
micro.Server(mgsrv),
|
||
|
micro.Register(reg),
|
||
|
micro.Router(router),
|
||
|
micro.Config(cs...),
|
||
|
)
|
||
|
|
||
|
h := handler.NewHandler(svc)
|
||
|
|
||
|
if err := svc.Init(
|
||
|
micro.AfterStart(func(_ context.Context) error {
|
||
|
return h.Init(svc.Options().Context)
|
||
|
}),
|
||
|
micro.BeforeStart(func(ctx context.Context) error {
|
||
|
if err := config.Load(ctx, cs, config.LoadOverride(true)); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err := config.Validate(ctx, cfg); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if err := svc.Init(
|
||
|
micro.Name(cfg.Service.Name),
|
||
|
micro.Version(cfg.Service.Version),
|
||
|
); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if err := svc.Server("grpc").Init(
|
||
|
server.Address(cfg.App.Address),
|
||
|
server.Name(cfg.Service.Name),
|
||
|
server.Version(cfg.Service.Version),
|
||
|
); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}),
|
||
|
micro.BeforeStart(func(_ context.Context) error {
|
||
|
level := logger.InfoLevel
|
||
|
if v, ok := cfg.Logger.Level[cfg.Service.Name]; ok {
|
||
|
level = logger.ParseLevel(v)
|
||
|
} else if v, ok := cfg.Logger.Level["all"]; ok {
|
||
|
level = logger.ParseLevel(v)
|
||
|
}
|
||
|
log := logger.NewLogger(
|
||
|
logger.WithLevel(level),
|
||
|
logger.WithCallerSkipCount(3),
|
||
|
)
|
||
|
return svc.Init(micro.Logger(log))
|
||
|
}),
|
||
|
); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
if err := pbmicro.RegisterDashboardServiceServer(mgsrv, h); err != nil {
|
||
|
logger.Fatalf(ctx, "failed to register handler: %v", err)
|
||
|
}
|
||
|
|
||
|
return svc, nil
|
||
|
}
|