summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go15
1 files changed, 14 insertions, 1 deletions
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 {