This commit is contained in:
Mark Bailey 2024-12-14 12:35:19 -05:00
parent ea3506c602
commit 5888709f99
2 changed files with 29 additions and 44 deletions

View File

@ -29,9 +29,10 @@ type Route struct {
type Router struct { type Router struct {
Mux *http.ServeMux Mux *http.ServeMux
SubRouters *[]Router
Middleware *[]middleware.Func Middleware *[]middleware.Func
BaseRoute Route
BasePath string BasePath string
SubRouters []*Router
Routes []Route Routes []Route
} }
@ -43,44 +44,35 @@ var (
fsOnce sync.Once 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} ctx := controller.ControllerCtx{RouterCtx: rtr}
c := route.Controller.Init(sess, database.ChooseDB(), logger.NewCompositeLogger(), ctx) c := route.Controller.Init(sess, database.ChooseDB(), logger.NewCompositeLogger(), ctx)
mux.Handle("GET "+r.BasePath+route.Path, handler.HandlerFunc(c.Get)) mux.Handle("GET "+fullPath, handler.HandlerFunc(c.Get))
mux.Handle("OPTIONS "+r.BasePath+route.Path, handler.HandlerFunc(c.Options)) mux.Handle("OPTIONS "+fullPath, handler.HandlerFunc(c.Options))
mux.Handle("TRACE "+r.BasePath+route.Path, handler.HandlerFunc(c.Trace)) mux.Handle("TRACE "+fullPath, handler.HandlerFunc(c.Trace))
mux.Handle("PUT "+r.BasePath+route.Path, handler.HandlerFunc(c.Put)) mux.Handle("PUT "+fullPath, handler.HandlerFunc(c.Put))
mux.Handle("DELETE "+r.BasePath+route.Path, handler.HandlerFunc(c.Delete)) mux.Handle("DELETE "+fullPath, handler.HandlerFunc(c.Delete))
mux.Handle("POST "+r.BasePath+route.Path, handler.HandlerFunc(c.Post)) mux.Handle("POST "+fullPath, handler.HandlerFunc(c.Post))
mux.Handle("PATCH "+r.BasePath+route.Path, handler.HandlerFunc(c.Patch)) mux.Handle("PATCH "+fullPath, handler.HandlerFunc(c.Patch))
mux.Handle("CONNECT "+r.BasePath+route.Path, handler.HandlerFunc(c.Connect)) mux.Handle("CONNECT "+fullPath, handler.HandlerFunc(c.Connect))
} }
func (r *Router) RegisterRoutes() http.Handler { func (r *Router) RegisterRoutes() http.Handler {
rtrOnce.Do(func() { rtrOnce.Do(func() { rtr = r })
rtr = r muxOnce.Do(func() { mux = http.NewServeMux(); r.Mux = mux })
}) fsOnce.Do(func() { r.RegisterFs() })
muxOnce.Do(func() {
mux = http.NewServeMux()
r.Mux = mux
})
fsOnce.Do(func() {
r.RegisterFs()
})
if r.Mux == nil { if r.Mux == nil {
r.Mux = http.NewServeMux() r.Mux = http.NewServeMux()
}
if r.SubRouters != nil {
for _, subRouter := range *r.SubRouters {
sr := subRouter.RegisterRoutes()
r.Mux.Handle(r.BasePath+subRouter.BasePath, sr)
}
} }
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 { for _, route := range r.Routes {
r.HandleAllRequestMethods(route, r.Mux) r.HandleAllRequestMethods(route, r.BasePath+route.Path, r.Mux)
} }
if r.Middleware != nil { if r.Middleware != nil {
@ -103,19 +95,13 @@ type RouteMapping struct {
} }
func (r *Router) GetFlatRouteList() []RouteMapping { func (r *Router) GetFlatRouteList() []RouteMapping {
var routes []RouteMapping routes := []RouteMapping{{Path: r.BasePath, Name: r.BaseRoute.Name}}
for _, route := range r.Routes { for _, route := range r.Routes {
routes = append(routes, RouteMapping{Path: r.BasePath + route.Path, Name: route.Name}) routes = append(routes, RouteMapping{Path: r.BasePath + route.Path, Name: route.Name})
} }
for _, subRouter := range r.SubRouters {
if r.SubRouters == nil {
return routes
}
for _, subRouter := range *r.SubRouters {
routes = append(routes, subRouter.GetFlatRouteList()...) routes = append(routes, subRouter.GetFlatRouteList()...)
} }
return routes return routes
} }
@ -125,7 +111,6 @@ func (r *Router) GetRouteByName(name string) (string, error) {
return route.Path, nil return route.Path, nil
} }
} }
return "", werror.Wrap(errors.New(name+" does not exist"), "Route not found") return "", werror.Wrap(errors.New(name+" does not exist"), "Route not found")
} }

View File

@ -16,21 +16,21 @@ var (
func NewRouter() *Router { func NewRouter() *Router {
rtrinstOnce.Do(func() { rtrinstOnce.Do(func() {
rtrinst = &Router{ rtrinst = &Router{
Mux: nil, Mux: nil,
BasePath: "/", BasePath: "/",
BaseRoute: Route{Controller: shared.HomePageController{}, Path: "", Name: "app.index"},
Routes: []Route{ Routes: []Route{
{Controller: shared.HomePageController{}, Path: "", Name: "app.index"},
{Controller: shared.SignUpHandler{}, Path: "sign-up", Name: "app.user.sign_up"}, {Controller: shared.SignUpHandler{}, Path: "sign-up", Name: "app.user.sign_up"},
{Controller: shared.SignInHandler{}, Path: "sign-in", Name: "app.user.sign_in"}, {Controller: shared.SignInHandler{}, Path: "sign-in", Name: "app.user.sign_in"},
{Controller: shared.SignOutHandler{}, Path: "sign-out", Name: "app.user.sign_out"}, {Controller: shared.SignOutHandler{}, Path: "sign-out", Name: "app.user.sign_out"},
{Controller: shared.PopulateHandler{}, Path: "populate", Name: "app.populate"}, {Controller: shared.PopulateHandler{}, Path: "populate", Name: "app.populate"},
}, },
SubRouters: &[]Router{ SubRouters: []*Router{
{ {
Mux: nil, Mux: nil,
BasePath: "/admin/", BasePath: "/admin/",
BaseRoute: Route{Controller: admin.IndexHandler{}, Path: "", Name: "app.admin.index"},
Routes: []Route{ Routes: []Route{
{Controller: admin.IndexHandler{}, Path: "", Name: "app.admin.index"},
{Controller: admin.IndexHandler{}, Path: "butt", Name: "app.admin.butt"}, {Controller: admin.IndexHandler{}, Path: "butt", Name: "app.admin.butt"},
}, },
SubRouters: nil, SubRouters: nil,