MSP-625 init assembly

This commit is contained in:
DeniskaRediska authored and d1slike committed 2022-07-27 10:44:07 +03:00
1 parent 1a63a616ee
commit ba2aeaf556
2 files changed
+128 -69

No files matched your search

+128
View File
@@ -0,0 +1,128 @@
package assembly
import (
"context"
"github.com/integration-system/isp-kit/app"
"github.com/integration-system/isp-kit/bootstrap"
"github.com/integration-system/isp-kit/cluster"
"github.com/integration-system/isp-kit/grpc/client"
"github.com/integration-system/isp-kit/http"
"github.com/integration-system/isp-kit/lb"
"github.com/integration-system/isp-kit/log"
"github.com/pkg/errors"
"isp-gate-service/conf"
"isp-gate-service/routes"
)
const defaultHost = "127.0.0.1"
type Assembly struct {
boot *bootstrap.Bootstrap
server *http.Server
logger *log.Adapter
routes *routes.Routes
locations []conf.Location
grpcClientByModuleName map[string]*client.Client
httpHostManagerByModuleName map[string]*lb.RoundRobin
}
func New(boot *bootstrap.Bootstrap) (*Assembly, error) {
server := http.NewServer()
localConfig := conf.Local{}
err := boot.App.Config().Read(&localConfig)
if err != nil {
return nil, errors.WithMessage(err, "read local config")
}
grpcClientByModuleName := make(map[string]*client.Client)
httpHostManagerByModuleName := make(map[string]*lb.RoundRobin)
for _, location := range localConfig.Locations {
switch location.Protocol {
case conf.GrpcProtocol:
cli, err := client.Default()
if err != nil {
return nil, errors.WithMessage(err, "new grpc client")
}
grpcClientByModuleName[location.TargetModule] = cli
case conf.HttpProtocol:
rb := lb.NewRoundRobin([]string{defaultHost})
httpHostManagerByModuleName[location.TargetModule] = rb
case conf.WebsocketProtocol:
default:
return nil, errors.Errorf("unexpected location protocol: %s", location.Protocol)
}
}
return &Assembly{
boot: boot,
server: server,
logger: boot.App.Logger(),
routes: routes.NewRoutes(),
locations: localConfig.Locations,
grpcClientByModuleName: grpcClientByModuleName,
httpHostManagerByModuleName: httpHostManagerByModuleName,
}, nil
}
func (a *Assembly) ReceiveConfig(ctx context.Context, remoteConfig []byte) error {
var (
newCfg conf.Remote
prevCfg conf.Remote
)
err := a.boot.RemoteConfig.Upgrade(remoteConfig, &newCfg, &prevCfg)
if err != nil {
a.logger.Fatal(ctx, errors.WithMessage(err, "upgrade remote config"))
}
a.logger.SetLevel(newCfg.LogLevel)
locator := NewLocator(a.logger, a.grpcClientByModuleName, a.httpHostManagerByModuleName, a.routes)
handler, err := locator.Handler(newCfg, a.locations)
if err != nil {
return errors.WithMessage(err, "locator handler")
}
a.server.Upgrade(handler)
return nil
}
func (a *Assembly) Runners() []app.Runner {
eventHandler := cluster.NewEventHandler().
RoutesReceiver(a.routes).
RemoteConfigReceiver(a)
for moduleName, upgrader := range a.grpcClientByModuleName {
eventHandler.RequireModule(moduleName, upgrader)
}
for moduleName, upgrader := range a.httpHostManagerByModuleName {
eventHandler.RequireModule(moduleName, upgrader)
}
return []app.Runner{
app.RunnerFunc(func(ctx context.Context) error {
return a.server.ListenAndServe(a.boot.BindingAddress)
}),
app.RunnerFunc(func(ctx context.Context) error {
return a.boot.ClusterCli.Run(ctx, eventHandler)
}),
}
}
func (a *Assembly) Closers() []app.Closer {
closers := []app.Closer{
a.boot.ClusterCli,
app.CloserFunc(func() error {
return a.server.Shutdown(a.boot.App.Context())
}),
}
for _, cliCloser := range a.grpcClientByModuleName {
closers = append(closers, cliCloser)
}
return closers
}
-69
View File
@@ -1,69 +0,0 @@
package server
import (
"sync"
"time"
"github.com/integration-system/isp-kit/log"
"github.com/integration-system/isp-lib/v2/config"
logrus "github.com/integration-system/isp-log"
"github.com/valyala/fasthttp"
"isp-gate-service/conf"
"isp-gate-service/handler"
"isp-gate-service/log_code"
)
const defaultTimeout = 60 * time.Second
var Http = &httpSrv{mx: sync.Mutex{}}
type httpSrv struct {
working bool
srv *fasthttp.Server
mx sync.Mutex
logger log.Logger
}
func (s *httpSrv) Init(isDifferentSetting bool, bodySize int64, logger log.Logger) {
s.logger = logger
if s.working {
if isDifferentSetting {
s.run(bodySize)
}
} else {
s.run(bodySize)
}
}
func (s *httpSrv) run(maxRequestBodySize int64) {
s.mx.Lock()
s.working = true
if s.srv != nil {
if err := s.srv.Shutdown(); err != nil {
logrus.Warn(log_code.WarnHttpServerShutdown, err)
}
}
localConfig := config.Get().(*conf.Configuration)
restAddress := localConfig.HttpInnerAddress.GetAddress()
s.srv = &fasthttp.Server{
Handler: handler.New(s.logger).CompleteRequest,
WriteTimeout: defaultTimeout,
ReadTimeout: defaultTimeout,
MaxRequestBodySize: int(maxRequestBodySize),
}
go func() {
if err := s.srv.ListenAndServe(restAddress); err != nil {
logrus.Error(log_code.ErrorHttpServerListen, err)
}
}()
s.mx.Unlock()
}
func (s *httpSrv) Close() {
if s.srv != nil {
s.working = false
if err := s.srv.Shutdown(); err != nil {
logrus.Warn(log_code.WarnHttpServerShutdown, err)
}
}
}