clustvirt/router/htmx/routes.go

64 lines
1.8 KiB
Go
Raw Permalink Normal View History

package htmx
import (
"context"
"net/http"
"git.staur.ca/stobbsm/clustvirt/cluster"
"git.staur.ca/stobbsm/clustvirt/view"
log "git.staur.ca/stobbsm/simplelog"
router "git.staur.ca/stobbsm/simpleroute"
)
func Routes(c *cluster.Cluster) *router.Router {
r := router.New()
r.Get("/cluster", 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()
}
})
r.Get("/host/{hostname}", func(w http.ResponseWriter, r *http.Request) {
host, err := c.GetHost(r.PathValue("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()
}
})
r.Get("/host/{hostname}/stats", func(w http.ResponseWriter, r *http.Request) {
host, err := c.GetHost(r.PathValue("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()
}
})
return r
}