Skip to content

Commit

Permalink
added support to read port from environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
s0s01qp committed Jul 28, 2021
1 parent 4adbde7 commit e7fc704
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
13 changes: 11 additions & 2 deletions flags/flags.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package flags

import (
actuatorCommons "github.com/sinhashubham95/go-actuator/commons"
"github.com/sinhashubham95/moxy/commons"
flag "github.com/spf13/pflag"
"os"
"strconv"
"strings"

"github.com/sinhashubham95/moxy/commons"
actuatorCommons "github.com/sinhashubham95/go-actuator/commons"
)

var (
Expand Down Expand Up @@ -41,6 +44,12 @@ func PersistencePath() string {

// Port is the port number where the application is running
func Port() int {
// see if the port is available from the environment variable
p, err := strconv.Atoi(os.Getenv(strings.ToUpper(commons.Port)))
if err == nil {
return p
}
// otherwise use the flag
return *port
}

Expand Down
7 changes: 7 additions & 0 deletions flags/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/sinhashubham95/moxy/commons"
"github.com/sinhashubham95/moxy/flags"
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"
)

Expand All @@ -23,6 +25,11 @@ func TestPort(t *testing.T) {
assert.Equal(t, commons.PortDefaultValue, flags.Port())
}

func TestPortFromEnv(t *testing.T) {
assert.NoError(t, os.Setenv(strings.ToUpper(commons.Port), "5050"))
assert.Equal(t, 5050, flags.Port())
}

func TestTLSEnabled(t *testing.T) {
assert.Equal(t, commons.TLSEnabledDefaultValue, flags.TLSEnabled())
}

0 comments on commit e7fc704

Please sign in to comment.