vinegar/servlet/Router.go
2023-08-01 10:02:31 -04:00

40 lines
776 B
Go

package servlet
import (
"errors"
"geniuscartel.xyz/vinegar/vinegarUtil"
"net/http"
"regexp"
)
type (
VinegarWebRouter struct {
Routes []*VinegarWebRoute
}
VinegarWebRoute struct {
Pattern *regexp.Regexp
Handler VinegarHandlerFunction
Cache vinegarUtil.Cache
}
)
func (s *VinegarWebRouter) AddRoute(route *VinegarWebRoute) {
route.Announce()
s.Routes = append(s.Routes, route)
}
func (r *VinegarWebRouter) RouteRequest(w http.ResponseWriter, req *http.Request) error {
path := req.URL.Path
for _, route := range r.Routes {
if route.Pattern.MatchString(path) {
//fmt.Printf("SERVING: [%s]=>{%s}\n", path, route.Pattern.String())
go route.Handler(w, req)
return nil
}
}
return errors.New("failed to match route for [" + path + "]")
}