From a8e7d407fe23fac4093d2e729225e9dfdda23442 Mon Sep 17 00:00:00 2001 From: saji Date: Wed, 28 Feb 2024 14:10:40 -0600 Subject: [PATCH] fix up static check issues --- go.mod | 3 +- go.sum | 2 - http.go | 11 ++--- internal/logparsers/parsers.go | 2 +- internal/middleware/recoverer2.go | 3 -- internal/middleware/slogger.go | 72 ------------------------------- skylab/skylab_gen.go | 8 +--- skylab/templates/golang.go.tmpl | 6 +-- 8 files changed, 11 insertions(+), 96 deletions(-) delete mode 100644 internal/middleware/recoverer2.go delete mode 100644 internal/middleware/slogger.go diff --git a/go.mod b/go.mod index f13b138..672a032 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/kschamplin/gotelem -go 1.20 +go 1.22 require ( github.com/go-chi/chi/v5 v5.0.12 @@ -9,7 +9,6 @@ require ( github.com/mattn/go-sqlite3 v1.14.22 github.com/urfave/cli/v2 v2.25.1 go.bug.st/serial v1.5.0 - golang.org/x/exp v0.0.0-20240213143201-ec583247a57a golang.org/x/sync v0.1.0 golang.org/x/sys v0.7.0 gopkg.in/yaml.v3 v3.0.1 diff --git a/go.sum b/go.sum index 33c444a..af5cd21 100644 --- a/go.sum +++ b/go.sum @@ -33,8 +33,6 @@ github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRT github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= go.bug.st/serial v1.5.0 h1:ThuUkHpOEmCVXxGEfpoExjQCS2WBVV4ZcUKVYInM9T4= go.bug.st/serial v1.5.0/go.mod h1:UABfsluHAiaNI+La2iESysd9Vetq7VRdpxvjx7CmmOE= -golang.org/x/exp v0.0.0-20240213143201-ec583247a57a h1:HinSgX1tJRX3KsL//Gxynpw5CTOAIPhgL4W8PNiIpVE= -golang.org/x/exp v0.0.0-20240213143201-ec583247a57a/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= diff --git a/http.go b/http.go index be68cee..4aa6f5c 100644 --- a/http.go +++ b/http.go @@ -19,10 +19,6 @@ import ( "nhooyr.io/websocket/wsjson" ) -type slogHttpLogger struct { - slog.Logger -} - func TelemRouter(log *slog.Logger, broker *Broker, db *db.TelemDb) http.Handler { r := chi.NewRouter() @@ -165,7 +161,8 @@ func apiV1GetValues(db *db.TelemDb) http.HandlerFunc { if startString != "" { start, err = time.Parse(time.RFC3339, startString) if err != nil { - + // error out + panic("hi") } } end := time.Now().Add(1 * time.Hour) @@ -173,6 +170,7 @@ func apiV1GetValues(db *db.TelemDb) http.HandlerFunc { if endParam != "" { end, err = time.Parse(time.RFC3339, endParam) if err != nil { + panic("hi") } } name := chi.URLParam(r, "name") @@ -186,6 +184,9 @@ func apiV1GetValues(db *db.TelemDb) http.HandlerFunc { fmt.Print(err) } b, err := json.Marshal(res) + if err != nil { + panic(err) + } w.Write(b) } diff --git a/internal/logparsers/parsers.go b/internal/logparsers/parsers.go index 459f890..0d816c6 100644 --- a/internal/logparsers/parsers.go +++ b/internal/logparsers/parsers.go @@ -23,7 +23,7 @@ func (e *FormatError) Error() string { if e.err != nil { return fmt.Sprintf("%s:%s", e.msg, e.err.Error()) } - return fmt.Sprintf("%s", e.msg) + return e.msg } func (e *FormatError) Unwrap() error { diff --git a/internal/middleware/recoverer2.go b/internal/middleware/recoverer2.go deleted file mode 100644 index 9aab6da..0000000 --- a/internal/middleware/recoverer2.go +++ /dev/null @@ -1,3 +0,0 @@ -package middleware - -// Recoverer2 is a reimplementation of recoverer but using slog as the backend. diff --git a/internal/middleware/slogger.go b/internal/middleware/slogger.go deleted file mode 100644 index 00c526f..0000000 --- a/internal/middleware/slogger.go +++ /dev/null @@ -1,72 +0,0 @@ -package middleware - -import ( - "context" - "net/http" - "time" - - "log/slog" - - chi_middleware "github.com/go-chi/chi/v5/middleware" -) - -// Slogger is a slog-enabled logging middleware. -// It logs the start and end of the request, and logs info -// about the request itself, response status, and response time. - -// Slogger returns a log handler that uses the given slog logger as the base. -func Slogger(sl *slog.Logger) func(next http.Handler) http.Handler { - - logger := sl.WithGroup("http") - return func(next http.Handler) http.Handler { - - // this triple-nested function is strange, but basically the Slogger() call makes a new middleware function (above) - // the middleware function returns a handler that calls the next handler in the chain(wrapping it) - - fn := func(w http.ResponseWriter, r *http.Request) { - // wrap writer allows us to get info on the response from further handlers. - ww := chi_middleware.NewWrapResponseWriter(w, r.ProtoMajor) - t1 := time.Now() - // attrs is stored to allow for the helpers to add additional elements to the main record. - attrs := make([]slog.Attr, 0) - - // This function runs at the end and adds all the response details to the attrs before logging them. - defer func() { - attrs = append(attrs, slog.Int("status_code", ww.Status())) - attrs = append(attrs, slog.Int("resp_size", ww.BytesWritten())) - attrs = append(attrs, slog.Duration("duration", time.Since(t1))) - attrs = append(attrs, slog.String("method", r.Method)) - logger.LogAttrs(r.Context(), slog.LevelInfo, r.RequestURI, attrs...) - - }() - - // embed the logger and the attrs for later items in the chain. - ctx := context.WithValue(r.Context(), SloggerAttrsKey, attrs) - ctx = context.WithValue(ctx, SloggerLogKey, logger) - // push it to the request and serve the next handler - r = r.WithContext(ctx) - next.ServeHTTP(ww, r) - } - - return http.HandlerFunc(fn) - } -} - -type slogKeyType int - -const ( - SloggerLogKey slogKeyType = iota - SloggerAttrsKey -) - -func AddSlogAttr(r *http.Request, attr slog.Attr) { - ctx := r.Context() - attrs, ok := ctx.Value(SloggerAttrsKey).([]slog.Attr) - if !ok { - return - } - attrs = append(attrs, attr) - -} - -// TODO: write rest of functions diff --git a/skylab/skylab_gen.go b/skylab/skylab_gen.go index 96e1191..008e608 100644 --- a/skylab/skylab_gen.go +++ b/skylab/skylab_gen.go @@ -1,4 +1,4 @@ -// generated by gen_skylab.go at 2024-02-27 19:26:47.373116343 -0600 CST m=+0.002925968 DO NOT EDIT! +// generated by gen_skylab.go at 2024-02-28 14:10:10.252960174 -0600 CST m=+0.003082874 DO NOT EDIT! package skylab @@ -104,11 +104,7 @@ const ( WslSlipSpeedMeasurementId SkylabId = 0x117 ) -var nameToIdMap = map[string]can.CanID{ - -} - -// list of every packet ID. can be used for O(1) checks. +// list of every packet ID. Can be used for O(1) checks. var idMap = map[can.CanID]bool{ { Id: 0x10, Extended: false }: true, diff --git a/skylab/templates/golang.go.tmpl b/skylab/templates/golang.go.tmpl index b8b8034..08bcf92 100644 --- a/skylab/templates/golang.go.tmpl +++ b/skylab/templates/golang.go.tmpl @@ -100,11 +100,7 @@ const ( {{- end}} ) -var nameToIdMap = map[string]can.CanID{ - -} - -// list of every packet ID. can be used for O(1) checks. +// list of every packet ID. Can be used for O(1) checks. var idMap = map[can.CanID]bool{ {{ range $p := .Packets -}} {{ if $p.Repeat }}