196 lines
6.7 KiB
Go
196 lines
6.7 KiB
Go
package assembly
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/txix-open/isp-kit/metrics"
|
|
"github.com/txix-open/isp-kit/metrics/http_metrics"
|
|
|
|
"isp-gate-service/cache"
|
|
"isp-gate-service/conf"
|
|
"isp-gate-service/middleware"
|
|
"isp-gate-service/proxy"
|
|
"isp-gate-service/repository"
|
|
"isp-gate-service/routes"
|
|
"isp-gate-service/service"
|
|
|
|
mux2 "github.com/gorilla/mux"
|
|
"github.com/txix-open/isp-kit/errors"
|
|
"github.com/txix-open/isp-kit/grpc/client"
|
|
"github.com/txix-open/isp-kit/lb"
|
|
"github.com/txix-open/isp-kit/log"
|
|
)
|
|
|
|
type Locator struct {
|
|
logger log.Logger
|
|
grpcClientByModuleName map[string]*client.Client
|
|
httpHostManagerByModuleName map[string]*lb.RoundRobin
|
|
routes *routes.Routes
|
|
systemCli *client.Client
|
|
adminCli *client.Client
|
|
lockerCli *client.Client
|
|
routerLb *lb.RoundRobin
|
|
usersAuthCache *cache.Cache
|
|
}
|
|
|
|
func NewLocator(
|
|
logger log.Logger,
|
|
grpcClientByModuleName map[string]*client.Client,
|
|
httpHostManagerByModuleName map[string]*lb.RoundRobin,
|
|
routes *routes.Routes,
|
|
systemCli *client.Client,
|
|
adminCli *client.Client,
|
|
lockerCli *client.Client,
|
|
routerLb *lb.RoundRobin,
|
|
usersAuthCache *cache.Cache,
|
|
) Locator {
|
|
return Locator{
|
|
logger: logger,
|
|
grpcClientByModuleName: grpcClientByModuleName,
|
|
httpHostManagerByModuleName: httpHostManagerByModuleName,
|
|
routes: routes,
|
|
systemCli: systemCli,
|
|
adminCli: adminCli,
|
|
lockerCli: lockerCli,
|
|
routerLb: routerLb,
|
|
usersAuthCache: usersAuthCache,
|
|
}
|
|
}
|
|
|
|
func (l Locator) Handler(config conf.Remote, locations []conf.Location) (http.Handler, error) { // nolint:funlen
|
|
systemRepo := repository.NewSystem(l.systemCli)
|
|
adminRepo := repository.NewAdmin(l.adminCli)
|
|
|
|
authenticationCache := repository.NewAuthenticationCache(time.Duration(config.Caching.AuthenticationDataInSec) * time.Second)
|
|
authentication := service.NewAuthentication(authenticationCache, systemRepo)
|
|
|
|
userAuthenticationCache := repository.NewUserAuthenticationCache(l.usersAuthCache)
|
|
userAuthRepo := repository.NewUserAuth(l.routerLb)
|
|
userAuthentication, err := service.NewUserAuthentication(
|
|
config.CustomAuth,
|
|
userAuthenticationCache,
|
|
userAuthRepo,
|
|
)
|
|
if err != nil {
|
|
return nil, errors.WithMessage(err, "new user authentication")
|
|
}
|
|
|
|
adminService := service.NewAdmin(
|
|
repository.NewAuthorizationCache(time.Duration(config.Caching.AuthorizationDataInSec)*time.Second),
|
|
adminRepo,
|
|
)
|
|
|
|
authorization := service.NewAuthorization(
|
|
repository.NewAuthorizationCache(time.Duration(config.Caching.AuthorizationDataInSec)*time.Second),
|
|
systemRepo,
|
|
)
|
|
|
|
lockRepo := repository.NewLocker(l.lockerCli)
|
|
dailyLimitService := service.NewDailyLimit(lockRepo, config.DailyLimits)
|
|
throttlingService := service.NewThrottling(lockRepo, config.Throttling)
|
|
|
|
skipBodyLoggingEndpointPrefixes := make([]string, 0, len(config.Logging.SkipBodyLoggingEndpointPrefixes))
|
|
for _, prefix := range config.Logging.SkipBodyLoggingEndpointPrefixes {
|
|
skipBodyLoggingEndpointPrefixes = append(skipBodyLoggingEndpointPrefixes, strings.TrimPrefix(prefix, "/"))
|
|
}
|
|
|
|
skipLogging := middleware.NewSkipLoggingPaths(config.Logging.SkipLoggingPathPrefixes)
|
|
|
|
mux := mux2.NewRouter()
|
|
for _, location := range locations {
|
|
var proxyFunc middleware.Handler
|
|
enableBodyLog := config.Logging.BodyLogEnable
|
|
|
|
switch location.Protocol {
|
|
case conf.GrpcProtocol:
|
|
cli := l.grpcClientByModuleName[location.TargetModule]
|
|
proxyFunc = proxy.NewGrpc(cli, location.SkipAuth, time.Duration(config.Http.ProxyTimeoutInSec)*time.Second)
|
|
case conf.HttpProtocol:
|
|
hostManager := l.httpHostManagerByModuleName[location.TargetModule]
|
|
if location.TargetModule == routerModuleName {
|
|
hostManager = l.routerLb
|
|
}
|
|
proxyFunc = proxy.NewHttp(hostManager, location.SkipAuth, time.Duration(config.Http.ProxyTimeoutInSec)*time.Second)
|
|
case conf.WsProtocol:
|
|
hostManager := l.httpHostManagerByModuleName[location.TargetModule]
|
|
proxyFunc = proxy.NewWs(hostManager, location.SkipAuth)
|
|
enableBodyLog = false
|
|
default:
|
|
return nil, errors.Errorf("not supported protocol %s", location.Protocol)
|
|
}
|
|
|
|
forwardReqIdByAppId := make(map[int]bool, len(config.ForwardReqIdClientSettings))
|
|
for _, setting := range config.ForwardReqIdClientSettings {
|
|
forwardReqIdByAppId[setting.ApplicationId] = setting.ForwardRequestId
|
|
}
|
|
|
|
enableUtf8JsonCharsetByApp := make(map[int]bool, len(config.ResponseContentTypeSettings))
|
|
for _, setting := range config.ResponseContentTypeSettings {
|
|
enableUtf8JsonCharsetByApp[setting.ApplicationId] = setting.AddJsonUtf8Charset
|
|
}
|
|
|
|
metricsStorage := http_metrics.NewServerStorage(metrics.DefaultRegistry)
|
|
|
|
handler := middleware.Chain(
|
|
proxyFunc,
|
|
middleware.Logger(
|
|
l.logger, config.Logging.RequestLogEnable,
|
|
enableBodyLog,
|
|
skipLogging,
|
|
skipBodyLoggingEndpointPrefixes,
|
|
config.Logging.EnableForceUnescapingUnicode,
|
|
),
|
|
middleware.RequestId(),
|
|
middleware.ErrorHandler(l.logger),
|
|
middleware.UserAuthenticate(userAuthentication, l.logger),
|
|
middleware.Authenticate(authentication),
|
|
middleware.AdminAuthenticate(adminService),
|
|
middleware.ClientRequestId(config.EnableClientRequestIdForwarding, forwardReqIdByAppId),
|
|
middleware.JsonResponseUtf8Charset(enableUtf8JsonCharsetByApp),
|
|
middleware.Authorize(authorization, l.logger),
|
|
middleware.AdminAuthorize(adminService),
|
|
middleware.Throttling(throttlingService),
|
|
middleware.DailyLimit(dailyLimitService),
|
|
middleware.Metrics(metricsStorage),
|
|
)
|
|
|
|
errorOnUnknownEndpoint := true
|
|
if location.SkipAuth {
|
|
errorOnUnknownEndpoint = false
|
|
handler = middleware.Chain(
|
|
proxyFunc,
|
|
middleware.Logger(l.logger, config.Logging.RequestLogEnable,
|
|
enableBodyLog,
|
|
skipLogging,
|
|
skipBodyLoggingEndpointPrefixes,
|
|
config.Logging.EnableForceUnescapingUnicode,
|
|
),
|
|
middleware.RequestId(),
|
|
middleware.ErrorHandler(l.logger),
|
|
middleware.ClientRequestId(config.EnableClientRequestIdForwarding, forwardReqIdByAppId),
|
|
middleware.Metrics(metricsStorage),
|
|
)
|
|
}
|
|
entrypoint := middleware.Entrypoint(
|
|
config.Http.MaxRequestBodySizeInMb*1024*1024, //nolint:mnd
|
|
handler,
|
|
middleware.EntryPointConfig{
|
|
WithPrefix: location.WithPrefix,
|
|
PathPrefix: location.PathPrefix,
|
|
ErrorOnUnknownEndpoint: errorOnUnknownEndpoint,
|
|
WithLendingSlash: location.Protocol != conf.GrpcProtocol,
|
|
SkipLogging: skipLogging,
|
|
},
|
|
l.routes,
|
|
l.logger,
|
|
)
|
|
mux.PathPrefix(location.PathPrefix).Handler(entrypoint)
|
|
}
|
|
|
|
mux.NotFoundHandler = middleware.UnknownLocation(l.logger, skipLogging)
|
|
|
|
return mux, nil
|
|
}
|