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.
|
2022-06-10 01:34:43 +00:00
|
|
|
func StripPath(path string) (middleware Middleware) {
|
2022-05-04 04:47:23 +00:00
|
|
|
return func(next fasthttp.RequestHandler) fasthttp.RequestHandler {
|
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
|
|
|
uri := ctx.RequestURI()
|
2020-05-21 02:20:55 +00:00
|
|
|
|
2022-05-04 04:47:23 +00:00
|
|
|
if strings.HasPrefix(string(uri), path) {
|
|
|
|
ctx.SetUserValueBytes(UserValueKeyBaseURL, path)
|
2021-08-10 00:31:08 +00:00
|
|
|
|
2022-05-04 04:47:23 +00:00
|
|
|
newURI := strings.TrimPrefix(string(uri), path)
|
|
|
|
ctx.Request.SetRequestURI(newURI)
|
|
|
|
}
|
2020-05-21 02:20:55 +00:00
|
|
|
|
2022-05-04 04:47:23 +00:00
|
|
|
next(ctx)
|
|
|
|
}
|
2020-05-21 02:20:55 +00:00
|
|
|
}
|
|
|
|
}
|