diff --git a/go.mod b/go.mod index 99ff13a..a286945 100644 --- a/go.mod +++ b/go.mod @@ -3,16 +3,25 @@ module git.staur.ca/stobbsm/clustvirt go 1.22.1 require ( - github.com/a-h/templ v0.2.646 - github.com/go-chi/chi/v5 v5.0.12 + git.staur.ca/stobbsm/simplelog v0.0.0-20240407172718-26a74495adc9 + git.staur.ca/stobbsm/simpleroute v0.0.0-20240407195233-523f184fd406 + github.com/a-h/templ v0.2.648 github.com/jaypipes/pcidb v1.0.0 github.com/spf13/cobra v1.8.0 - libvirt.org/go/libvirt v1.10001.0 + libvirt.org/go/libvirt v1.10002.0 libvirt.org/go/libvirtxml v1.10001.0 ) require ( + github.com/go-chi/chi/v5 v5.0.12 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/mitchellh/go-homedir v1.0.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mitchellh/go-homedir v1.1.0 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/rs/zerolog v1.32.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + golang.org/x/sys v0.19.0 // indirect ) + +replace git.staur.ca/stobbsm/simpleroute => ../simpleroute diff --git a/go.sum b/go.sum index 8fee0ac..5a8b8aa 100644 --- a/go.sum +++ b/go.sum @@ -1,24 +1,44 @@ -github.com/a-h/templ v0.2.646 h1:F4+tGmPavtPMdWF3ofCDkPx/31qtInztsmUdzu6EzVU= -github.com/a-h/templ v0.2.646/go.mod h1:SA7mtYwVEajbIXFRh3vKdYm/4FYyLQAtPH1+KxzGPA8= +git.staur.ca/stobbsm/simplelog v0.0.0-20240407172718-26a74495adc9 h1:A3XQw1dxgNqAHWUzzz2zdAV8bWHIEvxRgF+JYo4/elc= +git.staur.ca/stobbsm/simplelog v0.0.0-20240407172718-26a74495adc9/go.mod h1:ruZaa6TgS/lJ4lC78nXHm4sb+zftJ0IwsEN7mvaksc8= +github.com/a-h/templ v0.2.648 h1:A1ggHGIE7AONOHrFaDTM8SrqgqHL6fWgWCijQ21Zy9I= +github.com/a-h/templ v0.2.648/go.mod h1:SA7mtYwVEajbIXFRh3vKdYm/4FYyLQAtPH1+KxzGPA8= +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s= github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jaypipes/pcidb v1.0.0 h1:vtZIfkiCUE42oYbJS0TAq9XSfSmcsgo9IdxSm9qzYU8= github.com/jaypipes/pcidb v1.0.0/go.mod h1:TnYUvqhPBzCKnH34KrIX22kAeEbDCSRJ9cqLRCuNDfk= -github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0= -github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0= +github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= +golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -libvirt.org/go/libvirt v1.10001.0 h1:lEVDNE7xfzmZXiDEGIS8NvJSuaz11OjRXw+ufbQEtPY= -libvirt.org/go/libvirt v1.10001.0/go.mod h1:1WiFE8EjZfq+FCVog+rvr1yatKbKZ9FaFMZgEqxEJqQ= +libvirt.org/go/libvirt v1.10002.0 h1:ZFQsv1G8HE8SYhLBqaOuxze6+f00x96khLwn54aWJnI= +libvirt.org/go/libvirt v1.10002.0/go.mod h1:1WiFE8EjZfq+FCVog+rvr1yatKbKZ9FaFMZgEqxEJqQ= libvirt.org/go/libvirtxml v1.10001.0 h1:r9WBs24r3mxIG3/hAMRRwDMy4ZaPHmhHjw72o/ceXic= libvirt.org/go/libvirtxml v1.10001.0/go.mod h1:7Oq2BLDstLr/XtoQD8Fr3mfDNrzlI3utYKySXF2xkng= diff --git a/lib/host/view.go b/lib/host/view.go deleted file mode 100644 index 3de8403..0000000 --- a/lib/host/view.go +++ /dev/null @@ -1,6 +0,0 @@ -package host - -import "github.com/go-chi/chi/v5" - -func (h *Host) HTMXRenderHandler(router *chi.Router) { -} diff --git a/router/htmx/routes.go b/router/htmx/routes.go index 67b820d..7f78461 100644 --- a/router/htmx/routes.go +++ b/router/htmx/routes.go @@ -10,70 +10,54 @@ import ( router "git.staur.ca/stobbsm/simpleroute" ) -var Htmx = router.Routes{ - { - 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() - } - } - }, - }, - { - 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(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() - } - } - }, - }, - { - 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(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() - } - } - }, - }, +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 } diff --git a/router/server/root.go b/router/server/root.go index 8f3450d..f1d4ed7 100644 --- a/router/server/root.go +++ b/router/server/root.go @@ -13,9 +13,9 @@ import ( func (s *Server) baseRoutes() { // fileserver for static content fs := http.StripPrefix("/static", http.FileServer(http.Dir("public"))) - s.mux.Handle("GET /static/*", fs) + s.r.Get("/static/*", fs.ServeHTTP) // Root defaults to the cluster view - s.mux.Handle("GET /", templ.Handler(layouts.Manager("ClustVirt", "libvirt made simple"))) - s.mux.Handle("GET /about", templ.Handler(static.About())) + s.r.Get("/", templ.Handler(layouts.Manager("ClustVirt", "libvirt made simple")).ServeHTTP) + s.r.Get("/about", templ.Handler(static.About()).ServeHTTP) } diff --git a/router/server/server.go b/router/server/server.go index 029fb96..4ccf38a 100644 --- a/router/server/server.go +++ b/router/server/server.go @@ -19,11 +19,10 @@ import ( // a good choice to reduce dependencies, but does mean middleware needs to be implemented // in the server type Server struct { - bindAddr string - ssl bool - c *cluster.Cluster - middleware []middleware.Middleware - mux *http.ServeMux + bindAddr string + ssl bool + c *cluster.Cluster + r *router.Router } // New creates a new HTTP Server instance. @@ -42,7 +41,7 @@ func (s *Server) Start() { Dur("upTime", time.Since(tstart)). Msg("http server stopped") }() - s.mux = http.NewServeMux() + s.r = router.New() indev, _ := os.LookupEnv("CLUSTVIRT_DEV") indev = strings.ToLower(indev) @@ -54,45 +53,20 @@ func (s *Server) Start() { case "yes": fallthrough case "on": - s.AddMiddleware(middleware.NoCache) - s.mux.Handle("GET /_/debug", middleware.Profiler()) + s.r.AddMiddleware(middleware.NoCache) } - s.AddMiddleware(middleware.Logger) + s.r.AddMiddleware(middleware.Logger) // Add routes s.baseRoutes() - if err := s.AddSubRouter("/htmx", htmx.Htmx); err != nil { + if err := s.r.AddSubRouter("/htmx", htmx.Routes(s.c)); err != nil { log.Error("server.Start"). Str("subroute", "htmx"). Err(err).Send() } // Start the server - if err := http.ListenAndServe(s.bindAddr, s); err != nil { + if err := http.ListenAndServe(s.bindAddr, s.r); err != nil { log.Error("router.Server.Start"). Str("bindaddr", s.bindAddr). Err(err).Send() } } - -// AddMiddleware appends middleware to the chi router -func (s *Server) AddMiddleware(m ...middleware.Middleware) { - s.middleware = append(s.middleware, m...) -} - -// AddSubRouter attachs a SubRouter using it's MountTo method. This method -// needs the path prefix and the defined routes -func (s *Server) AddSubRouter(pfx string, sr router.SubRouter) error { - return sr.MountTo(pfx, s.c, s.mux) -} - -// ServeHTTP implements the Handler interface -func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { - s.chain().ServeHTTP(w, r) -} - -func (s *Server) chain() http.Handler { - h := http.Handler(s.mux) - for i := range s.middleware { - h = s.middleware[len(s.middleware)-1-i](h) - } - return h -} diff --git a/view/cluster_templ.go b/view/cluster_templ.go index ba651be..15f8317 100644 --- a/view/cluster_templ.go +++ b/view/cluster_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.646 +// templ: version: v0.2.648 package view //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/view/components/anchor_templ.go b/view/components/anchor_templ.go index a7292c5..5393990 100644 --- a/view/components/anchor_templ.go +++ b/view/components/anchor_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.646 +// templ: version: v0.2.648 package components //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/view/components/contentlist_templ.go b/view/components/contentlist_templ.go index 30af8cf..cb0be0f 100644 --- a/view/components/contentlist_templ.go +++ b/view/components/contentlist_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.646 +// templ: version: v0.2.648 package components //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/view/components/contentp_templ.go b/view/components/contentp_templ.go index 3751a00..5f9820d 100644 --- a/view/components/contentp_templ.go +++ b/view/components/contentp_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.646 +// templ: version: v0.2.648 package components //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/view/components/flexrow_templ.go b/view/components/flexrow_templ.go index f3e8d6b..3e26a78 100644 --- a/view/components/flexrow_templ.go +++ b/view/components/flexrow_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.646 +// templ: version: v0.2.648 package components //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/view/components/hero_templ.go b/view/components/hero_templ.go index 1ad40df..a965a44 100644 --- a/view/components/hero_templ.go +++ b/view/components/hero_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.646 +// templ: version: v0.2.648 package components //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/view/components/hostbutton_templ.go b/view/components/hostbutton_templ.go index bae11d2..90cc907 100644 --- a/view/components/hostbutton_templ.go +++ b/view/components/hostbutton_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.646 +// templ: version: v0.2.648 package components //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/view/components/infobox_templ.go b/view/components/infobox_templ.go index 16b8027..e37a970 100644 --- a/view/components/infobox_templ.go +++ b/view/components/infobox_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.646 +// templ: version: v0.2.648 package components //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/view/components/navbar_templ.go b/view/components/navbar_templ.go index afe5c09..3334789 100644 --- a/view/components/navbar_templ.go +++ b/view/components/navbar_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.646 +// templ: version: v0.2.648 package components //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/view/hostinfo_templ.go b/view/hostinfo_templ.go index 06e7c21..b49bdaa 100644 --- a/view/hostinfo_templ.go +++ b/view/hostinfo_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.646 +// templ: version: v0.2.648 package view //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/view/layouts/manager_templ.go b/view/layouts/manager_templ.go index 7bf0f4a..89569b5 100644 --- a/view/layouts/manager_templ.go +++ b/view/layouts/manager_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.646 +// templ: version: v0.2.648 package layouts //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/view/layouts/staticpage_templ.go b/view/layouts/staticpage_templ.go index 8c8032c..cc84ddd 100644 --- a/view/layouts/staticpage_templ.go +++ b/view/layouts/staticpage_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.646 +// templ: version: v0.2.648 package layouts //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/view/static/home_templ.go b/view/static/home_templ.go index bd93566..0f152f2 100644 --- a/view/static/home_templ.go +++ b/view/static/home_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.646 +// templ: version: v0.2.648 package static //lint:file-ignore SA4006 This context is only used if a nested component is present.