aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/Xe/ln/ex/http.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/Xe/ln/ex/http.go')
-rw-r--r--vendor/github.com/Xe/ln/ex/http.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/vendor/github.com/Xe/ln/ex/http.go b/vendor/github.com/Xe/ln/ex/http.go
new file mode 100644
index 0000000..c5715a3
--- /dev/null
+++ b/vendor/github.com/Xe/ln/ex/http.go
@@ -0,0 +1,36 @@
+package ex
+
+import (
+ "net"
+ "net/http"
+ "time"
+
+ "github.com/Xe/ln"
+)
+
+func HTTPLog(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ host, _, _ := net.SplitHostPort(r.RemoteAddr)
+ f := ln.F{
+ "remote_ip": host,
+ "x_forwarded_for": r.Header.Get("X-Forwarded-For"),
+ "path": r.URL.Path,
+ }
+ ctx := ln.WithF(r.Context(), f)
+ st := time.Now()
+
+ next.ServeHTTP(w, r.WithContext(ctx))
+
+ af := time.Now()
+ f["request_duration"] = af.Sub(st)
+
+ ws, ok := w.(interface {
+ Status() int
+ })
+ if ok {
+ f["status"] = ws.Status()
+ }
+
+ ln.Log(r.Context(), f)
+ })
+}