mirror of
https://github.com/nkanaev/yarr.git
synced 2025-05-25 05:29:20 +00:00
router servehttp
This commit is contained in:
parent
4a4303afef
commit
f214c3166b
@ -1,6 +1,7 @@
|
|||||||
package router
|
package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -35,7 +36,7 @@ func (r *Router) For(path string, handler Handler) {
|
|||||||
r.routes = append(r.routes, x)
|
r.routes = append(r.routes, x)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) resolve(path string) *route {
|
func (r *Router) resolve(path string) *Route {
|
||||||
for _, r := range r.routes {
|
for _, r := range r.routes {
|
||||||
if r.regex.MatchString(path) {
|
if r.regex.MatchString(path) {
|
||||||
return &r
|
return &r
|
||||||
@ -44,6 +45,24 @@ func (r *Router) resolve(path string) *route {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
path := req.Url.Path
|
||||||
|
|
||||||
|
route := r.resolve(path)
|
||||||
|
if route == nil {
|
||||||
|
rw.WriteHeader(http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
context := &Context{}
|
||||||
|
context.Req = req
|
||||||
|
context.Out = rw
|
||||||
|
context.vars = regexGroups(path, route.regex)
|
||||||
|
context.index = -1
|
||||||
|
context.chain = route.chain
|
||||||
|
context.Next()
|
||||||
|
}
|
||||||
|
|
||||||
func regexGroups(input string, regex *regexp.Regexp) map[string]string {
|
func regexGroups(input string, regex *regexp.Regexp) map[string]string {
|
||||||
groups := make(map[string]string)
|
groups := make(map[string]string)
|
||||||
matches := regex.FindStringSubmatchIndex(input)
|
matches := regex.FindStringSubmatchIndex(input)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user