fix router

This commit is contained in:
Nazar Kanaev 2021-03-16 20:48:00 +00:00
parent f214c3166b
commit 80482e70a8
4 changed files with 29 additions and 24 deletions

24
src/router/match.go Normal file
View file

@ -0,0 +1,24 @@
package router
import "regexp"
func regexGroups(input string, regex *regexp.Regexp) map[string]string {
groups := make(map[string]string)
matches := regex.FindStringSubmatchIndex(input)
for i, key := range regex.SubexpNames()[1:] {
groups[key] = input[matches[i*2+2]:matches[i*2+3]]
}
return groups
}
func routeRegexp(route string) *regexp.Regexp {
chunks := regexp.MustCompile(`[\*\:]\w+`)
output := chunks.ReplaceAllStringFunc(route, func(m string) string {
if m[0:1] == `*` {
return "(?P<" + m[1:] + ">.+)"
}
return "(?P<" + m[1:] + ">[^/]+)"
})
output = "^" + output + "$"
return regexp.MustCompile(output)
}