From 5888709f9903f960b2032ee88247c4cdc8185491 Mon Sep 17 00:00:00 2001 From: Mark Bailey Date: Sat, 14 Dec 2024 12:35:19 -0500 Subject: [PATCH] WIP --- app/routing/router.go | 63 +++++++++++++++++-------------------------- app/routing/routes.go | 10 +++---- 2 files changed, 29 insertions(+), 44 deletions(-) diff --git a/app/routing/router.go b/app/routing/router.go index bb4acd2..9749cd4 100644 --- a/app/routing/router.go +++ b/app/routing/router.go @@ -29,9 +29,10 @@ type Route struct { type Router struct { Mux *http.ServeMux - SubRouters *[]Router Middleware *[]middleware.Func + BaseRoute Route BasePath string + SubRouters []*Router Routes []Route } @@ -43,44 +44,35 @@ var ( fsOnce sync.Once ) -func (r *Router) HandleAllRequestMethods(route Route, mux *http.ServeMux) { +func (r *Router) HandleAllRequestMethods(route Route, fullPath string, mux *http.ServeMux) { ctx := controller.ControllerCtx{RouterCtx: rtr} c := route.Controller.Init(sess, database.ChooseDB(), logger.NewCompositeLogger(), ctx) - mux.Handle("GET "+r.BasePath+route.Path, handler.HandlerFunc(c.Get)) - mux.Handle("OPTIONS "+r.BasePath+route.Path, handler.HandlerFunc(c.Options)) - mux.Handle("TRACE "+r.BasePath+route.Path, handler.HandlerFunc(c.Trace)) - mux.Handle("PUT "+r.BasePath+route.Path, handler.HandlerFunc(c.Put)) - mux.Handle("DELETE "+r.BasePath+route.Path, handler.HandlerFunc(c.Delete)) - mux.Handle("POST "+r.BasePath+route.Path, handler.HandlerFunc(c.Post)) - mux.Handle("PATCH "+r.BasePath+route.Path, handler.HandlerFunc(c.Patch)) - mux.Handle("CONNECT "+r.BasePath+route.Path, handler.HandlerFunc(c.Connect)) + mux.Handle("GET "+fullPath, handler.HandlerFunc(c.Get)) + mux.Handle("OPTIONS "+fullPath, handler.HandlerFunc(c.Options)) + mux.Handle("TRACE "+fullPath, handler.HandlerFunc(c.Trace)) + mux.Handle("PUT "+fullPath, handler.HandlerFunc(c.Put)) + mux.Handle("DELETE "+fullPath, handler.HandlerFunc(c.Delete)) + mux.Handle("POST "+fullPath, handler.HandlerFunc(c.Post)) + mux.Handle("PATCH "+fullPath, handler.HandlerFunc(c.Patch)) + mux.Handle("CONNECT "+fullPath, handler.HandlerFunc(c.Connect)) } func (r *Router) RegisterRoutes() http.Handler { - rtrOnce.Do(func() { - rtr = r - }) - muxOnce.Do(func() { - mux = http.NewServeMux() - r.Mux = mux - }) - fsOnce.Do(func() { - r.RegisterFs() - }) + rtrOnce.Do(func() { rtr = r }) + muxOnce.Do(func() { mux = http.NewServeMux(); r.Mux = mux }) + fsOnce.Do(func() { r.RegisterFs() }) - if r.Mux == nil { - r.Mux = http.NewServeMux() - } - - if r.SubRouters != nil { - for _, subRouter := range *r.SubRouters { - sr := subRouter.RegisterRoutes() - r.Mux.Handle(r.BasePath+subRouter.BasePath, sr) - } + if r.Mux == nil { + r.Mux = http.NewServeMux() } + for _, subRouter := range r.SubRouters { + r.Mux.Handle(subRouter.BasePath, subRouter.RegisterRoutes()) + } + + r.Mux.Handle(r.BasePath+r.BaseRoute.Path, handler.HandlerFunc(r.BaseRoute.Controller.Get)) for _, route := range r.Routes { - r.HandleAllRequestMethods(route, r.Mux) + r.HandleAllRequestMethods(route, r.BasePath+route.Path, r.Mux) } if r.Middleware != nil { @@ -103,19 +95,13 @@ type RouteMapping struct { } func (r *Router) GetFlatRouteList() []RouteMapping { - var routes []RouteMapping - + routes := []RouteMapping{{Path: r.BasePath, Name: r.BaseRoute.Name}} for _, route := range r.Routes { routes = append(routes, RouteMapping{Path: r.BasePath + route.Path, Name: route.Name}) } - - if r.SubRouters == nil { - return routes - } - for _, subRouter := range *r.SubRouters { + for _, subRouter := range r.SubRouters { routes = append(routes, subRouter.GetFlatRouteList()...) } - return routes } @@ -125,7 +111,6 @@ func (r *Router) GetRouteByName(name string) (string, error) { return route.Path, nil } } - return "", werror.Wrap(errors.New(name+" does not exist"), "Route not found") } diff --git a/app/routing/routes.go b/app/routing/routes.go index c6686ab..e8efa33 100644 --- a/app/routing/routes.go +++ b/app/routing/routes.go @@ -16,21 +16,21 @@ var ( func NewRouter() *Router { rtrinstOnce.Do(func() { rtrinst = &Router{ - Mux: nil, - BasePath: "/", + Mux: nil, + BasePath: "/", + BaseRoute: Route{Controller: shared.HomePageController{}, Path: "", Name: "app.index"}, Routes: []Route{ - {Controller: shared.HomePageController{}, Path: "", Name: "app.index"}, {Controller: shared.SignUpHandler{}, Path: "sign-up", Name: "app.user.sign_up"}, {Controller: shared.SignInHandler{}, Path: "sign-in", Name: "app.user.sign_in"}, {Controller: shared.SignOutHandler{}, Path: "sign-out", Name: "app.user.sign_out"}, {Controller: shared.PopulateHandler{}, Path: "populate", Name: "app.populate"}, }, - SubRouters: &[]Router{ + SubRouters: []*Router{ { Mux: nil, BasePath: "/admin/", + BaseRoute: Route{Controller: admin.IndexHandler{}, Path: "", Name: "app.admin.index"}, Routes: []Route{ - {Controller: admin.IndexHandler{}, Path: "", Name: "app.admin.index"}, {Controller: admin.IndexHandler{}, Path: "butt", Name: "app.admin.butt"}, }, SubRouters: nil,