diff --git a/README.md b/README.md index 8713d59e78db6a43cfb801ebba1a130e9ee3dee8..5a2fb19140f0ae7a4b02c936ec9468414eab9ccb 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,12 @@ variables: ``` $ go-httpbin -help Usage of ./dist/go-httpbin: - -listen string - Listen address (default ":8080") -max-duration duration Maximum duration a response may take (default 10s) -max-memory int Maximum size of request or response, in bytes (default 1048576) + -port int + Port to listen on (default 8080) ``` ## Installation @@ -37,22 +37,10 @@ go get github.com/mccutchen/go-httpbin/... ## Development -### Building - ``` make -``` - -### Testing - -``` make test make testcover -``` - -### Running - -``` make run ``` diff --git a/cmd/go-httpbin/main.go b/cmd/go-httpbin/main.go index db64d332a03e4107934f93713adf3ad3063db3f4..f49403ed765e14a2569b7afaee2b51617d34d219 100644 --- a/cmd/go-httpbin/main.go +++ b/cmd/go-httpbin/main.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "log" + "net" "net/http" "os" "strconv" @@ -12,16 +13,16 @@ import ( "github.com/mccutchen/go-httpbin/httpbin" ) -const defaultListenAddr = ":8080" +const defaultPort = 8080 var ( - listenAddr string + port int maxMemory int64 maxDuration time.Duration ) func main() { - flag.StringVar(&listenAddr, "listen", ":8080", "Listen address") + flag.IntVar(&port, "port", defaultPort, "Port to listen on") flag.Int64Var(&maxMemory, "max-memory", httpbin.DefaultMaxMemory, "Maximum size of request or response, in bytes") flag.DurationVar(&maxDuration, "max-duration", httpbin.DefaultMaxDuration, "Maximum duration a response may take") flag.Parse() @@ -46,14 +47,21 @@ func main() { os.Exit(1) } } - if listenAddr == defaultListenAddr && os.Getenv("LISTEN") != "" { - listenAddr = os.Getenv("LISTEN") + if port == defaultPort && os.Getenv("PORT") != "" { + port, err = strconv.Atoi(os.Getenv("PORT")) + if err != nil { + fmt.Printf("invalid value %#v for env var PORT: %s\n", os.Getenv("PORT"), err) + flag.Usage() + os.Exit(1) + } } h := httpbin.NewHTTPBinWithOptions(&httpbin.Options{ MaxMemory: maxMemory, MaxDuration: maxDuration, }) - log.Printf("listening on %s", listenAddr) + + listenAddr := net.JoinHostPort("0.0.0.0", strconv.Itoa(port)) + log.Printf("listening on port %s", listenAddr) log.Fatal(http.ListenAndServe(listenAddr, h.Handler())) }