Skip to content

Commit 8e40979

Browse files
author
Onur Menal
committed
Json and Logging middleware
1 parent 97c2789 commit 8e40979

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ require (
99
github.com/jmoiron/sqlx v1.3.5 // indirect
1010
github.com/lib/pq v1.10.6 // indirect
1111
github.com/satori/go.uuid v1.2.0 // indirect
12+
github.com/sirupsen/logrus v1.8.1 // indirect
1213
)

Diff for: go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,7 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB
967967
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
968968
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
969969
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
970+
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
970971
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
971972
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
972973
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
@@ -1392,6 +1393,7 @@ golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBc
13921393
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13931394
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13941395
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1396+
golang.org/x/sys v0.0.0-20220317061510-51cd9980dadf h1:Fm4IcnUL803i92qDlmB0obyHmosDrxZWxJL3gIeNqOw=
13951397
golang.org/x/sys v0.0.0-20220317061510-51cd9980dadf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13961398
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
13971399
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=

Diff for: internal/transport/http/handler.go

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ func NewHandler(service CommentService) *Handler {
2525
h.Router = mux.NewRouter()
2626
h.mapRoutes()
2727

28+
h.Router.Use(JSONMiddleware)
29+
h.Router.Use(LoggingMiddleware)
30+
2831
h.Server = &http.Server{
2932
Addr: "0.0.0.0:8080",
3033
Handler: h.Router,

Diff for: internal/transport/http/middleware.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package http
2+
3+
import (
4+
"net/http"
5+
6+
log "github.com/sirupsen/logrus"
7+
)
8+
9+
func JSONMiddleware(next http.Handler) http.Handler {
10+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
11+
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
12+
13+
next.ServeHTTP(w, r)
14+
})
15+
}
16+
17+
func LoggingMiddleware(next http.Handler) http.Handler {
18+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
19+
log.WithFields(
20+
log.Fields{
21+
"method": r.Method,
22+
"path": r.URL.Path,
23+
}).Info("handled request")
24+
})
25+
}

0 commit comments

Comments
 (0)