Files
ng-blog/pkg/middlewares/stripprefix.go
2025-05-05 10:00:50 +02:00

26 lines
429 B
Go

package middlewares
import (
"net/http"
"net/url"
)
func AddPrefix(prefix string, h http.Handler) http.Handler {
if prefix == "" {
return h
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p := prefix + r.URL.Path
rp := prefix + r.URL.RawPath
r2 := new(http.Request)
*r2 = *r
r2.URL = new(url.URL)
*r2.URL = *r.URL
r2.URL.Path = p
r2.URL.RawPath = rp
h.ServeHTTP(w, r2)
})
}