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:
parent
4c132c4abf
commit
77c34481ce
@ -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...)
|
||||
}
|
||||
|
@ -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",
|
||||
|
@ -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
|
||||
|
57
router/routes.go
Normal file
57
router/routes.go
Normal 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...)
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user