Skip to content

Commit

Permalink
Merge branch 'sergiobuj-port_from_env'
Browse files Browse the repository at this point in the history
  • Loading branch information
ddollar committed Jan 2, 2015
2 parents 60ea19d + bff4e8f commit 348bae0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
19 changes: 17 additions & 2 deletions start.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

const shutdownGraceTime = 3 * time.Second
const defaultPort = 5000

var flagPort int
var flagConcurrency string
Expand All @@ -37,7 +38,7 @@ Examples:
func init() {
cmdStart.Flag.StringVar(&flagProcfile, "f", "Procfile", "procfile")
cmdStart.Flag.Var(&envs, "e", "env")
cmdStart.Flag.IntVar(&flagPort, "p", 5000, "port")
cmdStart.Flag.IntVar(&flagPort, "p", defaultPort, "port")
cmdStart.Flag.StringVar(&flagConcurrency, "c", "", "concurrency")
cmdStart.Flag.BoolVar(&flagRestart, "r", false, "restart")
}
Expand Down Expand Up @@ -98,8 +99,22 @@ func (f *Forego) monitorInterrupt() {
}
}

func basePort(env Env) (int, error) {
if flagPort != defaultPort {
return flagPort, nil
} else if env["PORT"] != "" {
return strconv.Atoi(env["PORT"])
}
return defaultPort, nil
}

func (f *Forego) startProcess(idx, procNum int, proc ProcfileEntry, env Env, of *OutletFactory) {
port := flagPort + (idx * 100)
port, err := basePort(env)
if err != nil {
panic(err)
}

port = port + (idx * 100)

const interactive = false
workDir := filepath.Dir(flagProcfile)
Expand Down
27 changes: 27 additions & 0 deletions start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,30 @@ func TestParseConcurrencyFlagNoValue(t *testing.T) {
}

}

func TestPortFromEnv(t *testing.T) {
env := make(Env)
port, err := basePort(env)
if err != nil {
t.Fatalf("Can not get base port: %s", err)
}
if port != 5000 {
t.Fatal("Base port should be 5000")
}

env["PORT"] = "6000"
port, err = basePort(env)
if err != nil {
t.Fatalf("Can not get base port: %s", err)
}
if port != 6000 {
t.Fatal("Base port should be 6000")
}

env["PORT"] = "forego"
port, err = basePort(env)
if err == nil {
t.Fatalf("Port 'forego' should fail: %s", err)
}

}

0 comments on commit 348bae0

Please sign in to comment.