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