assert trailing slash for handler prefixes

This commit is contained in:
Ryan McGuire 2025-03-25 10:49:47 -04:00
parent 89f016ed9d
commit adfedc9239

View File

@ -7,6 +7,7 @@ import (
"net"
"net/http"
"os"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
@ -60,6 +61,13 @@ func prepHTTPServer(opts *opts.AppHTTP) *http.Server {
// Inject extra handlers if given
// Used for grpc-gateway runtime.ServeMux handlers
for _, h := range opts.Handlers {
// prefix must end in / to route sub-paths
if !strings.HasSuffix(h.Prefix, "/") {
h.Prefix = h.Prefix + "/"
}
// if enabled, the path prefix is stripped before
// requests are sent to the handler
if h.StripPrefix {
h.Handler = http.StripPrefix(h.Prefix, h.Handler)
}