Skip to content

Commit eb519e2

Browse files
authored
Fix yaml struct tag, add request logging (#5)
* Fix struct tag: json -> yaml on ACL.Deployers * Add request logging and proxy error handler
1 parent 6ec39ff commit eb519e2

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

internal/acl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type ACLDeployer struct {
1919
}
2020

2121
type ACL struct {
22-
Deployers []*ACLDeployer `json:"deployers"`
22+
Deployers []*ACLDeployer `yaml:"deployers"`
2323
}
2424

2525
func NewACL(aclFilePath string) *ACL {

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)