diff --git a/assembly/locator.go b/assembly/locator.go index f5dea29..15f154d 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -126,7 +126,6 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli middleware.EntryPointConfig{ WithPrefix: location.WithPrefix, PathPrefix: location.PathPrefix, - IsGrpcPath: location.Protocol == "grpc", }, ) mux.PathPrefix(location.PathPrefix).Handler(entrypoint) diff --git a/middleware/entrypoint.go b/middleware/entrypoint.go index 340708c..cdf5eb5 100644 --- a/middleware/entrypoint.go +++ b/middleware/entrypoint.go @@ -1,6 +1,7 @@ package middleware import ( + "fmt" "net/http" "strings" @@ -12,26 +13,22 @@ import ( type EntryPointConfig struct { PathPrefix string WithPrefix bool - IsGrpcPath 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) - ctx := request.NewContext(req, writer, getEndpoint(req, cfg)) + + endpoint := req.URL.Path + if !cfg.WithPrefix { + prefix := fmt.Sprintf("%s/", cfg.PathPrefix) + endpoint = strings.TrimPrefix(req.URL.Path, prefix) + } + ctx := request.NewContext(req, writer, endpoint) + err := next.Handle(ctx) if err != nil { logger.Error(req.Context(), errors.WithMessage(err, "uncaught error")) } }) } - -func getEndpoint(req *http.Request, cfg EntryPointConfig) string { - if cfg.WithPrefix { - return req.URL.Path - } - if cfg.IsGrpcPath { - cfg.PathPrefix += "/" - } - return strings.TrimPrefix(req.URL.Path, cfg.PathPrefix) -}