From 77c34481ceef436a356b67631d0205cbea0d44e4 Mon Sep 17 00:00:00 2001 From: Matthew Stobbs Date: Sun, 24 Mar 2024 19:38:46 -0600 Subject: [PATCH] reworked how routes are defined - instead of having to make a new type for each set of routes, now we just need to define the routes as an exported variable. - TODO: add a mechanism to add routes externally to the server setup --- router/htmx/htmx.go | 58 ----------------------------------------- router/htmx/routes.go | 3 ++- router/router.go | 6 ++--- router/routes.go | 57 ++++++++++++++++++++++++++++++++++++++++ router/server/server.go | 9 ++++--- 5 files changed, 66 insertions(+), 67 deletions(-) create mode 100644 router/routes.go diff --git a/router/htmx/htmx.go b/router/htmx/htmx.go index 5abe647..c2489ee 100644 --- a/router/htmx/htmx.go +++ b/router/htmx/htmx.go @@ -1,60 +1,2 @@ // Package htmx contains the routes for the WebUI HTMX package htmx - -import ( - "errors" - "fmt" - "net/http" - - "git.staur.ca/stobbsm/clustvirt/cluster" - "git.staur.ca/stobbsm/clustvirt/lib/log" - "git.staur.ca/stobbsm/clustvirt/router" -) - -type htmx []router.Route - -func prefix(b, p string) string { return fmt.Sprintf("%s%s", b, p)} -func trace(p string) string { return fmt.Sprintf("TRACE %s", p) } -func options(p string) string { return fmt.Sprintf("OPTIONS %s", p) } -func connect(p string) string { return fmt.Sprintf("CONNECT %s", p) } -func head(p string) string { return fmt.Sprintf("HEAD %s", p) } -func get(p string) string { return fmt.Sprintf("GET %s", p) } -func post(p string) string { return fmt.Sprintf("POST %s", p) } -func put(p string) string { return fmt.Sprintf("PUT %s", p) } -func patch(p string) string { return fmt.Sprintf("PATCH %s", p) } -func delete(p string) string { return fmt.Sprintf("DELETE %s", p) } - -func (h htmx) Prefix() string { return "/htmx" } - -func (h htmx) MountTo(c *cluster.Cluster, mux *http.ServeMux) error { - var errs []error - for _, r := range h { - switch r.Method { - case http.MethodTrace: - mux.Handle(trace(prefix(h.Prefix(), r.Path)), r.Handler(c)) - case http.MethodOptions: - mux.Handle(options(prefix(h.Prefix(), r.Path)), r.Handler(c)) - case http.MethodConnect: - mux.Handle(connect(prefix(h.Prefix(), r.Path)), r.Handler(c)) - case http.MethodHead: - mux.Handle(head(prefix(h.Prefix(), r.Path)), r.Handler(c)) - case http.MethodGet: - mux.Handle(get(prefix(h.Prefix(), r.Path)), r.Handler(c)) - case http.MethodPost: - mux.Handle(post(prefix(h.Prefix(), r.Path)), r.Handler(c)) - case http.MethodPut: - mux.Handle(put(prefix(h.Prefix(), r.Path)), r.Handler(c)) - case http.MethodPatch: - mux.Handle(patch(prefix(h.Prefix(), r.Path)), r.Handler(c)) - case http.MethodDelete: - mux.Handle(delete(prefix(h.Prefix(), r.Path)), r.Handler(c)) - default: - mux.Handle(prefix(h.Prefix(), r.Path), r.Handler(c)) - } - log.Info("htmx.MoutnTo"). - Str("Route.Path", r.Path). - Str("Route.Method", r.Method). - Msg("route registered") - } - return errors.Join(errs...) -} diff --git a/router/htmx/routes.go b/router/htmx/routes.go index 32cfaf8..cb52b8b 100644 --- a/router/htmx/routes.go +++ b/router/htmx/routes.go @@ -6,10 +6,11 @@ import ( "git.staur.ca/stobbsm/clustvirt/cluster" "git.staur.ca/stobbsm/clustvirt/lib/log" + "git.staur.ca/stobbsm/clustvirt/router" "git.staur.ca/stobbsm/clustvirt/view" ) -var Htmx = htmx{ +var Htmx = router.Routes{ { Method: http.MethodGet, Path: "/cluster", diff --git a/router/router.go b/router/router.go index 5f1d75a..1f46893 100644 --- a/router/router.go +++ b/router/router.go @@ -11,10 +11,8 @@ import ( // SubRouter defines an interface to be able to add a subrouter to a // chi router type SubRouter interface { - // MountTo needs the cluster handle and the mux to add to - MountTo(*cluster.Cluster, *http.ServeMux) error - // Prefix returns the path prefix - Prefix() string + // MountTo needs the path prefix, cluster handle and the mux to add to + MountTo(string, *cluster.Cluster, *http.ServeMux) error } // Route defines a route that should be added to a chi router or diff --git a/router/routes.go b/router/routes.go new file mode 100644 index 0000000..badc8f9 --- /dev/null +++ b/router/routes.go @@ -0,0 +1,57 @@ +package router + +import ( + "errors" + "fmt" + "net/http" + + "git.staur.ca/stobbsm/clustvirt/cluster" + "git.staur.ca/stobbsm/clustvirt/lib/log" +) + +type Routes []Route + +func addprefix(b, p string) string { return fmt.Sprintf("%s%s", b, p) } +func trace(p string) string { return fmt.Sprintf("TRACE %s", p) } +func options(p string) string { return fmt.Sprintf("OPTIONS %s", p) } +func connect(p string) string { return fmt.Sprintf("CONNECT %s", p) } +func head(p string) string { return fmt.Sprintf("HEAD %s", p) } +func get(p string) string { return fmt.Sprintf("GET %s", p) } +func post(p string) string { return fmt.Sprintf("POST %s", p) } +func put(p string) string { return fmt.Sprintf("PUT %s", p) } +func patch(p string) string { return fmt.Sprintf("PATCH %s", p) } +func delete(p string) string { return fmt.Sprintf("DELETE %s", p) } + +func (rte Routes) MountTo(prefix string, c *cluster.Cluster, mux *http.ServeMux) error { + var errs []error + for _, r := range rte { + switch r.Method { + case http.MethodTrace: + mux.Handle(trace(addprefix(prefix, r.Path)), r.Handler(c)) + case http.MethodOptions: + mux.Handle(options(addprefix(prefix, r.Path)), r.Handler(c)) + case http.MethodConnect: + mux.Handle(connect(addprefix(prefix, r.Path)), r.Handler(c)) + case http.MethodHead: + mux.Handle(head(addprefix(prefix, r.Path)), r.Handler(c)) + case http.MethodGet: + mux.Handle(get(addprefix(prefix, r.Path)), r.Handler(c)) + case http.MethodPost: + mux.Handle(post(addprefix(prefix, r.Path)), r.Handler(c)) + case http.MethodPut: + mux.Handle(put(addprefix(prefix, r.Path)), r.Handler(c)) + case http.MethodPatch: + mux.Handle(patch(addprefix(prefix, r.Path)), r.Handler(c)) + case http.MethodDelete: + mux.Handle(delete(addprefix(prefix, r.Path)), r.Handler(c)) + default: + mux.Handle(addprefix(prefix, r.Path), r.Handler(c)) + } + log.Info("Routes.MoutnTo"). + Str("prefix", prefix). + Str("Route.Path", r.Path). + Str("Route.Method", r.Method). + Msg("route registered") + } + return errors.Join(errs...) +} diff --git a/router/server/server.go b/router/server/server.go index c80917a..375aac5 100644 --- a/router/server/server.go +++ b/router/server/server.go @@ -60,7 +60,7 @@ func (s *Server) Start() { s.AddMiddleware(middleware.Logger) // Add routes s.baseRoutes() - if err := s.AddSubRouter(htmx.Htmx); err != nil { + if err := s.AddSubRouter("/htmx", htmx.Htmx); err != nil { log.Error("server.Start"). Str("subroute", "htmx"). Err(err).Send() @@ -78,9 +78,10 @@ func (s *Server) AddMiddleware(m ...middleware.Middleware) { s.middleware = append(s.middleware, m...) } -// AddSubRouter attachs a SubRouter using it's MountTo method -func (s *Server) AddSubRouter(sr router.SubRouter) error { - return sr.MountTo(s.c, s.mux) +// AddSubRouter attachs a SubRouter using it's MountTo method. This method +// needs the path prefix and the defined routes +func (s *Server) AddSubRouter(pfx string, sr router.SubRouter) error { + return sr.MountTo(pfx, s.c, s.mux) } // ServeHTTP implements the Handler interface