From f6dbff900bdd356c0bb450618ec8b4375680c7bc Mon Sep 17 00:00:00 2001 From: Kirill Ryabov Date: Mon, 22 Jan 2024 21:17:01 +0700 Subject: [PATCH] MSP-2684 Add entrypoint config struct --- assembly/locator.go | 6 ++++-- middleware/entrypoint.go | 11 ++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/assembly/locator.go b/assembly/locator.go index c0f6494..e0381ff 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -115,8 +115,10 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli config.Http.MaxRequestBodySizeInMb*1024*1024, //nolint:gomnd handler, l.logger, - location.PathPrefix, - location.WithPrefix, + middleware.EntryPointConfig{ + WithPrefix: location.WithPrefix, + PathPrefix: location.PathPrefix, + }, ) mux.PathPrefix(location.PathPrefix).Handler(entrypoint) } diff --git a/middleware/entrypoint.go b/middleware/entrypoint.go index 50f409a..91cd85c 100644 --- a/middleware/entrypoint.go +++ b/middleware/entrypoint.go @@ -10,13 +10,18 @@ import ( "isp-gate-service/request" ) -func Entrypoint(maxReqBodySize int64, next Handler, logger log.Logger, pathPrefix string, withPrefix bool) http.Handler { +type EntryPointConfig struct { + PathPrefix string + WithPrefix bool +} + +func Entrypoint(maxReqBodySize int64, next Handler, logger log.Logger, cfg EntryPointConfig) http.Handler { return http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) { req.Body = http.MaxBytesReader(writer, req.Body, maxReqBodySize) endpoint := req.URL.Path - if !withPrefix { - prefix := fmt.Sprintf("%s/", pathPrefix) + if !cfg.WithPrefix { + prefix := fmt.Sprintf("%s/", cfg.PathPrefix) endpoint = strings.TrimPrefix(req.URL.Path, prefix) } ctx := request.NewContext(req, writer, endpoint)