package main import ( "context" "fmt" "log" "net/http" "git.staur.ca/stobbsm/clustvirt/cluster" "git.staur.ca/stobbsm/clustvirt/lib/host" "git.staur.ca/stobbsm/clustvirt/view" "git.staur.ca/stobbsm/clustvirt/view/components" "git.staur.ca/stobbsm/clustvirt/view/static" "github.com/a-h/templ" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" localmw "git.staur.ca/stobbsm/clustvirt/router/middleware" ) const DEBUG bool = true func main() { log.Println("Starting clustvirt, the libvirt cluster manager") clst := cluster.New(). AddHost("earth.staur.ca"). AddHost("venus.staur.ca"). AddHost("mars.staur.ca"). Build() initStats := cluster.InitStats(clst) // Start webserver and serve homepage defaultNavBar := []components.NavItem{ {Name: "Cluster", Href: "/"}, {Name: "Configuration", Href: "/config"}, {Name: "About", Href: "/about"}, } fs := http.StripPrefix("/static/", http.FileServer(http.Dir("public"))) r := chi.NewRouter() r.Use(localmw.Logger) if DEBUG { r.Use(middleware.NoCache) } r.Get("/static/*", func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() fs.ServeHTTP(w, r) }) r.Get("/", templ.Handler(view.HostMain(defaultNavBar)).ServeHTTP) r.Get("/cluster", func(w http.ResponseWriter, r *http.Request) { initStats.Update() log.Println( "Rendering clusterstats", view.ClusterInfo( initStats, initStats.Diff(), defaultNavBar, ).Render(context.Background(), w)) }) r.Get("/about", templ.Handler(static.Home()).ServeHTTP) 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)) }) 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)) }) }) log.Println(http.ListenAndServe(":3000", r)) }