55 lines
1.3 KiB
Go
55 lines
1.3 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"
|
||
|
"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...)
|
||
|
}
|