2020-05-21 02:20:55 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
2021-08-10 00:31:08 +00:00
|
|
|
"strings"
|
2020-05-21 02:20:55 +00:00
|
|
|
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
2022-05-03 02:19:30 +00:00
|
|
|
// StripPath strips the first level of a path.
|
|
|
|
func StripPath(path string, next fasthttp.RequestHandler) fasthttp.RequestHandler {
|
2020-05-21 02:20:55 +00:00
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
2021-08-10 00:31:08 +00:00
|
|
|
uri := ctx.RequestURI()
|
2020-05-21 02:20:55 +00:00
|
|
|
|
2021-08-10 00:31:08 +00:00
|
|
|
if strings.HasPrefix(string(uri), path) {
|
2022-02-06 13:37:28 +00:00
|
|
|
ctx.SetUserValueBytes(UserValueKeyBaseURL, path)
|
2021-08-10 00:31:08 +00:00
|
|
|
|
|
|
|
newURI := strings.TrimPrefix(string(uri), path)
|
|
|
|
ctx.Request.SetRequestURI(newURI)
|
2020-05-21 02:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
next(ctx)
|
|
|
|
}
|
|
|
|
}
|