mirror of
https://github.com/nkanaev/yarr.git
synced 2025-12-15 00:27:20 +00:00
refactoring
This commit is contained in:
42
server/router.go
Normal file
42
server/router.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type Route struct {
|
||||
url string
|
||||
urlRegex *regexp.Regexp
|
||||
handler func(http.ResponseWriter, *http.Request)
|
||||
}
|
||||
|
||||
func p(path string, handler func(http.ResponseWriter, *http.Request)) Route {
|
||||
var urlRegexp string
|
||||
urlRegexp = regexp.MustCompile(`[\*\:]\w+`).ReplaceAllStringFunc(path, func(m string) string {
|
||||
if m[0:1] == `*` {
|
||||
return "(?P<" + m[1:] + ">.+)"
|
||||
}
|
||||
return "(?P<" + m[1:] + ">[^/]+)"
|
||||
})
|
||||
urlRegexp = "^" + urlRegexp + "$"
|
||||
return Route{
|
||||
url: path,
|
||||
urlRegex: regexp.MustCompile(urlRegexp),
|
||||
handler: handler,
|
||||
}
|
||||
}
|
||||
|
||||
func getRoute(req *http.Request) (*Route, map[string]string) {
|
||||
vars := make(map[string]string)
|
||||
for _, route := range routes {
|
||||
if route.urlRegex.MatchString(req.URL.Path) {
|
||||
matches := route.urlRegex.FindStringSubmatchIndex(req.URL.Path)
|
||||
for i, key := range route.urlRegex.SubexpNames()[1:] {
|
||||
vars[key] = req.URL.Path[matches[i*2+2]:matches[i*2+3]]
|
||||
}
|
||||
return &route, vars
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
Reference in New Issue
Block a user