diff options
author | Ben Burwell <ben.burwell@trifecta.com> | 2015-11-18 15:51:21 -0500 |
---|---|---|
committer | Ben Burwell <ben.burwell@trifecta.com> | 2015-11-18 15:51:21 -0500 |
commit | 72d059bbd05fbbc906297e8d7fb2929c1fdd2697 (patch) | |
tree | 85fabf9e50a8302a94cf9fcc45b99409101bea3a | |
parent | 08b44af47306d8ede59496ccb16c97b4028cd677 (diff) |
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.
-rw-r--r-- | main.go | 15 |
1 files changed, 14 insertions, 1 deletions
@@ -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 { |