Skip to content

Commit 83881f3

Browse files
committed
Add request logging and proxy error handler
1 parent 7be10d5 commit 83881f3

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ func main() {
5757
return net.Dial("unix", *dockerSocket)
5858
},
5959
},
60+
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
61+
logger.Error("proxy error", zap.String("method", r.Method), zap.String("path", r.URL.Path), zap.Error(err))
62+
http.Error(w, err.Error(), http.StatusBadGateway)
63+
},
6064
}
6165

62-
handler := authMiddleware(logger, acl, servicesEditGuard, proxy)
66+
handler := requestLogger(logger, authMiddleware(logger, acl, servicesEditGuard, proxy))
6367

6468
server := &http.Server{
6569
Addr: *listen,
@@ -80,6 +84,13 @@ func main() {
8084
}
8185
}
8286

87+
func requestLogger(logger *zap.Logger, next http.Handler) http.Handler {
88+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
89+
logger.Info("request", zap.String("method", r.Method), zap.String("path", r.URL.Path), zap.String("upgrade", r.Header.Get("Upgrade")))
90+
next.ServeHTTP(w, r)
91+
})
92+
}
93+
8394
func authMiddleware(logger *zap.Logger, acl *internal.ACL, servicesEditGuard *guards.ServicesEdit, next http.Handler) http.Handler {
8495
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
8596
username := r.Header.Get("X-Docker-Auth-Username")

0 commit comments

Comments
 (0)