Skip to content

Commit 2436119

Browse files
committed
feat: use chi's httplogger for all request paths
1 parent 4898904 commit 2436119

File tree

5 files changed

+46
-56
lines changed

5 files changed

+46
-56
lines changed

.golangci.yml

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,44 @@
1+
version: "2"
12
run:
2-
deadline: 5m
33
allow-parallel-runners: true
4-
5-
issues:
6-
# don't skip warning about doc comments
7-
# don't exclude the default set of lint
8-
exclude-use-default: false
9-
# restore some of the defaults
10-
# (fill in the rest as needed)
11-
exclude-rules:
12-
- path: "api/*"
13-
linters:
14-
- lll
15-
- path: "internal/*"
16-
linters:
17-
- dupl
18-
- lll
194
linters:
20-
disable-all: true
5+
default: none
216
enable:
227
- dupl
238
- errcheck
249
- goconst
2510
- gocyclo
26-
- gofmt
27-
- goimports
28-
- gosimple
2911
- govet
3012
- ineffassign
3113
- lll
3214
- misspell
3315
- nakedret
3416
- prealloc
3517
- staticcheck
36-
- typecheck
3718
- unconvert
3819
- unparam
3920
- unused
21+
exclusions:
22+
generated: lax
23+
rules:
24+
- linters:
25+
- lll
26+
path: api/*
27+
- linters:
28+
- dupl
29+
- lll
30+
path: internal/*
31+
paths:
32+
- third_party$
33+
- builtin$
34+
- examples$
35+
formatters:
36+
enable:
37+
- gofmt
38+
- goimports
39+
exclusions:
40+
generated: lax
41+
paths:
42+
- third_party$
43+
- builtin$
44+
- examples$

cmd/main.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import (
1111
"log/slog"
1212
"net/http"
1313
"os"
14+
"time"
1415

1516
corev1 "k8s.io/api/core/v1"
1617
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1718

1819
"github.com/caarlos0/env/v11"
1920
"github.com/go-chi/chi"
21+
"github.com/go-chi/httplog/v2"
2022
"github.com/lestrrat-go/httprc/v3"
2123
"github.com/lestrrat-go/jwx/v3/jwk"
2224
api "github.com/statisticsnorway/labid/api/oas"
@@ -39,6 +41,8 @@ type config struct {
3941
TeamApiTokenUrl string `env:"TEAM_API_TOKEN_URL"`
4042

4143
Host string `env:"HOST,required,notEmpty"`
44+
45+
LogLevel slog.Level `env:"LOG_LEVEL" envDefault:"INFO"`
4246
}
4347

4448
func main() {
@@ -116,21 +120,22 @@ func main() {
116120
errorAndExit(fmt.Errorf("create token handler: %w", err))
117121
}
118122

119-
srv, err := api.NewServer(tokenHandler, api.WithMiddleware(token.Logging(log)))
123+
srv, err := api.NewServer(tokenHandler)
120124
if err != nil {
121125
errorAndExit(fmt.Errorf("create api server: %w", err))
122126
}
123127

128+
middlelog := httplog.NewLogger("labid", httplog.Options{
129+
LogLevel: cfg.LogLevel,
130+
JSON: true,
131+
QuietDownRoutes: []string{"/"},
132+
QuietDownPeriod: 10 * time.Second,
133+
})
134+
124135
r := chi.NewRouter()
136+
r.Use(httplog.RequestLogger(middlelog))
125137
r.Mount("/", srv)
126138
r.Group(func(r chi.Router) {
127-
r.Use(func(next http.Handler) http.Handler {
128-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
129-
log.Info("handle request", "method", r.Method, "pattern", r.Pattern)
130-
next.ServeHTTP(w, r)
131-
})
132-
})
133-
134139
jwks, err := Jwks(localJwks)
135140
if err != nil {
136141
errorAndExit(fmt.Errorf("create jwks handler: %w", err))
@@ -140,7 +145,7 @@ func main() {
140145
})
141146

142147
if err := http.ListenAndServe(fmt.Sprintf(":%s", cfg.Port), r); err != nil {
143-
slog.Error(err.Error())
148+
log.Error(err.Error())
144149
}
145150
}
146151

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.24.0
55
require (
66
github.com/caarlos0/env/v11 v11.3.1
77
github.com/go-chi/chi v1.5.5
8+
github.com/go-chi/httplog/v2 v2.1.1
89
github.com/go-faster/errors v0.7.1
910
github.com/go-faster/jx v1.1.0
1011
github.com/lestrrat-go/httprc/v3 v3.0.0-beta1
@@ -27,6 +28,7 @@ require (
2728
github.com/fatih/color v1.18.0 // indirect
2829
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
2930
github.com/ghodss/yaml v1.0.0 // indirect
31+
github.com/go-chi/chi/v5 v5.2.1 // indirect
3032
github.com/go-faster/yaml v0.4.6 // indirect
3133
github.com/go-logr/logr v1.4.2 // indirect
3234
github.com/go-logr/stdr v1.2.2 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
1919
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
2020
github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE=
2121
github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw=
22+
github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8=
23+
github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
24+
github.com/go-chi/httplog/v2 v2.1.1 h1:ojojiu4PIaoeJ/qAO4GWUxJqvYUTobeo7zmuHQJAxRk=
25+
github.com/go-chi/httplog/v2 v2.1.1/go.mod h1:/XXdxicJsp4BA5fapgIC3VuTD+z0Z/VzukoB3VDc1YE=
2226
github.com/go-faster/errors v0.7.1 h1:MkJTnDoEdi9pDabt1dpWf7AA8/BaSYZqibYyhZ20AYg=
2327
github.com/go-faster/errors v0.7.1/go.mod h1:5ySTjWFiphBs07IKuiL69nxdfd5+fzh1u7FPGZP2quo=
2428
github.com/go-faster/jx v1.1.0 h1:ZsW3wD+snOdmTDy9eIVgQdjUpXRRV4rqW8NS3t+20bg=

internal/token/logging.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)