From 402c6c18cfd49a76b24fb697b20a314d4023b85c Mon Sep 17 00:00:00 2001 From: Kirill Ryabov Date: Mon, 22 Jan 2024 20:08:03 +0700 Subject: [PATCH 1/9] MSP-2684 Update local config --- conf/local.go | 1 + 1 file changed, 1 insertion(+) diff --git a/conf/local.go b/conf/local.go index 99910a4..5ca0822 100644 --- a/conf/local.go +++ b/conf/local.go @@ -11,6 +11,7 @@ type Local struct { } type Location struct { + WithPrefix bool SkipAuth bool PathPrefix string `validate:"required"` Protocol string `validate:"required,oneof=http grpc ws"` From 8a83ba0b3a211ea39c3d46289c94ca97bb178a0f Mon Sep 17 00:00:00 2001 From: Kirill Ryabov Date: Mon, 22 Jan 2024 20:12:58 +0700 Subject: [PATCH 2/9] 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) From c390fe3948ef77494952e3a955b2501a1532833a Mon Sep 17 00:00:00 2001 From: Kirill Ryabov Date: Mon, 22 Jan 2024 20:17:02 +0700 Subject: [PATCH 3/9] MSP-2684 Fix linter's warning --- assembly/locator.go | 1 + 1 file changed, 1 insertion(+) diff --git a/assembly/locator.go b/assembly/locator.go index 4b3cd66..14e896d 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -71,6 +71,7 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli mux := mux2.NewRouter() for _, location := range locations { + location := location var proxyFunc middleware.Handler enableBodyLog := config.Logging.BodyLogEnable From f94672569ba27616038d02bf5ccddefca0adba83 Mon Sep 17 00:00:00 2001 From: Kirill Ryabov Date: Mon, 22 Jan 2024 20:22:20 +0700 Subject: [PATCH 4/9] MSP-2684 Update config.yml --- conf/config.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/conf/config.yml b/conf/config.yml index a41bf7b..ea41273 100644 --- a/conf/config.yml +++ b/conf/config.yml @@ -25,3 +25,8 @@ locations: - pathPrefix: /mdm protocol: http targetModule: mdm-api + - pathPrefix: /api/pdp-4me-service/public + protocol: http + targetModule: pdp-4me-service + skipAuth: true + withPrefix: true \ No newline at end of file From e4892b737f88838a0648a29effb7ab99a7412ce8 Mon Sep 17 00:00:00 2001 From: Kirill Ryabov Date: Mon, 22 Jan 2024 20:51:06 +0700 Subject: [PATCH 5/9] MSP-2684 Bump version to 5.1.1 --- .version | 2 +- CHANGELOG.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.version b/.version index 831446c..ac14c3d 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -5.1.0 +5.1.1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 76a5522..fd4a266 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +### v5.1.1 +* добавлен параметр `withPrefix` в локальном конфиге для `location`, чтобы не обрезать путь при проксировании запроса ### v5.1.0 * обновлены зависимости (увеличены лимиты по семплированию логов, добавлена возможность конфигурировать семплирование) ### v5.0.0 From 87690bbd1221a42a9b736cdb3e85fc148216c05e Mon Sep 17 00:00:00 2001 From: Kirill Ryabov Date: Mon, 22 Jan 2024 21:02:25 +0700 Subject: [PATCH 6/9] MSP-2684 Remove unnecessary location --- conf/config.yml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/conf/config.yml b/conf/config.yml index ea41273..5274100 100644 --- a/conf/config.yml +++ b/conf/config.yml @@ -24,9 +24,4 @@ locations: targetModule: router - pathPrefix: /mdm protocol: http - targetModule: mdm-api - - pathPrefix: /api/pdp-4me-service/public - protocol: http - targetModule: pdp-4me-service - skipAuth: true - withPrefix: true \ No newline at end of file + targetModule: mdm-api \ No newline at end of file From 1f40e9d5d30da251c6746509c92b8a9ce3eb8b25 Mon Sep 17 00:00:00 2001 From: Kirill Ryabov Date: Mon, 22 Jan 2024 21:02:51 +0700 Subject: [PATCH 7/9] MSP-2684 Update Entrypoint function --- assembly/locator.go | 8 ++------ middleware/entrypoint.go | 6 +++--- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/assembly/locator.go b/assembly/locator.go index 14e896d..c0f6494 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -71,7 +71,6 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli mux := mux2.NewRouter() for _, location := range locations { - location := location var proxyFunc middleware.Handler enableBodyLog := config.Logging.BodyLogEnable @@ -112,15 +111,12 @@ 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, l.logger, - prefixToTrim, + location.PathPrefix, + location.WithPrefix, ) mux.PathPrefix(location.PathPrefix).Handler(entrypoint) } diff --git a/middleware/entrypoint.go b/middleware/entrypoint.go index d8b8f0a..50f409a 100644 --- a/middleware/entrypoint.go +++ b/middleware/entrypoint.go @@ -10,13 +10,13 @@ import ( "isp-gate-service/request" ) -func Entrypoint(maxReqBodySize int64, next Handler, logger log.Logger, pathPrefix *string) http.Handler { +func Entrypoint(maxReqBodySize int64, next Handler, logger log.Logger, pathPrefix string, withPrefix bool) 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 pathPrefix != nil { - prefix := fmt.Sprintf("%s/", *pathPrefix) + if !withPrefix { + prefix := fmt.Sprintf("%s/", pathPrefix) endpoint = strings.TrimPrefix(req.URL.Path, prefix) } ctx := request.NewContext(req, writer, endpoint) From 6683d2dcd00b4d5c754f88faf33fb8a89fdd6023 Mon Sep 17 00:00:00 2001 From: Kirill Ryabov Date: Mon, 22 Jan 2024 21:03:32 +0700 Subject: [PATCH 8/9] MSP-2684 Remove unnecessary location --- conf/config.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/conf/config.yml b/conf/config.yml index 5274100..2ce41fd 100644 --- a/conf/config.yml +++ b/conf/config.yml @@ -21,7 +21,4 @@ logfile: locations: - pathPrefix: /api protocol: grpc - targetModule: router - - pathPrefix: /mdm - protocol: http - targetModule: mdm-api \ No newline at end of file + targetModule: router \ No newline at end of file From f6dbff900bdd356c0bb450618ec8b4375680c7bc Mon Sep 17 00:00:00 2001 From: Kirill Ryabov Date: Mon, 22 Jan 2024 21:17:01 +0700 Subject: [PATCH 9/9] 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)