This commit is contained in:
parent
9a98117384
commit
a8e7d407fe
3
go.mod
3
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
|
||||
|
|
2
go.sum
2
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=
|
||||
|
|
11
http.go
11
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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
package middleware
|
||||
|
||||
// Recoverer2 is a reimplementation of recoverer but using slog as the backend.
|
|
@ -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
|
|
@ -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,
|
||||
|
|
|
@ -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 }}
|
||||
|
|
Loading…
Reference in a new issue