Skip to content
Snippets Groups Projects
Commit 682c4dbe authored by Will McCutchen's avatar Will McCutchen
Browse files

Just take a -port arg instead of a -listen address

This will simplify deployment on some platforms (e.g. heroku)
parent ff41abb0
No related branches found
No related tags found
No related merge requests found
...@@ -15,12 +15,12 @@ variables: ...@@ -15,12 +15,12 @@ variables:
``` ```
$ go-httpbin -help $ go-httpbin -help
Usage of ./dist/go-httpbin: Usage of ./dist/go-httpbin:
-listen string
Listen address (default ":8080")
-max-duration duration -max-duration duration
Maximum duration a response may take (default 10s) Maximum duration a response may take (default 10s)
-max-memory int -max-memory int
Maximum size of request or response, in bytes (default 1048576) Maximum size of request or response, in bytes (default 1048576)
-port int
Port to listen on (default 8080)
``` ```
## Installation ## Installation
...@@ -37,22 +37,10 @@ go get github.com/mccutchen/go-httpbin/... ...@@ -37,22 +37,10 @@ go get github.com/mccutchen/go-httpbin/...
## Development ## Development
### Building
``` ```
make make
```
### Testing
```
make test make test
make testcover make testcover
```
### Running
```
make run make run
``` ```
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"log" "log"
"net"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
...@@ -12,16 +13,16 @@ import ( ...@@ -12,16 +13,16 @@ import (
"github.com/mccutchen/go-httpbin/httpbin" "github.com/mccutchen/go-httpbin/httpbin"
) )
const defaultListenAddr = ":8080" const defaultPort = 8080
var ( var (
listenAddr string port int
maxMemory int64 maxMemory int64
maxDuration time.Duration maxDuration time.Duration
) )
func main() { 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.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.DurationVar(&maxDuration, "max-duration", httpbin.DefaultMaxDuration, "Maximum duration a response may take")
flag.Parse() flag.Parse()
...@@ -46,14 +47,21 @@ func main() { ...@@ -46,14 +47,21 @@ func main() {
os.Exit(1) os.Exit(1)
} }
} }
if listenAddr == defaultListenAddr && os.Getenv("LISTEN") != "" { if port == defaultPort && os.Getenv("PORT") != "" {
listenAddr = os.Getenv("LISTEN") 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{ h := httpbin.NewHTTPBinWithOptions(&httpbin.Options{
MaxMemory: maxMemory, MaxMemory: maxMemory,
MaxDuration: maxDuration, 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())) log.Fatal(http.ListenAndServe(listenAddr, h.Handler()))
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment