diff --git a/README.md b/README.md index 20ad056..862d456 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,11 @@ A simple service to proxy unix sockets to http. Useful for containerizing web services that don't natively support listening on unix sockets. Usage of unixproxy: + -log + Log requests to stdout + -strip string + Strip prefix from reqeusts -unix string Unix socket to listen on -url string URL to proxy to - diff --git a/main.go b/main.go index cb37642..7329572 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,14 @@ type myConfig struct { MyURL string MySock string Strip string + Log bool +} + +func logRequest(handler http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL) + handler.ServeHTTP(w, r) + }) } func main() { @@ -23,7 +31,7 @@ func main() { flag.StringVar(&c.MyURL, "url", "", "URL to proxy to") flag.StringVar(&c.MySock, "unix", "", "Unix socket to listen on") flag.StringVar(&c.Strip, "strip", "", "Strip prefix from reqeusts") - + flag.BoolVar(&c.Log, "log", false, "Log requests to stdout") flag.Parse() if c.MySock == "" { @@ -49,9 +57,21 @@ func main() { WriteTimeout: 3 * time.Second, IdleTimeout: 30 * time.Second, ReadHeaderTimeout: 20 * time.Second, - Handler: http.StripPrefix(c.Strip, frontendProxy), } + if c.Strip == "" { + if c.Log { + srv.Handler = logRequest(frontendProxy) + } else { + srv.Handler = frontendProxy + } + } else { + if c.Log { + srv.Handler = logRequest(http.StripPrefix(c.Strip, frontendProxy)) + } else { + srv.Handler = http.StripPrefix(c.Strip, frontendProxy) + } + } unixListener, err := net.Listen("unix", c.MySock) if err != nil {