2024-03-21 22:56:24 +00:00
|
|
|
package htmx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Htmx = &htmx{
|
|
|
|
router: chi.NewRouter(),
|
|
|
|
routes: []router.Route{
|
2024-03-23 17:34:50 +00:00
|
|
|
{
|
|
|
|
Method: http.MethodGet,
|
|
|
|
Path: "/cluster",
|
|
|
|
Handler: func(c *cluster.Cluster) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := view.ClusterInfo(c).Render(context.Background(), w); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
log.Error("htmx.Handler").
|
|
|
|
Str("uri", r.RequestURI).
|
|
|
|
Err(err).
|
|
|
|
Int("statusCode", http.StatusBadRequest).Send()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-03-21 22:56:24 +00:00
|
|
|
Method: http.MethodGet,
|
|
|
|
Path: "/host/{hostname}",
|
|
|
|
Handler: func(c *cluster.Cluster) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
host, err := c.GetHost(chi.URLParam(r, "hostname"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
log.Error("htmx.Handler").
|
|
|
|
Str("uri", r.RequestURI).
|
|
|
|
Err(err).
|
|
|
|
Int("statusCode", http.StatusBadRequest).
|
|
|
|
Send()
|
|
|
|
}
|
|
|
|
if err = view.HostInfo(host).Render(context.Background(), w); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
log.Error("htmx.Handler").
|
|
|
|
Str("uri", r.RequestURI).
|
|
|
|
Err(err).
|
|
|
|
Int("statusCode", http.StatusInternalServerError).
|
|
|
|
Send()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2024-03-23 17:34:50 +00:00
|
|
|
{
|
2024-03-21 22:56:24 +00:00
|
|
|
Method: http.MethodGet,
|
|
|
|
Path: "/host/{hostname}/stats",
|
|
|
|
Handler: func(c *cluster.Cluster) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
host, err := c.GetHost(chi.URLParam(r, "hostname"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
log.Error("htmx.Handler").
|
|
|
|
Str("uri", r.RequestURI).
|
|
|
|
Err(err).
|
|
|
|
Int("statusCode", http.StatusBadRequest).
|
|
|
|
Send()
|
|
|
|
}
|
|
|
|
if err = view.HostStats(host).Render(context.Background(), w); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
log.Error("htmx.Handler").
|
|
|
|
Str("uri", r.RequestURI).
|
|
|
|
Err(err).
|
|
|
|
Int("statusCode", http.StatusInternalServerError).
|
|
|
|
Send()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|