From 8a83ba0b3a211ea39c3d46289c94ca97bb178a0f Mon Sep 17 00:00:00 2001 From: Kirill Ryabov Date: Mon, 22 Jan 2024 20:12:58 +0700 Subject: [PATCH] MSP-2684 Add using 'withPrefix' flag to avoid trimming request's url prefix --- assembly/locator.go | 7 +++++-- middleware/entrypoint.go | 9 ++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/assembly/locator.go b/assembly/locator.go index 90c2dd8..4b3cd66 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -111,12 +111,15 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli middleware.ErrorHandler(l.logger), ) } - + var prefixToTrim *string + if !location.WithPrefix { + prefixToTrim = &location.PathPrefix + } entrypoint := middleware.Entrypoint( config.Http.MaxRequestBodySizeInMb*1024*1024, //nolint:gomnd handler, - location.PathPrefix, l.logger, + prefixToTrim, ) mux.PathPrefix(location.PathPrefix).Handler(entrypoint) } diff --git a/middleware/entrypoint.go b/middleware/entrypoint.go index c163c0c..d8b8f0a 100644 --- a/middleware/entrypoint.go +++ b/middleware/entrypoint.go @@ -10,12 +10,15 @@ import ( "isp-gate-service/request" ) -func Entrypoint(maxReqBodySize int64, next Handler, pathPrefix string, logger log.Logger) http.Handler { +func Entrypoint(maxReqBodySize int64, next Handler, logger log.Logger, pathPrefix *string) http.Handler { return http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) { req.Body = http.MaxBytesReader(writer, req.Body, maxReqBodySize) - prefix := fmt.Sprintf("%s/", pathPrefix) - endpoint := strings.TrimPrefix(req.URL.Path, prefix) + endpoint := req.URL.Path + if pathPrefix != nil { + prefix := fmt.Sprintf("%s/", *pathPrefix) + endpoint = strings.TrimPrefix(req.URL.Path, prefix) + } ctx := request.NewContext(req, writer, endpoint) err := next.Handle(ctx)