40 lines
776 B
Go
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 + "]")
|
|
}
|