2025-05-13 13:05:46: merge [master:49d1bfaa3f027c5465de7a0c0d46a9bab0588e19];

This commit is contained in:
txix committed 2025-05-13 13:05:46 +03:00
1 parent e2b4becfd6
commit 8d40e76637
5 files changed
+89 -70

No files matched your search

+12 -9
View File
@@ -1,7 +1,6 @@
package middleware
import (
"fmt"
"net/http"
"strings"
@@ -13,22 +12,26 @@ 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)
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)
ctx := request.NewContext(req, writer, getEndpoint(req, cfg))
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)
}