Files
kra-new/internal/utils/routepath/routepath.go
T

23 lines
634 B
Go

package routepath
import "strings"
// Normalize removes only a complete configured router prefix. Paths
// such as /administrator must not be shortened when the prefix is /admin.
func Normalize(path, routerPrefix string) string {
prefix := strings.Trim(strings.TrimSpace(routerPrefix), "/")
if prefix == "" {
return path
}
// Gin also accepts a relative group name such as "api". Normalize it
// the same way before comparing a request path with Casbin policies.
prefix = "/" + prefix
if path == prefix {
return "/"
}
if strings.HasPrefix(path, prefix+"/") {
return strings.TrimPrefix(path, prefix)
}
return path
}