2024-03-10 07:15:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-03-17 06:10:04 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-03-10 07:15:49 +00:00
|
|
|
"log"
|
2024-03-14 01:12:48 +00:00
|
|
|
"net/http"
|
2024-03-12 01:39:17 +00:00
|
|
|
|
2024-03-16 17:09:58 +00:00
|
|
|
"git.staur.ca/stobbsm/clustvirt/lib/host"
|
2024-03-17 06:10:04 +00:00
|
|
|
"git.staur.ca/stobbsm/clustvirt/view"
|
2024-03-16 04:50:24 +00:00
|
|
|
"git.staur.ca/stobbsm/clustvirt/view/components"
|
2024-03-15 17:55:28 +00:00
|
|
|
"git.staur.ca/stobbsm/clustvirt/view/static"
|
2024-03-16 04:50:24 +00:00
|
|
|
"github.com/a-h/templ"
|
2024-03-14 01:12:48 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2024-03-10 07:15:49 +00:00
|
|
|
)
|
|
|
|
|
2024-03-14 01:12:48 +00:00
|
|
|
const DEBUG bool = true
|
|
|
|
|
2024-03-10 07:15:49 +00:00
|
|
|
func main() {
|
|
|
|
log.Println("Starting clustvirt, the libvirt cluster manager")
|
|
|
|
|
2024-03-14 01:12:48 +00:00
|
|
|
// Start webserver and serve homepage
|
|
|
|
|
2024-03-16 04:50:24 +00:00
|
|
|
defaultNavBar := []components.NavItem{
|
2024-03-19 04:38:28 +00:00
|
|
|
{Name: "Cluster", Href: "/" },
|
|
|
|
{Name: "Configuration", Href: "/config" },
|
2024-03-16 04:50:24 +00:00
|
|
|
{Name: "About", Href: "/about"},
|
|
|
|
}
|
2024-03-14 01:12:48 +00:00
|
|
|
fs := http.StripPrefix("/static/", http.FileServer(http.Dir("public")))
|
|
|
|
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Use(middleware.Logger)
|
2024-03-16 17:09:58 +00:00
|
|
|
if DEBUG {
|
|
|
|
r.Use(middleware.NoCache)
|
|
|
|
}
|
2024-03-14 01:12:48 +00:00
|
|
|
r.Get("/static/*", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
fs.ServeHTTP(w, r)
|
|
|
|
})
|
2024-03-17 06:10:04 +00:00
|
|
|
r.Get("/", templ.Handler(view.HostMain(defaultNavBar)).ServeHTTP)
|
2024-03-16 04:50:24 +00:00
|
|
|
r.Get("/about", templ.Handler(static.Home()).ServeHTTP)
|
2024-03-17 06:10:04 +00:00
|
|
|
|
|
|
|
r.Route("/htmx", func(r chi.Router) {
|
|
|
|
r.Get("/host/{hostname}", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
rhost, err := host.ConnectHost(host.URI_QEMU_SSH_SYSTEM, chi.URLParam(r, "hostname"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("error while getting host: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rhost.Close()
|
|
|
|
log.Println("Rendering HostInfo", view.HostInfo(rhost).Render(context.Background(), w))
|
|
|
|
})
|
2024-03-19 04:38:28 +00:00
|
|
|
r.Get("/host/{hostname}/stats", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
rhost, err := host.ConnectHost(host.URI_QEMU_SSH_SYSTEM, chi.URLParam(r, "hostname"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("error while getting host: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rhost.Close()
|
|
|
|
log.Println("Rendering stats", view.SysInfo(rhost).Render(context.Background(), w))
|
|
|
|
})
|
2024-03-17 06:10:04 +00:00
|
|
|
})
|
2024-03-14 01:12:48 +00:00
|
|
|
|
|
|
|
log.Println(http.ListenAndServe(":3000", r))
|
2024-03-10 07:15:49 +00:00
|
|
|
}
|