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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StripPathMiddleware strips the first level of a path.
|
2021-08-10 00:31:08 +00:00
|
|
|
func StripPathMiddleware(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) {
|
|
|
|
ctx.SetUserValue("base_url", path)
|
|
|
|
|
|
|
|
newURI := strings.TrimPrefix(string(uri), path)
|
|
|
|
ctx.Request.SetRequestURI(newURI)
|
2020-05-21 02:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
next(ctx)
|
|
|
|
}
|
|
|
|
}
|