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 UNDEFINED
) )
func NewApiRoute(pattern string) *ApiRoute { func NewApiRoute(serv *VinegarServlet, pattern string) *ApiRoute {
functionMap := make(map[HttpMethod]VinegarHandlerFunction) functionMap := make(map[HttpMethod]VinegarHandlerFunction)
ancestorRoute := NewServletRoute(pattern, createMethodHandler(&functionMap)) ancestorRoute := NewServletRoute(pattern, createMethodHandler(&functionMap))
route := ApiRoute{ route := ApiRoute{
ancestorRoute, ancestorRoute,
&functionMap, &functionMap,
} }
serv.AddRoute(route.VinegarRoute)
return &route return &route
} }

View File

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