From 76b295e7f5cf42cfb96bb68eb31c804bf98419ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Hock=20Isaza?= Date: Fri, 2 Jan 2015 16:53:13 -0500 Subject: [PATCH] Read PORT from the environment Read the `PORT` from the system environment if it's present. This is related to #44 and will make the port read order this: 1. `-p` flag. 2. `PORT` in the `.env` file 3. `PORT` environment variable 4. 5000 as default port --- outlet.go | 2 +- start.go | 2 ++ start_test.go | 14 +++++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/outlet.go b/outlet.go index 5ecb52f..a5a566e 100644 --- a/outlet.go +++ b/outlet.go @@ -2,12 +2,12 @@ package main import ( "bufio" + "bytes" "fmt" "github.com/ddollar/forego/Godeps/_workspace/src/github.com/daviddengcn/go-colortext" "io" "os" "sync" - "bytes" ) type OutletFactory struct { diff --git a/start.go b/start.go index 8d07a0c..cc7cde2 100644 --- a/start.go +++ b/start.go @@ -104,6 +104,8 @@ func basePort(env Env) (int, error) { return flagPort, nil } else if env["PORT"] != "" { return strconv.Atoi(env["PORT"]) + } else if os.Getenv("PORT") != "" { + return strconv.Atoi(os.Getenv("PORT")) } return defaultPort, nil } diff --git a/start_test.go b/start_test.go index 666e680..df53b40 100644 --- a/start_test.go +++ b/start_test.go @@ -1,6 +1,9 @@ package main -import "testing" +import ( + "os" + "testing" +) func TestParseConcurrencyFlagEmpty(t *testing.T) { c, err := parseConcurrency("") @@ -117,6 +120,15 @@ func TestPortFromEnv(t *testing.T) { t.Fatal("Base port should be 5000") } + os.Setenv("PORT", "4000") + port, err = basePort(env) + if err != nil { + t.Fatal("Can not get port: %s", err) + } + if port != 4000 { + t.Fatal("Base port should be 4000") + } + env["PORT"] = "6000" port, err = basePort(env) if err != nil {