From 1113062d9e91722241e52aafae305a6ee30e24f6 Mon Sep 17 00:00:00 2001 From: Matthew Stobbs Date: Sun, 7 Apr 2024 00:00:59 -0600 Subject: [PATCH] moving some modules to their own thing - lib/log is now git.staur.ca/stobbsm/simplelog - router is now git.staur.ca/stobbsm/simpleroute --- cluster/builder.go | 2 +- cluster/stats.go | 2 +- cmd/clusterctl/cmd/daemon.go | 2 +- cmd/clusterctl/cmd/daemon_restart.go | 2 +- cmd/clusterctl/cmd/daemon_start.go | 2 +- daemon/control.go | 2 +- daemon/main.go | 2 +- go.work.sum | 30 +++++++++++++++ lib/guest/lib.go | 2 +- lib/host/lib.go | 2 +- lib/host/uri.go | 2 +- router/errors.go | 6 --- router/htmx/routes.go | 4 +- router/middleware/fromchi.go | 22 ----------- router/middleware/logger.go | 30 --------------- router/middleware/middleware.go | 7 ---- router/middleware/nocache.go | 22 ----------- router/router.go | 28 -------------- router/routes.go | 57 ---------------------------- router/server/server.go | 6 +-- util/pcidb.go | 2 +- 21 files changed, 46 insertions(+), 188 deletions(-) create mode 100644 go.work.sum delete mode 100644 router/errors.go delete mode 100644 router/middleware/fromchi.go delete mode 100644 router/middleware/logger.go delete mode 100644 router/middleware/middleware.go delete mode 100644 router/middleware/nocache.go delete mode 100644 router/router.go delete mode 100644 router/routes.go diff --git a/cluster/builder.go b/cluster/builder.go index 7b0a617..412f1c8 100644 --- a/cluster/builder.go +++ b/cluster/builder.go @@ -4,7 +4,7 @@ import ( "time" "git.staur.ca/stobbsm/clustvirt/lib/host" - "git.staur.ca/stobbsm/clustvirt/lib/log" + log "git.staur.ca/stobbsm/simplelog" ) // ClusterBuilder is used to build a Cluster object, which can then be used diff --git a/cluster/stats.go b/cluster/stats.go index 093118b..143556e 100644 --- a/cluster/stats.go +++ b/cluster/stats.go @@ -1,8 +1,8 @@ package cluster import ( - "git.staur.ca/stobbsm/clustvirt/lib/log" "git.staur.ca/stobbsm/clustvirt/lib/storagepool" + log "git.staur.ca/stobbsm/simplelog" ) // ClusterStats is used to gather stats for the entire cluster diff --git a/cmd/clusterctl/cmd/daemon.go b/cmd/clusterctl/cmd/daemon.go index f065e82..ebc1a19 100644 --- a/cmd/clusterctl/cmd/daemon.go +++ b/cmd/clusterctl/cmd/daemon.go @@ -10,7 +10,7 @@ package cmd import ( - "git.staur.ca/stobbsm/clustvirt/lib/log" + log "git.staur.ca/stobbsm/simplelog" "github.com/spf13/cobra" ) diff --git a/cmd/clusterctl/cmd/daemon_restart.go b/cmd/clusterctl/cmd/daemon_restart.go index e1a83e2..4c64915 100644 --- a/cmd/clusterctl/cmd/daemon_restart.go +++ b/cmd/clusterctl/cmd/daemon_restart.go @@ -14,7 +14,7 @@ import ( "strconv" "syscall" - "git.staur.ca/stobbsm/clustvirt/lib/log" + log "git.staur.ca/stobbsm/simplelog" "github.com/spf13/cobra" ) diff --git a/cmd/clusterctl/cmd/daemon_start.go b/cmd/clusterctl/cmd/daemon_start.go index 721d067..0935089 100644 --- a/cmd/clusterctl/cmd/daemon_start.go +++ b/cmd/clusterctl/cmd/daemon_start.go @@ -11,7 +11,7 @@ package cmd import ( "git.staur.ca/stobbsm/clustvirt/daemon" - "git.staur.ca/stobbsm/clustvirt/lib/log" + log "git.staur.ca/stobbsm/simplelog" "github.com/spf13/cobra" ) diff --git a/daemon/control.go b/daemon/control.go index be2d569..d8cc4aa 100644 --- a/daemon/control.go +++ b/daemon/control.go @@ -4,7 +4,7 @@ import ( "fmt" "os" - "git.staur.ca/stobbsm/clustvirt/lib/log" + log "git.staur.ca/stobbsm/simplelog" ) func Start() { diff --git a/daemon/main.go b/daemon/main.go index b798264..0ed5d2c 100644 --- a/daemon/main.go +++ b/daemon/main.go @@ -5,7 +5,7 @@ import ( "os" "syscall" - "git.staur.ca/stobbsm/clustvirt/lib/log" + log "git.staur.ca/stobbsm/simplelog" ) // path to store the PID, configurable diff --git a/go.work.sum b/go.work.sum new file mode 100644 index 0000000..03561f9 --- /dev/null +++ b/go.work.sum @@ -0,0 +1,30 @@ +github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ= +github.com/a-h/htmlformat v0.0.0-20231108124658-5bd994fe268e/go.mod h1:FMIm5afKmEfarNbIXOaPHFY8X7fo+fRQB6I9MPG2nB0= +github.com/a-h/lexical v0.0.53/go.mod h1:d73jw5cgKXuYypRozNBuxRNFrTWQ3y5hVMG7rUjh1Qw= +github.com/a-h/parse v0.0.0-20240121214402-3caf7543159a/go.mod h1:3mnrkvGpurZ4ZrTDbYU84xhwXW2TjTKShSwjRi2ihfQ= +github.com/a-h/pathvars v0.0.12/go.mod h1:7rLTtvDVyKneR/N65hC0lh2sZ2KRyAmWFaOvv00uxb0= +github.com/a-h/protocol v0.0.0-20230224160810-b4eec67c1c22/go.mod h1:Gm0KywveHnkiIhqFSMZglXwWZRQICg3KDWLYdglv/d8= +github.com/a-h/templ v0.2.646/go.mod h1:SA7mtYwVEajbIXFRh3vKdYm/4FYyLQAtPH1+KxzGPA8= +github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= +github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= +github.com/cli/browser v1.2.0/go.mod h1:xFFnXLVcAyW9ni0cuo6NnrbCP75JxJ0RO7VtCBiH/oI= +github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/natefinch/atomic v1.0.1/go.mod h1:N/D/ELrljoqDyT3rZrsUmtsuzvHkeB/wWjHV22AZRbM= +github.com/rs/cors v1.8.3/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= +github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs= +github.com/segmentio/encoding v0.3.6/go.mod h1:n0JeuIqEQrQoPDGsjo8UNd1iA0U8d8+oHAA4E3G3OxM= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +go.lsp.dev/jsonrpc2 v0.10.0/go.mod h1:fmEzIdXPi/rf6d4uFcayi8HpFP1nBF99ERP1htC72Ac= +go.lsp.dev/pkg v0.0.0-20210717090340-384b27a52fb2/go.mod h1:gtSHRuYfbCT0qnbLnovpie/WEmqyJ7T4n6VXiFMBtcw= +go.lsp.dev/uri v0.3.0/go.mod h1:P5sbO1IQR+qySTWOCnhnK7phBx+W3zbLqSMDJNTw88I= +go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= +go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= +go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= diff --git a/lib/guest/lib.go b/lib/guest/lib.go index 0f0f728..8060cbc 100644 --- a/lib/guest/lib.go +++ b/lib/guest/lib.go @@ -5,8 +5,8 @@ import ( "errors" "time" - "git.staur.ca/stobbsm/clustvirt/lib/log" "git.staur.ca/stobbsm/clustvirt/util" + log "git.staur.ca/stobbsm/simplelog" "libvirt.org/go/libvirt" ) diff --git a/lib/host/lib.go b/lib/host/lib.go index a370b22..54696a4 100644 --- a/lib/host/lib.go +++ b/lib/host/lib.go @@ -10,7 +10,7 @@ import ( "sync" "git.staur.ca/stobbsm/clustvirt/lib/guest" - "git.staur.ca/stobbsm/clustvirt/lib/log" + log "git.staur.ca/stobbsm/simplelog" "libvirt.org/go/libvirt" "libvirt.org/go/libvirtxml" ) diff --git a/lib/host/uri.go b/lib/host/uri.go index 109344e..a492cab 100644 --- a/lib/host/uri.go +++ b/lib/host/uri.go @@ -3,7 +3,7 @@ package host import ( "strings" - "git.staur.ca/stobbsm/clustvirt/lib/log" + log "git.staur.ca/stobbsm/simplelog" ) // URI is a string type, accessed via the pre-defined variables, and represent diff --git a/router/errors.go b/router/errors.go deleted file mode 100644 index 6e5ac9a..0000000 --- a/router/errors.go +++ /dev/null @@ -1,6 +0,0 @@ -package router - -import "errors" - -// Errors uses by the router module and it's sub modules -var ErrMethodNotExist = errors.New("http method doesn't exist") diff --git a/router/htmx/routes.go b/router/htmx/routes.go index cb52b8b..67b820d 100644 --- a/router/htmx/routes.go +++ b/router/htmx/routes.go @@ -5,9 +5,9 @@ import ( "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" + log "git.staur.ca/stobbsm/simplelog" + router "git.staur.ca/stobbsm/simpleroute" ) var Htmx = router.Routes{ diff --git a/router/middleware/fromchi.go b/router/middleware/fromchi.go deleted file mode 100644 index 934aa9c..0000000 --- a/router/middleware/fromchi.go +++ /dev/null @@ -1,22 +0,0 @@ -package middleware - -import "github.com/go-chi/chi/v5/middleware" - -// These middlewares are cloned from chi/middleware -var ( - AllowContentEncoding = middleware.AllowContentEncoding - AllowContentType = middleware.AllowContentType - BasicAuth = middleware.BasicAuth - Compress = middleware.Compress - Heartbeat = middleware.Heartbeat - //NoCache = middleware.NoCache - Profiler = middleware.Profiler - RealIP = middleware.RealIP - Recoverer = middleware.Recoverer - RequestID = middleware.RequestID - SetHeader = middleware.SetHeader - Sunset = middleware.Sunset - Throttle = middleware.Throttle - Timeout = middleware.Timeout - URLFormat = middleware.URLFormat -) diff --git a/router/middleware/logger.go b/router/middleware/logger.go deleted file mode 100644 index c52f0b5..0000000 --- a/router/middleware/logger.go +++ /dev/null @@ -1,30 +0,0 @@ -package middleware - -import ( - "net/http" - "time" - - "git.staur.ca/stobbsm/clustvirt/lib/log" -) - -// Logger uses the in package log module to handle route logging -func Logger(next http.Handler) http.Handler { - log.Info("logger.Logger").Str("middleware", "Logger").Msg("middleware loaded") - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - start := time.Now() - defer func() { - log.Info("router.middleware.Logger"). - Str("httpMethod", r.Method). - Str("host", r.Host). - Str("uri", r.URL.RequestURI()). - Str("user_agent", r.UserAgent()). - Str("referer", r.Referer()). - Strs("transfer-encoding", r.TransferEncoding). - Int64("length", r.ContentLength). - Dur("elasped_ms", time.Since(start)). - Msg("incoming connection") - }() - - next.ServeHTTP(w, r) - }) -} diff --git a/router/middleware/middleware.go b/router/middleware/middleware.go deleted file mode 100644 index 3f81324..0000000 --- a/router/middleware/middleware.go +++ /dev/null @@ -1,7 +0,0 @@ -package middleware - -import "net/http" - -// Middleware is a function that is meant to be chained with other functions -// through an http request lifecycle -type Middleware func(http.Handler) http.Handler diff --git a/router/middleware/nocache.go b/router/middleware/nocache.go deleted file mode 100644 index e55f419..0000000 --- a/router/middleware/nocache.go +++ /dev/null @@ -1,22 +0,0 @@ -package middleware - -import ( - "net/http" - "time" - - "git.staur.ca/stobbsm/clustvirt/lib/log" -) - -// NoCache adds headers indicating the browser shouldn't cache -// any of the responses. Useful for debugging, should not be used -// on production -func NoCache(next http.Handler) http.Handler { - log.Info("router.middleware").Str("middleware", "NoCache").Msg("middleware loaded") - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Cache-Control", "no-cache, no-store, no-transform, must-revalidate, private, max-age=0") - w.Header().Add("Expires", time.Unix(0, 0).UTC().Format(http.TimeFormat)) - w.Header().Add("Pragma", "no-cache") - - next.ServeHTTP(w, r) - }) -} diff --git a/router/router.go b/router/router.go deleted file mode 100644 index 1f46893..0000000 --- a/router/router.go +++ /dev/null @@ -1,28 +0,0 @@ -package router - -import ( - "net/http" - - "git.staur.ca/stobbsm/clustvirt/cluster" -) - -// Types that are shared among routers - -// SubRouter defines an interface to be able to add a subrouter to a -// chi router -type SubRouter interface { - // MountTo needs the path prefix, cluster handle and the mux to add to - MountTo(string, *cluster.Cluster, *http.ServeMux) error -} - -// Route defines a route that should be added to a chi router or -// subrouter -type Route struct { - Method string - Path string - Handler RouteHandler -} - -// RouteHandler gets the active cluster for the server to inject into -// the http.HandlerFunc that is then returned -type RouteHandler func(*cluster.Cluster) http.HandlerFunc diff --git a/router/routes.go b/router/routes.go deleted file mode 100644 index badc8f9..0000000 --- a/router/routes.go +++ /dev/null @@ -1,57 +0,0 @@ -package router - -import ( - "errors" - "fmt" - "net/http" - - "git.staur.ca/stobbsm/clustvirt/cluster" - "git.staur.ca/stobbsm/clustvirt/lib/log" -) - -type Routes []Route - -func addprefix(b, p string) string { return fmt.Sprintf("%s%s", b, p) } -func trace(p string) string { return fmt.Sprintf("TRACE %s", p) } -func options(p string) string { return fmt.Sprintf("OPTIONS %s", p) } -func connect(p string) string { return fmt.Sprintf("CONNECT %s", p) } -func head(p string) string { return fmt.Sprintf("HEAD %s", p) } -func get(p string) string { return fmt.Sprintf("GET %s", p) } -func post(p string) string { return fmt.Sprintf("POST %s", p) } -func put(p string) string { return fmt.Sprintf("PUT %s", p) } -func patch(p string) string { return fmt.Sprintf("PATCH %s", p) } -func delete(p string) string { return fmt.Sprintf("DELETE %s", p) } - -func (rte Routes) MountTo(prefix string, c *cluster.Cluster, mux *http.ServeMux) error { - var errs []error - for _, r := range rte { - switch r.Method { - case http.MethodTrace: - mux.Handle(trace(addprefix(prefix, r.Path)), r.Handler(c)) - case http.MethodOptions: - mux.Handle(options(addprefix(prefix, r.Path)), r.Handler(c)) - case http.MethodConnect: - mux.Handle(connect(addprefix(prefix, r.Path)), r.Handler(c)) - case http.MethodHead: - mux.Handle(head(addprefix(prefix, r.Path)), r.Handler(c)) - case http.MethodGet: - mux.Handle(get(addprefix(prefix, r.Path)), r.Handler(c)) - case http.MethodPost: - mux.Handle(post(addprefix(prefix, r.Path)), r.Handler(c)) - case http.MethodPut: - mux.Handle(put(addprefix(prefix, r.Path)), r.Handler(c)) - case http.MethodPatch: - mux.Handle(patch(addprefix(prefix, r.Path)), r.Handler(c)) - case http.MethodDelete: - mux.Handle(delete(addprefix(prefix, r.Path)), r.Handler(c)) - default: - mux.Handle(addprefix(prefix, r.Path), r.Handler(c)) - } - log.Info("Routes.MoutnTo"). - Str("prefix", prefix). - Str("Route.Path", r.Path). - Str("Route.Method", r.Method). - Msg("route registered") - } - return errors.Join(errs...) -} diff --git a/router/server/server.go b/router/server/server.go index 375aac5..029fb96 100644 --- a/router/server/server.go +++ b/router/server/server.go @@ -8,10 +8,10 @@ import ( "time" "git.staur.ca/stobbsm/clustvirt/cluster" - "git.staur.ca/stobbsm/clustvirt/lib/log" - "git.staur.ca/stobbsm/clustvirt/router" "git.staur.ca/stobbsm/clustvirt/router/htmx" - "git.staur.ca/stobbsm/clustvirt/router/middleware" + log "git.staur.ca/stobbsm/simplelog" + router "git.staur.ca/stobbsm/simpleroute" + "git.staur.ca/stobbsm/simpleroute/middleware" ) // Server represents an HTTP server that uses net/http ServeMux to route requests diff --git a/util/pcidb.go b/util/pcidb.go index 0681701..575a27b 100644 --- a/util/pcidb.go +++ b/util/pcidb.go @@ -1,7 +1,7 @@ package util import ( - "git.staur.ca/stobbsm/clustvirt/lib/log" + log "git.staur.ca/stobbsm/simplelog" "github.com/jaypipes/pcidb" )