Dependency updates, logging middleware.
This commit is contained in:
@@ -29,7 +29,7 @@ func (app *App) handlePub(pattern string, fn handlerFunc) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
app.mux.HandleFunc(pattern, wrapped)
|
app.mux.HandleFunc(pattern, withLogging(wrapped))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) handleNotSignedIn(pattern string, fn handlerFunc) {
|
func (app *App) handleNotSignedIn(pattern string, fn handlerFunc) {
|
||||||
|
|||||||
47
hub/middleware.go
Normal file
47
hub/middleware.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package hub
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _log = log.New(os.Stderr, "", 0)
|
||||||
|
|
||||||
|
type responseWriterWrapper struct {
|
||||||
|
http.ResponseWriter
|
||||||
|
httpStatus int
|
||||||
|
responseSize int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *responseWriterWrapper) WriteHeader(status int) {
|
||||||
|
w.httpStatus = status
|
||||||
|
w.ResponseWriter.WriteHeader(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *responseWriterWrapper) Write(b []byte) (int, error) {
|
||||||
|
if w.httpStatus == 0 {
|
||||||
|
w.httpStatus = 200
|
||||||
|
}
|
||||||
|
w.responseSize += len(b)
|
||||||
|
return w.ResponseWriter.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func withLogging(inner http.HandlerFunc) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
t := time.Now()
|
||||||
|
wrapper := responseWriterWrapper{w, 0, 0}
|
||||||
|
|
||||||
|
inner(&wrapper, r)
|
||||||
|
_log.Printf("%s \"%s %s %s\" %d %d %v\n",
|
||||||
|
r.RemoteAddr,
|
||||||
|
r.Method,
|
||||||
|
r.URL.Path,
|
||||||
|
r.Proto,
|
||||||
|
wrapper.httpStatus,
|
||||||
|
wrapper.responseSize,
|
||||||
|
time.Since(t),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user