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
This commit is contained in:
Nicolás Hock Isaza
2015-01-02 16:53:13 -05:00
parent 23c8b0413e
commit 76b295e7f5
3 changed files with 16 additions and 2 deletions
+1 -1
View File
@@ -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 {
+2
View File
@@ -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
}
+13 -1
View File
@@ -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 {