cms-template/service/service.go
Vasiliy Tolstov 973f9ac822
Some checks failed
build / test (push) Failing after 5s
build / lint (push) Failing after 6s
initial import
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-09-26 19:45:39 +03:00

150 lines
3.5 KiB
Go

package service
import (
"context"
"fmt"
cmsservice "go.unistack.org/cms-service"
cmsstorage "go.unistack.org/cms-service/storage"
mpb "go.unistack.org/cms-template-proto/micro"
serviceConfig "go.unistack.org/cms-template/config"
"go.unistack.org/cms-template/handler"
storage "go.unistack.org/cms-template/storage"
grpcsrv "go.unistack.org/micro-server-grpc/v3"
"go.unistack.org/micro/v3"
"go.unistack.org/micro/v3/broker"
"go.unistack.org/micro/v3/config"
"go.unistack.org/micro/v3/logger"
"go.unistack.org/micro/v3/register"
"go.unistack.org/micro/v3/router"
"go.unistack.org/micro/v3/server"
)
func NewService(ctx context.Context) (micro.Service, error) {
var err error
var svc micro.Service
var reg register.Register
var brk broker.Broker
var rtr router.Router
if ctx == nil {
ctx = context.Background()
}
cfg := serviceConfig.NewConfig()
cs := cmsservice.NewConfigLocal(cfg) // service.NewConfigRemote(cfg)...)
if r, ok := register.FromContext(ctx); ok && r != nil {
reg = r
} else {
reg = register.NewRegister()
}
if b, ok := broker.FromContext(ctx); ok && b != nil {
brk = b
} else {
brk = broker.NewBroker()
}
if r, ok := router.FromContext(ctx); ok && r != nil {
rtr = r
} else {
rtr = router.NewRouter()
}
// create grpc server
mgsrv := grpcsrv.NewServer(
server.Register(reg),
)
// create service
svc = micro.NewService(
micro.Server(mgsrv),
micro.Register(reg),
micro.Broker(brk),
micro.Router(rtr),
micro.Config(cs...),
)
h := handler.NewHandler(svc)
// init service
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))
}),
micro.BeforeStart(func(_ context.Context) error {
var scheme string
var connstr string
var dsn string
if v, ok := cfg.Storage.DSN[cfg.Service.Name]; ok {
connstr = v
} else if v, ok := cfg.Storage.DSN["all"]; ok {
connstr = v
}
fmt.Printf("SSSS %s\n", connstr)
scheme, dsn, err = cmsstorage.StorageOptions(connstr)
if err != nil {
return err
}
db, dbok := cmsstorage.FromContext(svc.Options().Context)
if !dbok {
var err error
db, err = cmsstorage.NewStorage(scheme, dsn)
if err != nil {
return err
}
}
store, err := storage.NewStorage(scheme, db)
if err != nil {
return err
}
return svc.Init(micro.Context(cmsstorage.InterfaceNewContext(svc.Options().Context, store)))
}),
); err != nil {
return nil, err
}
if err = mpb.RegisterAccountServiceServer(mgsrv, h); err != nil {
logger.Fatalf(ctx, "failed to register handler: %v", err)
}
return svc, nil
}