moving some modules to their own thing

- lib/log is now git.staur.ca/stobbsm/simplelog
- router is now git.staur.ca/stobbsm/simpleroute
This commit is contained in:
Matthew Stobbs 2024-04-07 00:00:59 -06:00
parent f0cf97c125
commit 1113062d9e
21 changed files with 46 additions and 188 deletions

View File

@ -4,7 +4,7 @@ import (
"time" "time"
"git.staur.ca/stobbsm/clustvirt/lib/host" "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 // ClusterBuilder is used to build a Cluster object, which can then be used

View File

@ -1,8 +1,8 @@
package cluster package cluster
import ( import (
"git.staur.ca/stobbsm/clustvirt/lib/log"
"git.staur.ca/stobbsm/clustvirt/lib/storagepool" "git.staur.ca/stobbsm/clustvirt/lib/storagepool"
log "git.staur.ca/stobbsm/simplelog"
) )
// ClusterStats is used to gather stats for the entire cluster // ClusterStats is used to gather stats for the entire cluster

View File

@ -10,7 +10,7 @@
package cmd package cmd
import ( import (
"git.staur.ca/stobbsm/clustvirt/lib/log" log "git.staur.ca/stobbsm/simplelog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )

View File

@ -14,7 +14,7 @@ import (
"strconv" "strconv"
"syscall" "syscall"
"git.staur.ca/stobbsm/clustvirt/lib/log" log "git.staur.ca/stobbsm/simplelog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )

View File

@ -11,7 +11,7 @@ package cmd
import ( import (
"git.staur.ca/stobbsm/clustvirt/daemon" "git.staur.ca/stobbsm/clustvirt/daemon"
"git.staur.ca/stobbsm/clustvirt/lib/log" log "git.staur.ca/stobbsm/simplelog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"git.staur.ca/stobbsm/clustvirt/lib/log" log "git.staur.ca/stobbsm/simplelog"
) )
func Start() { func Start() {

View File

@ -5,7 +5,7 @@ import (
"os" "os"
"syscall" "syscall"
"git.staur.ca/stobbsm/clustvirt/lib/log" log "git.staur.ca/stobbsm/simplelog"
) )
// path to store the PID, configurable // path to store the PID, configurable

30
go.work.sum Normal file
View File

@ -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=

View File

@ -5,8 +5,8 @@ import (
"errors" "errors"
"time" "time"
"git.staur.ca/stobbsm/clustvirt/lib/log"
"git.staur.ca/stobbsm/clustvirt/util" "git.staur.ca/stobbsm/clustvirt/util"
log "git.staur.ca/stobbsm/simplelog"
"libvirt.org/go/libvirt" "libvirt.org/go/libvirt"
) )

View File

@ -10,7 +10,7 @@ import (
"sync" "sync"
"git.staur.ca/stobbsm/clustvirt/lib/guest" "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/libvirt"
"libvirt.org/go/libvirtxml" "libvirt.org/go/libvirtxml"
) )

View File

@ -3,7 +3,7 @@ package host
import ( import (
"strings" "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 // URI is a string type, accessed via the pre-defined variables, and represent

View File

@ -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")

View File

@ -5,9 +5,9 @@ import (
"net/http" "net/http"
"git.staur.ca/stobbsm/clustvirt/cluster" "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" "git.staur.ca/stobbsm/clustvirt/view"
log "git.staur.ca/stobbsm/simplelog"
router "git.staur.ca/stobbsm/simpleroute"
) )
var Htmx = router.Routes{ var Htmx = router.Routes{

View File

@ -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
)

View File

@ -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)
})
}

View File

@ -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

View File

@ -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)
})
}

View File

@ -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

View File

@ -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...)
}

View File

@ -8,10 +8,10 @@ import (
"time" "time"
"git.staur.ca/stobbsm/clustvirt/cluster" "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/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 // Server represents an HTTP server that uses net/http ServeMux to route requests

View File

@ -1,7 +1,7 @@
package util package util
import ( import (
"git.staur.ca/stobbsm/clustvirt/lib/log" log "git.staur.ca/stobbsm/simplelog"
"github.com/jaypipes/pcidb" "github.com/jaypipes/pcidb"
) )