Skip to content

Commit

Permalink
feat: add log requests middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
dorianneto committed Sep 12, 2024
1 parent f8dd047 commit 422c1a1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ module github.com/dorianneto/media-metadata
go 1.23.0

require gopkg.in/vansante/go-ffprobe.v2 v2.2.0

require github.com/justinas/alice v1.2.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/justinas/alice v1.2.0 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo=
github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA=
gopkg.in/vansante/go-ffprobe.v2 v2.2.0 h1:iuOqTsbfYuqIz4tAU9NWh22CmBGxlGHdgj4iqP+NUmY=
gopkg.in/vansante/go-ffprobe.v2 v2.2.0/go.mod h1:qF0AlAjk7Nqzqf3y333Ly+KxN3cKF2JqA3JT5ZheUGE=
17 changes: 17 additions & 0 deletions middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import "net/http"

func (app *application) logRequests(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var (
ip = r.RemoteAddr
method = r.Method
uri = r.URL.RequestURI()
)

app.logger.Info("received request", "ip", ip, "method", method, "URI", uri)

next.ServeHTTP(w, r)
})
}
6 changes: 5 additions & 1 deletion routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"net/http"

"github.com/justinas/alice"
)

func (app *application) routes() http.Handler {
Expand All @@ -10,5 +12,7 @@ func (app *application) routes() http.Handler {
mux.HandleFunc("GET /{$}", app.healthCheckHandler)
mux.HandleFunc("POST /metadata", app.metadataHandler)

return mux
middlewares := alice.New(app.logRequests)

return middlewares.Then(mux)
}

0 comments on commit 422c1a1

Please sign in to comment.