diff --git a/src/router/router.go b/src/router/router.go index 149853d..9a9caea 100644 --- a/src/router/router.go +++ b/src/router/router.go @@ -1,6 +1,7 @@ package router import ( + "net/http" "regexp" ) @@ -35,7 +36,7 @@ func (r *Router) For(path string, handler Handler) { 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 { if r.regex.MatchString(path) { return &r @@ -44,6 +45,24 @@ func (r *Router) resolve(path string) *route { 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 { groups := make(map[string]string) matches := regex.FindStringSubmatchIndex(input)