cleaned up the api for adding routes (it does it automatically when you create the route).

Added a function to the server to announce registered routes.
This commit is contained in:
dtookey 2022-07-21 09:53:49 -04:00
parent a72b54a114
commit 14cbf49e9a
2 changed files with 6 additions and 5 deletions

View File

@ -25,13 +25,15 @@ const (
UNDEFINED
)
func NewApiRoute(pattern string) *ApiRoute {
func NewApiRoute(serv *VinegarServlet, pattern string) *ApiRoute {
functionMap := make(map[HttpMethod]VinegarHandlerFunction)
ancestorRoute := NewServletRoute(pattern, createMethodHandler(&functionMap))
route := ApiRoute{
ancestorRoute,
&functionMap,
}
serv.AddRoute(route.VinegarRoute)
return &route
}

View File

@ -52,6 +52,7 @@ func NewServletRoute(routePattern string, handleFunc VinegarHandlerFunction) *Vi
}
func (s *VinegarServlet) AddRoute(route *VinegarRoute) {
route.Announce()
s.Routes = append(s.Routes, route)
}
@ -78,10 +79,8 @@ func (s *VinegarServlet) Start() {
}
func (s *VinegarServlet) PrintRoutes() {
for _, route := range s.Routes {
log.Println(route.Pattern.String())
}
func (r *VinegarRoute) Announce() {
log.Printf("Added route for [%s]\n", r.Pattern.String())
}
func SendError(w http.ResponseWriter, code int, msg string) {