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
This commit is contained in:
Matthew Stobbs 2024-03-24 19:38:46 -06:00
parent 4c132c4abf
commit 77c34481ce
5 changed files with 66 additions and 67 deletions

View File

@ -1,60 +1,2 @@
// Package htmx contains the routes for the WebUI HTMX // Package htmx contains the routes for the WebUI HTMX
package 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...)
}

View File

@ -6,10 +6,11 @@ import (
"git.staur.ca/stobbsm/clustvirt/cluster" "git.staur.ca/stobbsm/clustvirt/cluster"
"git.staur.ca/stobbsm/clustvirt/lib/log" "git.staur.ca/stobbsm/clustvirt/lib/log"
"git.staur.ca/stobbsm/clustvirt/router"
"git.staur.ca/stobbsm/clustvirt/view" "git.staur.ca/stobbsm/clustvirt/view"
) )
var Htmx = htmx{ var Htmx = router.Routes{
{ {
Method: http.MethodGet, Method: http.MethodGet,
Path: "/cluster", Path: "/cluster",

View File

@ -11,10 +11,8 @@ import (
// SubRouter defines an interface to be able to add a subrouter to a // SubRouter defines an interface to be able to add a subrouter to a
// chi router // chi router
type SubRouter interface { type SubRouter interface {
// MountTo needs the cluster handle and the mux to add to // MountTo needs the path prefix, cluster handle and the mux to add to
MountTo(*cluster.Cluster, *http.ServeMux) error MountTo(string, *cluster.Cluster, *http.ServeMux) error
// Prefix returns the path prefix
Prefix() string
} }
// Route defines a route that should be added to a chi router or // Route defines a route that should be added to a chi router or

57
router/routes.go Normal file
View File

@ -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...)
}

View File

@ -60,7 +60,7 @@ func (s *Server) Start() {
s.AddMiddleware(middleware.Logger) s.AddMiddleware(middleware.Logger)
// Add routes // Add routes
s.baseRoutes() s.baseRoutes()
if err := s.AddSubRouter(htmx.Htmx); err != nil { if err := s.AddSubRouter("/htmx", htmx.Htmx); err != nil {
log.Error("server.Start"). log.Error("server.Start").
Str("subroute", "htmx"). Str("subroute", "htmx").
Err(err).Send() Err(err).Send()
@ -78,9 +78,10 @@ func (s *Server) AddMiddleware(m ...middleware.Middleware) {
s.middleware = append(s.middleware, m...) s.middleware = append(s.middleware, m...)
} }
// AddSubRouter attachs a SubRouter using it's MountTo method // AddSubRouter attachs a SubRouter using it's MountTo method. This method
func (s *Server) AddSubRouter(sr router.SubRouter) error { // needs the path prefix and the defined routes
return sr.MountTo(s.c, s.mux) func (s *Server) AddSubRouter(pfx string, sr router.SubRouter) error {
return sr.MountTo(pfx, s.c, s.mux)
} }
// ServeHTTP implements the Handler interface // ServeHTTP implements the Handler interface