Update readme with log sna strip options

This commit is contained in:
Your Name 2020-07-13 14:30:24 -04:00
parent 3a411ea621
commit cfecb83eb5
2 changed files with 26 additions and 3 deletions

View File

@ -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

24
main.go
View File

@ -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 {