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