From 72d059bbd05fbbc906297e8d7fb2929c1fdd2697 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Wed, 18 Nov 2015 15:51:21 -0500 Subject: Allow use of different ports for the zipserver Let users pick a port to run the server on by specifying a PORT environment variable. If this value is not specified, fall back to 8080. --- main.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 4c3c3b5..9385f85 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,8 @@ import ( "encoding/json" "fmt" "net/http" + "os" + "strings" ) func main() { @@ -13,7 +15,7 @@ func main() { handleZipcodeRequest(w, r, db) }) - http.ListenAndServe(":8080", nil) + http.ListenAndServe(getListeningAddress(), nil) } func getInitializedDatabase() *ZipcodeDatabase { @@ -26,6 +28,17 @@ func getInitializedDatabase() *ZipcodeDatabase { return db } +func getListeningAddress() string { + port := os.Getenv("PORT") + if port == "" { + port = "8080" + } + + return strings.Join([]string{ + ":", + port, + }, "") +} func handleZipcodeRequest(response http.ResponseWriter, request *http.Request, db *ZipcodeDatabase) { zip := zipcodeForRequest(request) if details := db.Find(zip); details == nil { -- cgit v1.2.3