Compare commits

..

3 commits

Author SHA1 Message Date
4a906473de update log msg 2024-10-10 19:08:06 +02:00
de3e535f81 update dependencies 2024-10-10 19:08:06 +02:00
a69a527a81 add logging 2024-10-10 19:04:51 +02:00
3 changed files with 49 additions and 7 deletions

9
go.mod
View file

@ -4,18 +4,19 @@ go 1.23.1
require (
github.com/carlmjohnson/requests v0.24.2
github.com/danielgtaylor/huma/v2 v2.22.1
github.com/danielgtaylor/huma/v2 v2.23.0
github.com/go-chi/chi/v5 v5.1.0
github.com/go-chi/cors v1.2.1
github.com/google/uuid v1.6.0
github.com/jarcoal/httpmock v1.3.1
golang.org/x/text v0.18.0
golang.org/x/text v0.19.0
gotest.tools/v3 v3.5.1
)
require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/net v0.30.0 // indirect
)

9
go.sum
View file

@ -1,8 +1,11 @@
github.com/carlmjohnson/requests v0.24.2 h1:JDakhAmTIKL/qL/1P7Kkc2INGBJIkIFP6xUeUmPzLso=
github.com/carlmjohnson/requests v0.24.2/go.mod h1:duYA/jDnyZ6f3xbcF5PpZ9N8clgopubP2nK5i6MVMhU=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/danielgtaylor/huma/v2 v2.22.1 h1:fXhyjGSj5u5VeI+laa+e+7OxiQsP9RC55/tWZZvI4YA=
github.com/danielgtaylor/huma/v2 v2.22.1/go.mod h1:2NZmGf/A+SstJYQlq0Xp4nsTDCmPvKS2w9vI8c9sf1A=
github.com/danielgtaylor/huma/v2 v2.23.0 h1:0Q3Mq+KTYr6shFqx3gQulDTVwR9xa6/SmSmbDJCRyMI=
github.com/danielgtaylor/huma/v2 v2.23.0/go.mod h1:2NZmGf/A+SstJYQlq0Xp4nsTDCmPvKS2w9vI8c9sf1A=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
@ -24,14 +27,20 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
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/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

38
main.go
View file

@ -9,6 +9,8 @@ import (
"github.com/danielgtaylor/huma/v2/humacli"
"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
"github.com/google/uuid"
"log/slog"
"net/http"
"time"
)
@ -53,10 +55,12 @@ func addRoutes(api huma.API, glConfig GrandLyonConfig, now func() time.Time) {
}) (*stopOutput, error) {
passages, err := getPassages(ctx, glConfig, now, input.StopID)
if errors.Is(err, errNoPassageFound) {
slog.ErrorContext(ctx, "passage not found", getRequestIDAttr(ctx))
return nil, huma.NewError(http.StatusNotFound, "no passage found")
}
if err != nil {
slog.ErrorContext(ctx, "error getting passages", "err", err, getRequestIDAttr(ctx))
return nil, err
}
@ -68,10 +72,12 @@ func addRoutes(api huma.API, glConfig GrandLyonConfig, now func() time.Time) {
}) (*velovOutput, error) {
station, err := getStation(ctx, glConfig.Client, input.StationID)
if errors.Is(err, errStationNotFound) {
slog.ErrorContext(ctx, "station not found", getRequestIDAttr(ctx))
return nil, huma.NewError(http.StatusNotFound, "station not found")
}
if err != nil {
slog.ErrorContext(ctx, "error getting station", "err", err, getRequestIDAttr(ctx))
return nil, err
}
@ -94,6 +100,8 @@ func main() {
Handler: router,
}
api.UseMiddleware(logging)
glConfig := GrandLyonConfig{
Username: options.GrandLyonUsername,
Password: options.GrandLyonPassword,
@ -102,9 +110,9 @@ func main() {
addRoutes(api, glConfig, time.Now)
hooks.OnStart(func() {
fmt.Printf("Starting server on %s...\n", server.Addr)
slog.Info("Starting server", "addr", server.Addr)
if err := server.ListenAndServe(); err != nil {
fmt.Printf("Error running server: %s\n", err)
slog.Error("Error running server", "err", err)
}
})
@ -112,10 +120,34 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
fmt.Printf("Error shutting down server: %s\n", err)
slog.ErrorContext(ctx, "Error shutting down server", "err", err)
}
})
})
cli.Run()
}
type contextKey string
const requestIDKey contextKey = "request_id"
func logging(ctx huma.Context, next func(huma.Context)) {
reqID := uuid.New().String()
ctx = huma.WithValue(ctx, requestIDKey, reqID)
start := time.Now()
next(ctx)
elapsed := time.Since(start)
slog.InfoContext(ctx.Context(), "received request",
"method", ctx.Method(),
"path", ctx.URL().Path,
"status", ctx.Status(),
"duration", elapsed,
getRequestIDAttr(ctx.Context()),
)
}
func getRequestIDAttr(ctx context.Context) slog.Attr {
val, _ := ctx.Value(requestIDKey).(string)
return slog.String("request_id", val)
}