2024-03-24 04:05:06 +00:00
|
|
|
// 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"
|
|
|
|
)
|
|
|
|
|
2024-03-24 05:59:56 +00:00
|
|
|
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" }
|
2024-03-24 04:05:06 +00:00
|
|
|
|
2024-03-24 05:59:56 +00:00
|
|
|
func (h htmx) MountTo(c *cluster.Cluster, mux *http.ServeMux) error {
|
2024-03-24 04:05:06 +00:00
|
|
|
var errs []error
|
2024-03-24 05:59:56 +00:00
|
|
|
for _, r := range h {
|
2024-03-24 04:05:06 +00:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodTrace:
|
2024-03-24 05:59:56 +00:00
|
|
|
mux.Handle(trace(prefix(h.Prefix(), r.Path)), r.Handler(c))
|
2024-03-24 04:05:06 +00:00
|
|
|
case http.MethodOptions:
|
2024-03-24 05:59:56 +00:00
|
|
|
mux.Handle(options(prefix(h.Prefix(), r.Path)), r.Handler(c))
|
2024-03-24 04:05:06 +00:00
|
|
|
case http.MethodConnect:
|
2024-03-24 05:59:56 +00:00
|
|
|
mux.Handle(connect(prefix(h.Prefix(), r.Path)), r.Handler(c))
|
2024-03-24 04:05:06 +00:00
|
|
|
case http.MethodHead:
|
2024-03-24 05:59:56 +00:00
|
|
|
mux.Handle(head(prefix(h.Prefix(), r.Path)), r.Handler(c))
|
2024-03-24 04:05:06 +00:00
|
|
|
case http.MethodGet:
|
2024-03-24 05:59:56 +00:00
|
|
|
mux.Handle(get(prefix(h.Prefix(), r.Path)), r.Handler(c))
|
2024-03-24 04:05:06 +00:00
|
|
|
case http.MethodPost:
|
2024-03-24 05:59:56 +00:00
|
|
|
mux.Handle(post(prefix(h.Prefix(), r.Path)), r.Handler(c))
|
2024-03-24 04:05:06 +00:00
|
|
|
case http.MethodPut:
|
2024-03-24 05:59:56 +00:00
|
|
|
mux.Handle(put(prefix(h.Prefix(), r.Path)), r.Handler(c))
|
2024-03-24 04:05:06 +00:00
|
|
|
case http.MethodPatch:
|
2024-03-24 05:59:56 +00:00
|
|
|
mux.Handle(patch(prefix(h.Prefix(), r.Path)), r.Handler(c))
|
2024-03-24 04:05:06 +00:00
|
|
|
case http.MethodDelete:
|
2024-03-24 05:59:56 +00:00
|
|
|
mux.Handle(delete(prefix(h.Prefix(), r.Path)), r.Handler(c))
|
2024-03-24 04:05:06 +00:00
|
|
|
default:
|
2024-03-24 05:59:56 +00:00
|
|
|
mux.Handle(prefix(h.Prefix(), r.Path), r.Handler(c))
|
2024-03-24 04:05:06 +00:00
|
|
|
}
|
|
|
|
log.Info("htmx.MoutnTo").
|
|
|
|
Str("Route.Path", r.Path).
|
|
|
|
Str("Route.Method", r.Method).
|
|
|
|
Msg("route registered")
|
|
|
|
}
|
|
|
|
return errors.Join(errs...)
|
|
|
|
}
|