clustvirt/router/htmx/htmx.go
Matthew Stobbs 4c132c4abf removed chi router
- switched to go 1.22 net/http ServeMux for routing
- seems to be working really well
2024-03-23 23:59:56 -06:00

61 lines
2.1 KiB
Go

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