2024-03-21 22:56:24 +00:00
|
|
|
// Package htmx contains the routes for the WebUI HTMX
|
2024-03-21 17:15:23 +00:00
|
|
|
package htmx
|
|
|
|
|
2024-03-21 22:56:24 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2024-03-21 17:15:23 +00:00
|
|
|
|
2024-03-21 22:56:24 +00:00
|
|
|
"git.staur.ca/stobbsm/clustvirt/cluster"
|
|
|
|
"git.staur.ca/stobbsm/clustvirt/lib/log"
|
|
|
|
"git.staur.ca/stobbsm/clustvirt/router"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
)
|
|
|
|
|
|
|
|
type htmx struct {
|
|
|
|
router chi.Router
|
|
|
|
routes []router.Route
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *htmx) MountTo(c *cluster.Cluster, rr chi.Router) error {
|
|
|
|
var errs []error
|
|
|
|
for _, r := range h.routes {
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodTrace:
|
|
|
|
h.router.Trace(r.Path, r.Handler(c))
|
|
|
|
case http.MethodOptions:
|
|
|
|
h.router.Options(r.Path, r.Handler(c))
|
|
|
|
case http.MethodConnect:
|
|
|
|
h.router.Connect(r.Path, r.Handler(c))
|
|
|
|
case http.MethodHead:
|
|
|
|
h.router.Head(r.Path, r.Handler(c))
|
|
|
|
case http.MethodGet:
|
|
|
|
h.router.Get(r.Path, r.Handler(c))
|
|
|
|
case http.MethodPost:
|
|
|
|
h.router.Post(r.Path, r.Handler(c))
|
|
|
|
case http.MethodPut:
|
|
|
|
h.router.Put(r.Path, r.Handler(c))
|
|
|
|
case http.MethodPatch:
|
|
|
|
h.router.Patch(r.Path, r.Handler(c))
|
|
|
|
case http.MethodDelete:
|
|
|
|
h.router.Delete(r.Path, r.Handler(c))
|
|
|
|
default:
|
|
|
|
err := fmt.Errorf("method: %s: %w", r.Method, router.ErrMethodNotExist)
|
|
|
|
errs = append(errs, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Info("htmx.MoutnTo").
|
|
|
|
Str("Route.Path", r.Path).
|
|
|
|
Str("Route.Method", r.Method).
|
|
|
|
Msg("route registered")
|
|
|
|
}
|
|
|
|
rr.Mount("/htmx", h.router)
|
|
|
|
return errors.Join(errs...)
|
|
|
|
}
|