Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

refactor: read Environment Variables only once (#685)

Guilherme Oenning1dffaf9e6cd7
diff --git a/Gopkg.lock b/Gopkg.lock
index 4ac4bed53..e7a6a311c 100644
--- a/Gopkg.lock
+++ b/Gopkg.lock
@@ -111,6 +111,14 @@
pruneopts = "UT"
revision = "c2b33e84"
+[[projects]]
+ branch = "master"
+ digest = "1:bd1b5ce5a04000dbe5d1c22812699509c17a8572c6385a43023c7e08ea4e7623"
+ name = "github.com/joeshaw/envdecode"
+ packages = ["."]
+ pruneopts = "UT"
+ revision = "c9e0158544672841c3ebf49308f536d918f2e525"
+
[[projects]]
branch = "master"
digest = "1:3a1e2d3c7b109c4f7bd10b208d0cbc578e618fb26832cf96544c638d927022bf"
@@ -229,6 +237,7 @@
"github.com/goenning/letteravatar",
"github.com/goenning/sqlcertcache",
"github.com/gosimple/slug",
+ "github.com/joeshaw/envdecode",
"github.com/julienschmidt/httprouter",
"github.com/lib/pq",
"github.com/russross/blackfriday",
diff --git a/Gopkg.toml b/Gopkg.toml
index cc9601834..8af3e0e0d 100644
--- a/Gopkg.toml
+++ b/Gopkg.toml
@@ -45,3 +45,7 @@ ignored = ["github.com/magefile/mage/mg","github.com/magefile/mage/sh"]
branch = "master"
name = "github.com/goenning/sqlcertcache"
+[[constraint]]
+ branch = "master"
+ name = "github.com/joeshaw/envdecode"
+
diff --git a/app/cmd/ping.go b/app/cmd/ping.go
index b2fe37b67..48dc516aa 100644
--- a/app/cmd/ping.go
+++ b/app/cmd/ping.go
@@ -12,7 +12,7 @@ import (
//Returns an exitcode, 0 for OK and 1 for ERROR
func RunPing() int {
protocol := "http://"
- if env.IsDefined("SSL_CERT") || env.IsDefined("SSL_AUTO") {
+ if env.Config.SSLCert != "" || env.Config.AutoSSL {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
diff --git a/app/cmd/server.go b/app/cmd/server.go
index 8d0fb5478..45ae1bd0a 100644
--- a/app/cmd/server.go
+++ b/app/cmd/server.go
@@ -21,7 +21,7 @@ func RunServer(settings *models.SystemSettings) int {
e := routes(web.New(settings))
- go e.Start(":" + env.GetEnvOrDefault("PORT", "3000"))
+ go e.Start(":" + env.Config.Port)
return listenSignals(e, settings)
}
diff --git a/app/middlewares/maintenance.go b/app/middlewares/maintenance.go
index efc8048a7..3b71c8430 100644
--- a/app/middlewares/maintenance.go
+++ b/app/middlewares/maintenance.go
@@ -11,22 +11,19 @@ import (
func Maintenance() web.MiddlewareFunc {
return func(next web.HandlerFunc) web.HandlerFunc {
return func(c web.Context) error {
- if env.GetEnvOrDefault("MAINTENANCE", "") != "true" {
+ if env.Config.Maintenance.Enabled == false {
return next(c)
}
c.Response.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
c.Response.Header().Set("Retry-After", "3600")
- message := env.GetEnvOrDefault("MAINTENANCE_MESSAGE", "Sorry, we're down for scheduled maintenance right now.")
- until := env.GetEnvOrDefault("MAINTENANCE_UNTIL", "")
-
return c.Render(http.StatusServiceUnavailable, "maintenance.html", web.Props{
Title: "UNDER MAINTENANCE",
- Description: message,
+ Description: env.Config.Maintenance.Message,
Data: web.Map{
- "message": message,
- "until": until,
+ "message": env.Config.Maintenance.Message,
+ "until": env.Config.Maintenance.Until,
},
})
}
diff --git a/app/middlewares/maintenance_test.go b/app/middlewares/maintenance_test.go
index db5e3d43e..df905b2e4 100644
--- a/app/middlewares/maintenance_test.go
+++ b/app/middlewares/maintenance_test.go
@@ -2,12 +2,12 @@ package middlewares_test
import (
"net/http"
- "os"
"testing"
"time"
"github.com/getfider/fider/app/middlewares"
. "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/mock"
"github.com/getfider/fider/app/pkg/web"
)
@@ -30,11 +30,11 @@ func TestMaintenance_Enabled(t *testing.T) {
RegisterT(t)
defer func() {
- os.Setenv("MAINTENANCE", "")
+ env.Config.Maintenance.Enabled = false
}()
server, _ := mock.NewServer()
- os.Setenv("MAINTENANCE", "true")
+ env.Config.Maintenance.Enabled = true
server.Use(middlewares.ClientCache(30 * time.Hour))
server.Use(middlewares.Maintenance())
handler := func(c web.Context) error {
diff --git a/app/middlewares/security.go b/app/middlewares/security.go
index 1e2aca519..f90b696c3 100644
--- a/app/middlewares/security.go
+++ b/app/middlewares/security.go
@@ -12,7 +12,7 @@ import (
func Secure() web.MiddlewareFunc {
return func(next web.HandlerFunc) web.HandlerFunc {
return func(c web.Context) error {
- cdnHost := env.GetEnvOrDefault("CDN_HOST", "")
+ cdnHost := env.Config.CDN.Host
if cdnHost != "" && !env.IsSingleHostMode() {
cdnHost = "*." + cdnHost
}
diff --git a/app/middlewares/security_test.go b/app/middlewares/security_test.go
index f9c30ec38..b94f4be90 100644
--- a/app/middlewares/security_test.go
+++ b/app/middlewares/security_test.go
@@ -2,11 +2,11 @@ package middlewares_test
import (
"net/http"
- "os"
"testing"
"github.com/getfider/fider/app/middlewares"
. "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/mock"
"github.com/getfider/fider/app/pkg/web"
)
@@ -35,7 +35,7 @@ func TestSecureWithoutCDN(t *testing.T) {
func TestSecureWithCDN(t *testing.T) {
RegisterT(t)
- os.Setenv("CDN_HOST", "test.fider.io")
+ env.Config.CDN.Host = "test.fider.io"
server, _ := mock.NewServer()
server.Use(middlewares.Secure())
@@ -58,7 +58,7 @@ func TestSecureWithCDN(t *testing.T) {
func TestSecureWithCDN_SingleHost(t *testing.T) {
RegisterT(t)
- os.Setenv("CDN_HOST", "test.fider.io")
+ env.Config.CDN.Host = "test.fider.io"
server, _ := mock.NewSingleTenantServer()
server.Use(middlewares.Secure())
diff --git a/app/pkg/assert/assert.go b/app/pkg/assert/assert.go
index 4b317aafd..66efb2121 100644
--- a/app/pkg/assert/assert.go
+++ b/app/pkg/assert/assert.go
@@ -9,6 +9,7 @@ import (
"testing"
"time"
+ "github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/errors"
)
@@ -61,6 +62,7 @@ func restartEnv() {
for k, v := range envVariables {
os.Setenv(k, v)
}
+ env.Reload()
}
//AnyAssertions is used to assert any kind of value
diff --git a/app/pkg/blob/s3/s3.go b/app/pkg/blob/s3/s3.go
index 705cab513..f65a6a641 100644
--- a/app/pkg/blob/s3/s3.go
+++ b/app/pkg/blob/s3/s3.go
@@ -33,21 +33,17 @@ func isNotFound(err error) bool {
return false
}
-//Client is an S3 Client
+//DefaultClient is an S3 Client
var DefaultClient *s3.S3
func init() {
- endpointURL := env.GetEnvOrDefault("BLOB_STORAGE_S3_ENDPOINT_URL", "")
- if endpointURL != "" {
- region := env.GetEnvOrDefault("BLOB_STORAGE_S3_REGION", "")
- accessKeyID := env.GetEnvOrDefault("BLOB_STORAGE_S3_ACCESS_KEY_ID", "")
- secretAccessKey := env.GetEnvOrDefault("BLOB_STORAGE_S3_SECRET_ACCESS_KEY", "")
-
+ s3EnvConfig := env.Config.BlobStorage.S3
+ if s3EnvConfig.EndpointURL != "" {
s3Config := &aws.Config{
- Credentials: credentials.NewStaticCredentials(accessKeyID, secretAccessKey, ""),
- Endpoint: aws.String(endpointURL),
- Region: aws.String(region),
- DisableSSL: aws.Bool(strings.HasSuffix(endpointURL, "http://")),
+ Credentials: credentials.NewStaticCredentials(s3EnvConfig.AccessKeyID, s3EnvConfig.SecretAccessKey, ""),
+ Endpoint: aws.String(s3EnvConfig.EndpointURL),
+ Region: aws.String(s3EnvConfig.Region),
+ DisableSSL: aws.Bool(strings.HasSuffix(s3EnvConfig.EndpointURL, "http://")),
S3ForcePathStyle: aws.Bool(true),
}
awsSession := session.New(s3Config)
diff --git a/app/pkg/dbx/dbx.go b/app/pkg/dbx/dbx.go
index 912949562..e057b33c5 100644
--- a/app/pkg/dbx/dbx.go
+++ b/app/pkg/dbx/dbx.go
@@ -4,7 +4,6 @@ import (
"database/sql"
"io/ioutil"
"reflect"
- "strconv"
"time"
"strings"
@@ -26,22 +25,13 @@ func New() *Database {
// NewWithLogger creates a new Database instance with logging or panic
func NewWithLogger(logger log.Logger) *Database {
- conn, err := sql.Open("postgres", env.MustGet("DATABASE_URL"))
+ conn, err := sql.Open("postgres", env.Config.Database.URL)
if err != nil {
panic(errors.Wrap(err, "failed to open connection to the database"))
}
- maxIdle, err := strconv.Atoi(env.GetEnvOrDefault("DATABASE_MAX_IDLE_CONNS", "10"))
- if err != nil {
- panic(errors.Wrap(err, "failed to convert DATABASE_MAX_IDLE_CONNS to integer"))
- }
- conn.SetMaxIdleConns(maxIdle)
-
- maxOpen, err := strconv.Atoi(env.GetEnvOrDefault("DATABASE_MAX_OPEN_CONNS", "20"))
- if err != nil {
- panic(errors.Wrap(err, "failed to convert DATABASE_MAX_OPEN_CONNS to integer"))
- }
- conn.SetMaxOpenConns(maxOpen)
+ conn.SetMaxIdleConns(env.Config.Database.MaxIdleConns)
+ conn.SetMaxOpenConns(env.Config.Database.MaxOpenConns)
return &Database{conn, logger, NewRowMapper()}
}
diff --git a/app/pkg/di/blob.go b/app/pkg/di/blob.go
index 116b75cd2..3cf572303 100644
--- a/app/pkg/di/blob.go
+++ b/app/pkg/di/blob.go
@@ -22,28 +22,35 @@ func NewEmailer(logger log.Logger) email.Sender {
if env.IsTest() {
return noop.NewSender()
}
- if env.IsDefined("EMAIL_MAILGUN_API") {
- return mailgun.NewSender(logger, web.NewHTTPClient(), env.MustGet("EMAIL_MAILGUN_DOMAIN"), env.MustGet("EMAIL_MAILGUN_API"))
+
+ if env.Config.Email.Mailgun.APIKey != "" {
+ return mailgun.NewSender(
+ logger,
+ web.NewHTTPClient(),
+ env.Config.Email.Mailgun.Domain,
+ env.Config.Email.Mailgun.APIKey,
+ )
}
+
return smtp.NewSender(
logger,
- env.MustGet("EMAIL_SMTP_HOST"),
- env.MustGet("EMAIL_SMTP_PORT"),
- env.GetEnvOrDefault("EMAIL_SMTP_USERNAME", ""),
- env.GetEnvOrDefault("EMAIL_SMTP_PASSWORD", ""),
+ env.Config.Email.SMTP.Host,
+ env.Config.Email.SMTP.Port,
+ env.Config.Email.SMTP.Username,
+ env.Config.Email.SMTP.Password,
)
}
// NewBlobStorage creates a new blob storage instance based on current configuration
func NewBlobStorage(trx *dbx.Trx) blob.Storage {
- storageType := strings.ToLower(env.GetEnvOrDefault("BLOB_STORAGE", "sql"))
+ storageType := strings.ToLower(env.Config.BlobStorage.Type)
switch storageType {
case "sql":
return sql.NewStorage(trx)
case "s3":
- return s3.NewStorage(env.MustGet("BLOB_STORAGE_S3_BUCKET"))
+ return s3.NewStorage(env.Config.BlobStorage.S3.BucketName)
case "fs":
- return fs.NewStorage(env.MustGet("BLOB_STORAGE_FS_PATH"))
+ return fs.NewStorage(env.Config.BlobStorage.FS.Path)
}
panic("Invalid blob storage type: " + storageType)
}
diff --git a/app/pkg/email/email.go b/app/pkg/email/email.go
index 222881e33..ad61dbef2 100644
--- a/app/pkg/email/email.go
+++ b/app/pkg/email/email.go
@@ -80,7 +80,7 @@ type Context interface {
}
// NoReply is the default 'from' address
-var NoReply = env.MustGet("EMAIL_NOREPLY")
+var NoReply = env.Config.Email.NoReply
// Recipient contains details of who is receiving the email
type Recipient struct {
@@ -112,9 +112,9 @@ func (r Recipient) String() string {
return address.String()
}
-var whitelist = env.GetEnvOrDefault("EMAIL_WHITELIST", "")
+var whitelist = env.Config.Email.Whitelist
var whitelistRegex = regexp.MustCompile(whitelist)
-var blacklist = env.GetEnvOrDefault("EMAIL_BLACKLIST", "")
+var blacklist = env.Config.Email.Blacklist
var blacklistRegex = regexp.MustCompile(blacklist)
// SetWhitelist can be used to change email whitelist during runtime
diff --git a/app/pkg/email/mailgun/mailgun_test.go b/app/pkg/email/mailgun/mailgun_test.go
index 3660cb207..01de464af 100644
--- a/app/pkg/email/mailgun/mailgun_test.go
+++ b/app/pkg/email/mailgun/mailgun_test.go
@@ -3,9 +3,9 @@ package mailgun_test
import (
"io/ioutil"
"net/url"
- "os"
"testing"
+ "github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/worker"
"github.com/getfider/fider/app/models"
@@ -32,7 +32,7 @@ func init() {
func TestSend_Success(t *testing.T) {
RegisterT(t)
- os.Setenv("HOST_MODE", "MULTI")
+ env.Config.HostMode = "multi"
client.Reset()
to := email.Recipient{
diff --git a/app/pkg/env/env.go b/app/pkg/env/env.go
index fed9427f6..1b10bb5d9 100644
--- a/app/pkg/env/env.go
+++ b/app/pkg/env/env.go
@@ -2,46 +2,130 @@ package env
import (
"fmt"
- "net"
"os"
"strings"
"path"
+
+ "github.com/getfider/fider/app/pkg/errors"
+ "github.com/joeshaw/envdecode"
)
-// GetEnvOrDefault retrieves the value of the environment variable named by the key.
-// It returns the value if available, otherwise returns defaultValue
-func GetEnvOrDefault(name string, defaultValue string) string {
- value := os.Getenv(name)
- if value == "" {
- return defaultValue
+type config struct {
+ Environment string `env:"GO_ENV,default=production"`
+ AutoSSL bool `env:"SSL_AUTO,default=false"`
+ SSLCert string `env:"SSL_CERT"`
+ SSLCertKey string `env:"SSL_CERT_KEY"`
+ Port string `env:"PORT,default=3000"`
+ HostMode string `env:"HOST_MODE,default=single"`
+ HostDomain string `env:"HOST_DOMAIN"`
+ JWTSecret string `env:"JWT_SECRET,required"`
+ Database struct {
+ URL string `env:"DATABASE_URL,required"`
+ MaxIdleConns int `env:"DATABASE_MAX_IDLE_CONNS,default=2,strict"`
+ MaxOpenConns int `env:"DATABASE_MAX_OPEN_CONNS,default=4,strict"`
+ }
+ CDN struct {
+ Host string `env:"CDN_HOST"`
+ }
+ Log struct {
+ Level string `env:"LOG_LEVEL,default=INFO"`
+ }
+ OAuth struct {
+ Google struct {
+ ClientID string `env:"OAUTH_GOOGLE_CLIENTID"`
+ Secret string `env:"OAUTH_GOOGLE_SECRET"`
+ }
+ Facebook struct {
+ AppID string `env:"OAUTH_FACEBOOK_APPID"`
+ Secret string `env:"OAUTH_FACEBOOK_SECRET"`
+ }
+ GitHub struct {
+ ClientID string `env:"OAUTH_GITHUB_CLIENTID"`
+ Secret string `env:"OAUTH_GITHUB_SECRET"`
+ }
+ }
+ Email struct {
+ NoReply string `env:"EMAIL_NOREPLY,required"`
+ Whitelist string `env:"EMAIL_WHITELIST"`
+ Blacklist string `env:"EMAIL_BLACKLIST"`
+ Mailgun struct {
+ APIKey string `env:"EMAIL_MAILGUN_API"`
+ Domain string `env:"EMAIL_MAILGUN_DOMAIN"`
+ }
+ SMTP struct {
+ Host string `env:"EMAIL_SMTP_HOST"`
+ Port string `env:"EMAIL_SMTP_PORT"`
+ Username string `env:"EMAIL_SMTP_USERNAME"`
+ Password string `env:"EMAIL_SMTP_PASSWORD"`
+ }
}
- return value
+ BlobStorage struct {
+ Type string `env:"BLOB_STORAGE,default=sql"`
+ S3 struct {
+ EndpointURL string `env:"BLOB_STORAGE_S3_ENDPOINT_URL"`
+ Region string `env:"BLOB_STORAGE_S3_REGION"`
+ AccessKeyID string `env:"BLOB_STORAGE_S3_ACCESS_KEY_ID"`
+ SecretAccessKey string `env:"BLOB_STORAGE_S3_SECRET_ACCESS_KEY"`
+ BucketName string `env:"BLOB_STORAGE_S3_BUCKET"`
+ }
+ FS struct {
+ Path string `env:"BLOB_STORAGE_FS_PATH"`
+ }
+ }
+ Maintenance struct {
+ Enabled bool `env:"MAINTENANCE,default=false,strict"`
+ Message string `env:"MAINTENANCE_MESSAGE"`
+ Until string `env:"MAINTENANCE_UNTIL"`
+ }
+ GoogleAnalytics string `env:"GOOGLE_ANALYTICS"`
}
-// IsDefined returns true if given environment variable is defined
-func IsDefined(name string) bool {
- value := os.Getenv(name)
- return value != ""
+// Config is a strongly typed reference to all configuration parsed from Environment Variables
+var Config config
+
+func init() {
+ Reload()
+}
+
+// Reload configuration from current Enviornment Variables
+func Reload() {
+ Config = config{}
+ err := envdecode.Decode(&Config)
+ if err != nil {
+ panic(errors.Wrap(err, "failed to parse environment variables"))
+ }
+
+ //Environment validations
+ if Config.HostMode != "single" {
+ mustBeSet("HOST_DOMAIN")
+ }
+
+ if Config.Email.Mailgun.APIKey != "" {
+ mustBeSet("EMAIL_MAILGUN_DOMAIN")
+ } else {
+ mustBeSet("EMAIL_SMTP_HOST")
+ mustBeSet("EMAIL_SMTP_PORT")
+ }
+
+ bsType := strings.ToLower(Config.BlobStorage.Type)
+ if bsType == "s3" {
+ mustBeSet("BLOB_STORAGE_S3_BUCKET")
+ } else if bsType == "fs" {
+ mustBeSet("BLOB_STORAGE_FS_PATH")
+ }
}
-// MustGet returns environment variable or panic
-func MustGet(name string) string {
+func mustBeSet(name string) {
value := os.Getenv(name)
if value == "" {
panic(fmt.Errorf("Could not find environment variable named '%s'", name))
}
- return value
-}
-
-// Mode returns HOST_MODE or its default value
-func Mode() string {
- return GetEnvOrDefault("HOST_MODE", "single")
}
// IsSingleHostMode returns true if host mode is set to single tenant
func IsSingleHostMode() bool {
- return Mode() == "single"
+ return Config.HostMode == "single"
}
var hasLegal *bool
@@ -60,51 +144,24 @@ func HasLegal() bool {
// MultiTenantDomain returns domain name of current instance for multi tenant hosts
func MultiTenantDomain() string {
if !IsSingleHostMode() {
- return "."+MustGet("HOST_DOMAIN")
+ return "." + Config.HostDomain
}
return ""
}
-var publicIP = make(map[string]string, 0)
-
-// GetPublicIP returns the public IP of current hosting server
-func GetPublicIP() (string, error) {
- if IsSingleHostMode() {
- return "", nil
- }
-
- domain := MultiTenantDomain()[1:]
- if _, ok := publicIP[domain]; !ok {
- addr, err := net.LookupIP(domain)
- if err == nil {
- publicIP[domain] = addr[0].String()
- } else {
- return "", err
- }
- }
- return publicIP[domain], nil
-}
-
-// Current returns current Fider environment
-func Current() string {
- env := os.Getenv("GO_ENV")
- switch env {
- case "test":
- return "test"
- case "production":
- return "production"
- }
- return "development"
-}
-
// IsProduction returns true on Fider production environment
func IsProduction() bool {
- return Current() == "production"
+ return Config.Environment == "production" || (!IsTest() && !IsDevelopment())
}
// IsTest returns true on Fider test environment
func IsTest() bool {
- return Current() == "test"
+ return Config.Environment == "test"
+}
+
+// IsDevelopment returns true on Fider production environment
+func IsDevelopment() bool {
+ return Config.Environment == "development"
}
// Path returns root path of project + given path
@@ -124,11 +181,6 @@ func Etc(p ...string) string {
return Path(paths...)
}
-// IsDevelopment returns true on Fider production environment
-func IsDevelopment() bool {
- return Current() == "development"
-}
-
// Subdomain returns the Fider subdomain (if available) from given host
func Subdomain(host string) string {
if IsSingleHostMode() {
@@ -140,8 +192,8 @@ func Subdomain(host string) string {
return strings.Replace(host, domain, "", -1)
}
- if IsDefined("CDN_HOST") {
- domain = MustGet("CDN_HOST")
+ if Config.CDN.Host != "" {
+ domain = Config.CDN.Host
parts := strings.Split(domain, ":")
if parts[0] != "" && strings.Contains(host, "."+parts[0]) {
return strings.Replace(host, "."+parts[0], "", -1)
diff --git a/app/pkg/env/env_test.go b/app/pkg/env/env_test.go
index 8dbc58036..1eb7ec7b5 100644
--- a/app/pkg/env/env_test.go
+++ b/app/pkg/env/env_test.go
@@ -16,31 +16,7 @@ var envs = []struct {
{"test", "test", env.IsTest},
{"development", "development", env.IsDevelopment},
{"production", "production", env.IsProduction},
- {"anything", "development", env.IsDevelopment},
-}
-
-func TestGetEnvOrDefault(t *testing.T) {
- RegisterT(t)
-
- key := env.GetEnvOrDefault("UNKNOWN_KEY", "some value")
- Expect(key).Equals("some value")
-
- path := env.GetEnvOrDefault("PATH", "default path")
- Expect(path).NotEquals("default path")
-}
-
-func TestCurrent(t *testing.T) {
- RegisterT(t)
-
- current := env.Current()
- defer func() {
- os.Setenv("GO_ENV", current)
- }()
-
- for _, testCase := range envs {
- os.Setenv("GO_ENV", testCase.go_env)
- Expect(env.Current()).Equals(testCase.env)
- }
+ {"anything", "production", env.IsProduction},
}
func TestPath(t *testing.T) {
@@ -62,26 +38,18 @@ func TestPath(t *testing.T) {
func TestIsEnvironment(t *testing.T) {
RegisterT(t)
- current := env.Current()
+ current := env.Config.Environment
defer func() {
- os.Setenv("GO_ENV", current)
+ env.Config.Environment = current
}()
for _, testCase := range envs {
- os.Setenv("GO_ENV", testCase.go_env)
+ env.Config.Environment = testCase.go_env
actual := testCase.isEnv()
Expect(actual).IsTrue()
}
}
-func TestMustGet(t *testing.T) {
- RegisterT(t)
-
- Expect(func() {
- env.MustGet("THIS_DOES_NOT_EXIST")
- }).Panics()
-}
-
func TestHasLegal(t *testing.T) {
RegisterT(t)
@@ -91,45 +59,20 @@ func TestHasLegal(t *testing.T) {
func TestMultiTenantDomain(t *testing.T) {
RegisterT(t)
- os.Setenv("HOST_DOMAIN", "test.fider.io")
+ env.Config.HostDomain = "test.fider.io"
Expect(env.MultiTenantDomain()).Equals(".test.fider.io")
- os.Setenv("HOST_DOMAIN", "dev.fider.io")
+ env.Config.HostDomain = "dev.fider.io"
Expect(env.MultiTenantDomain()).Equals(".dev.fider.io")
- os.Setenv("HOST_DOMAIN", "fider.io")
+ env.Config.HostDomain = "fider.io"
Expect(env.MultiTenantDomain()).Equals(".fider.io")
}
-func TestGetPublicIP(t *testing.T) {
- RegisterT(t)
-
- os.Setenv("HOST_MODE", "multi")
- os.Setenv("HOST_DOMAIN", "dev.fider.io")
- ip, err := env.GetPublicIP()
- Expect(err).IsNil()
- Expect(ip).Equals("127.0.0.1")
-
- os.Setenv("HOST_DOMAIN", "fider.io")
- ip, err = env.GetPublicIP()
- Expect(err).IsNil()
- Expect(ip).Equals("174.138.111.44")
-
- os.Setenv("HOST_DOMAIN", "invalidinvalidinvalid.io")
- ip, err = env.GetPublicIP()
- Expect(err).IsNotNil()
- Expect(ip).IsEmpty()
-
- os.Setenv("HOST_MODE", "single")
- ip, err = env.GetPublicIP()
- Expect(err).IsNil()
- Expect(ip).Equals("")
-}
-
func TestSubdomain(t *testing.T) {
RegisterT(t)
Expect(env.Subdomain("demo.test.assets-fider.io")).Equals("")
- os.Setenv("CDN_HOST", "test.assets-fider.io:3000")
+ env.Config.CDN.Host = "test.assets-fider.io:3000"
Expect(env.Subdomain("demo.test.fider.io")).Equals("demo")
Expect(env.Subdomain("demo.test.assets-fider.io")).Equals("demo")
@@ -137,7 +80,7 @@ func TestSubdomain(t *testing.T) {
Expect(env.Subdomain("test.assets-fider.io")).Equals("")
Expect(env.Subdomain("helloworld.com")).Equals("")
- os.Setenv("HOST_MODE", "single")
+ env.Config.HostMode = "single"
Expect(env.Subdomain("demo.test.fider.io")).Equals("")
Expect(env.Subdomain("demo.test.assets-fider.io")).Equals("")
diff --git a/app/pkg/jwt/jwt.go b/app/pkg/jwt/jwt.go
index 1905ff6e6..1420532c4 100644
--- a/app/pkg/jwt/jwt.go
+++ b/app/pkg/jwt/jwt.go
@@ -6,7 +6,7 @@ import (
"github.com/getfider/fider/app/pkg/errors"
)
-var jwtSecret = env.MustGet("JWT_SECRET")
+var jwtSecret = env.Config.JWTSecret
// Metadata is the basic JWT information
type Metadata = jwtgo.StandardClaims
diff --git a/app/pkg/log/console/logger.go b/app/pkg/log/console/logger.go
index b5f8be630..84ab27d17 100644
--- a/app/pkg/log/console/logger.go
+++ b/app/pkg/log/console/logger.go
@@ -22,7 +22,7 @@ type Logger struct {
// NewLogger creates a new Logger
func NewLogger(tag string) *Logger {
- level := strings.ToUpper(env.GetEnvOrDefault("LOG_LEVEL", ""))
+ level := strings.ToUpper(env.Config.Log.Level)
return &Logger{
tag: tag,
logger: stdLog.New(os.Stdout, "", 0),
diff --git a/app/pkg/log/database/logger.go b/app/pkg/log/database/logger.go
index c506b47dc..bb01e9609 100644
--- a/app/pkg/log/database/logger.go
+++ b/app/pkg/log/database/logger.go
@@ -25,7 +25,7 @@ type Logger struct {
// NewLogger creates a new Logger
func NewLogger(tag string, db *dbx.Database) *Logger {
- level := strings.ToUpper(env.GetEnvOrDefault("LOG_LEVEL", ""))
+ level := strings.ToUpper(env.Config.Log.Level)
return &Logger{
tag: tag,
db: db,
diff --git a/app/pkg/mock/setup.go b/app/pkg/mock/setup.go
index 01e1be055..ac4c92b84 100644
--- a/app/pkg/mock/setup.go
+++ b/app/pkg/mock/setup.go
@@ -1,8 +1,6 @@
package mock
import (
- "os"
-
"github.com/getfider/fider/app"
"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app/pkg/blob/fs"
@@ -28,7 +26,7 @@ var AryaStark *models.User
func NewSingleTenantServer() (*Server, *app.Services) {
services := createServices(false)
server := createServer(services)
- os.Setenv("HOST_MODE", "single")
+ env.Config.HostMode = "single"
return server, services
}
@@ -36,7 +34,7 @@ func NewSingleTenantServer() (*Server, *app.Services) {
func NewServer() (*Server, *app.Services) {
services := createServices(true)
server := createServer(services)
- os.Setenv("HOST_MODE", "multi")
+ env.Config.HostMode = "multi"
return server, services
}
diff --git a/app/pkg/web/context.go b/app/pkg/web/context.go
index d2fd68ace..0753be738 100644
--- a/app/pkg/web/context.go
+++ b/app/pkg/web/context.go
@@ -523,11 +523,11 @@ func (ctx *Context) PermanentRedirect(url string) error {
// GlobalAssetsURL return the full URL to a globally shared static asset
func (ctx *Context) GlobalAssetsURL(path string, a ...interface{}) string {
path = fmt.Sprintf(path, a...)
- if env.IsDefined("CDN_HOST") {
+ if env.Config.CDN.Host != "" {
if env.IsSingleHostMode() {
- return ctx.Request.URL.Scheme + "://" + env.MustGet("CDN_HOST") + path
+ return ctx.Request.URL.Scheme + "://" + env.Config.CDN.Host + path
}
- return ctx.Request.URL.Scheme + "://cdn." + env.MustGet("CDN_HOST") + path
+ return ctx.Request.URL.Scheme + "://cdn." + env.Config.CDN.Host + path
}
return ctx.BaseURL() + path
}
@@ -535,11 +535,11 @@ func (ctx *Context) GlobalAssetsURL(path string, a ...interface{}) string {
// TenantAssetsURL return the full URL to a tenant-specific static asset
func (ctx *Context) TenantAssetsURL(path string, a ...interface{}) string {
path = fmt.Sprintf(path, a...)
- if env.IsDefined("CDN_HOST") && ctx.Tenant() != nil {
+ if env.Config.CDN.Host != "" && ctx.Tenant() != nil {
if env.IsSingleHostMode() {
- return ctx.Request.URL.Scheme + "://" + env.MustGet("CDN_HOST") + path
+ return ctx.Request.URL.Scheme + "://" + env.Config.CDN.Host + path
}
- return ctx.Request.URL.Scheme + "://" + ctx.Tenant().Subdomain + "." + env.MustGet("CDN_HOST") + path
+ return ctx.Request.URL.Scheme + "://" + ctx.Tenant().Subdomain + "." + env.Config.CDN.Host + path
}
return ctx.BaseURL() + path
}
diff --git a/app/pkg/web/context_test.go b/app/pkg/web/context_test.go
index 9e4f99613..cdbc7c413 100644
--- a/app/pkg/web/context_test.go
+++ b/app/pkg/web/context_test.go
@@ -4,12 +4,12 @@ import (
"crypto/tls"
"net/http/httptest"
"net/url"
- "os"
"strings"
"testing"
"github.com/getfider/fider/app/models"
. "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/web"
)
@@ -112,7 +112,7 @@ func TestTenantURL_WithCNAME(t *testing.T) {
func TestTenantURL_SingleHostMode(t *testing.T) {
RegisterT(t)
- os.Setenv("HOST_MODE", "single")
+ env.Config.HostMode = "single"
ctx := newGetContext("http://demo.test.fider.io:3000", nil)
tenant := &models.Tenant{
@@ -125,12 +125,12 @@ func TestTenantURL_SingleHostMode(t *testing.T) {
func TestGlobalAssetsURL_SingleHostMode(t *testing.T) {
RegisterT(t)
- os.Setenv("HOST_MODE", "single")
+ env.Config.HostMode = "single"
ctx := newGetContext("http://feedback.theavengers.com:3000", nil)
Expect(ctx.GlobalAssetsURL("/assets/main.js")).Equals("http://feedback.theavengers.com:3000/assets/main.js")
Expect(ctx.GlobalAssetsURL("/assets/main.css")).Equals("http://feedback.theavengers.com:3000/assets/main.css")
- os.Setenv("CDN_HOST", "assets-fider.io")
+ env.Config.CDN.Host = "assets-fider.io"
Expect(ctx.GlobalAssetsURL("/assets/main.js")).Equals("http://assets-fider.io/assets/main.js")
Expect(ctx.GlobalAssetsURL("/assets/main.css")).Equals("http://assets-fider.io/assets/main.css")
}
@@ -138,7 +138,7 @@ func TestGlobalAssetsURL_SingleHostMode(t *testing.T) {
func TestGlobalAssetsURL_MultiHostMode(t *testing.T) {
RegisterT(t)
- os.Setenv("HOST_MODE", "multi")
+ env.Config.HostMode = "multi"
ctx := newGetContext("http://theavengers.test.fider.io:3000", nil)
ctx.SetTenant(&models.Tenant{
ID: 1,
@@ -149,7 +149,7 @@ func TestGlobalAssetsURL_MultiHostMode(t *testing.T) {
Expect(ctx.GlobalAssetsURL("/assets/main.js")).Equals("http://theavengers.test.fider.io:3000/assets/main.js")
Expect(ctx.GlobalAssetsURL("/assets/main.css")).Equals("http://theavengers.test.fider.io:3000/assets/main.css")
- os.Setenv("CDN_HOST", "assets-fider.io")
+ env.Config.CDN.Host = "assets-fider.io"
Expect(ctx.GlobalAssetsURL("/assets/main.js")).Equals("http://cdn.assets-fider.io/assets/main.js")
Expect(ctx.GlobalAssetsURL("/assets/main.css")).Equals("http://cdn.assets-fider.io/assets/main.css")
}
@@ -157,7 +157,7 @@ func TestGlobalAssetsURL_MultiHostMode(t *testing.T) {
func TestTenantAssetsURL_SingleHostMode(t *testing.T) {
RegisterT(t)
- os.Setenv("HOST_MODE", "single")
+ env.Config.HostMode = "single"
ctx := newGetContext("http://feedback.theavengers.com:3000", nil)
ctx.SetTenant(&models.Tenant{
ID: 1,
@@ -167,7 +167,7 @@ func TestTenantAssetsURL_SingleHostMode(t *testing.T) {
Expect(ctx.TenantAssetsURL("/assets/main.js")).Equals("http://feedback.theavengers.com:3000/assets/main.js")
Expect(ctx.TenantAssetsURL("/assets/main.css")).Equals("http://feedback.theavengers.com:3000/assets/main.css")
- os.Setenv("CDN_HOST", "assets-fider.io")
+ env.Config.CDN.Host = "assets-fider.io"
Expect(ctx.TenantAssetsURL("/assets/main.js")).Equals("http://assets-fider.io/assets/main.js")
Expect(ctx.TenantAssetsURL("/assets/main.css")).Equals("http://assets-fider.io/assets/main.css")
}
@@ -175,7 +175,7 @@ func TestTenantAssetsURL_SingleHostMode(t *testing.T) {
func TestTenantAssetsURL_MultiHostMode(t *testing.T) {
RegisterT(t)
- os.Setenv("HOST_MODE", "multi")
+ env.Config.HostMode = "multi"
ctx := newGetContext("http://theavengers.test.fider.io:3000", nil)
ctx.SetTenant(&models.Tenant{
ID: 1,
@@ -186,7 +186,7 @@ func TestTenantAssetsURL_MultiHostMode(t *testing.T) {
Expect(ctx.TenantAssetsURL("/assets/main.js")).Equals("http://theavengers.test.fider.io:3000/assets/main.js")
Expect(ctx.TenantAssetsURL("/assets/main.css")).Equals("http://theavengers.test.fider.io:3000/assets/main.css")
- os.Setenv("CDN_HOST", "assets-fider.io")
+ env.Config.CDN.Host = "assets-fider.io"
Expect(ctx.TenantAssetsURL("/assets/main.js")).Equals("http://theavengers.assets-fider.io/assets/main.js")
Expect(ctx.TenantAssetsURL("/assets/main.css")).Equals("http://theavengers.assets-fider.io/assets/main.css")
}
diff --git a/app/pkg/web/engine.go b/app/pkg/web/engine.go
index cab7a935f..a51ca91ec 100644
--- a/app/pkg/web/engine.go
+++ b/app/pkg/web/engine.go
@@ -90,18 +90,17 @@ func New(settings *models.SystemSettings) *Engine {
func (e *Engine) Start(address string) {
e.logger.Info("Application is starting")
e.logger.Infof("GO_ENV: @{Env}", log.Props{
- "Env": env.Current(),
+ "Env": env.Config.Environment,
})
var (
- autoSSL = env.GetEnvOrDefault("SSL_AUTO", "")
certFilePath = ""
keyFilePath = ""
)
- if env.IsDefined("SSL_CERT") {
- certFilePath = env.Etc(env.GetEnvOrDefault("SSL_CERT", ""))
- keyFilePath = env.Etc(env.GetEnvOrDefault("SSL_CERT_KEY", ""))
+ if env.Config.SSLCert != "" {
+ certFilePath = env.Etc(env.Config.SSLCert)
+ keyFilePath = env.Etc(env.Config.SSLCertKey)
}
e.server = &http.Server{
@@ -122,7 +121,7 @@ func (e *Engine) Start(address string) {
err error
certManager *CertificateManager
)
- if autoSSL == "true" {
+ if env.Config.AutoSSL {
certManager, err = NewCertificateManager(certFilePath, keyFilePath, e.db.Connection())
if err != nil {
panic(errors.Wrap(err, "failed to initialize CertificateManager"))
diff --git a/app/pkg/web/oauth.go b/app/pkg/web/oauth.go
index 939e45b8b..4c289eb21 100644
--- a/app/pkg/web/oauth.go
+++ b/app/pkg/web/oauth.go
@@ -5,9 +5,10 @@ import (
"io/ioutil"
"net/http"
"net/url"
- "os"
"strings"
+ "github.com/getfider/fider/app/pkg/env"
+
jwt "github.com/dgrijalva/jwt-go"
"github.com/getfider/fider/app/pkg/validate"
"github.com/getfider/fider/app/storage"
@@ -24,8 +25,8 @@ import (
"golang.org/x/oauth2/google"
)
-func getProviderStatus(envKey string) int {
- if os.Getenv(envKey) == "" {
+func getProviderStatus(key string) int {
+ if key == "" {
return models.OAuthConfigDisabled
}
return models.OAuthConfigEnabled
@@ -37,9 +38,9 @@ var (
Provider: oauth.FacebookProvider,
DisplayName: "Facebook",
ProfileURL: "https://graph.facebook.com/me?fields=name,email",
- Status: getProviderStatus("OAUTH_FACEBOOK_APPID"),
- ClientID: os.Getenv("OAUTH_FACEBOOK_APPID"),
- ClientSecret: os.Getenv("OAUTH_FACEBOOK_SECRET"),
+ Status: getProviderStatus(env.Config.OAuth.Facebook.AppID),
+ ClientID: env.Config.OAuth.Facebook.AppID,
+ ClientSecret: env.Config.OAuth.Facebook.Secret,
Scope: "public_profile email",
AuthorizeURL: facebook.Endpoint.AuthURL,
TokenURL: facebook.Endpoint.TokenURL,
@@ -51,9 +52,9 @@ var (
Provider: oauth.GoogleProvider,
DisplayName: "Google",
ProfileURL: "https://www.googleapis.com/plus/v1/people/me",
- Status: getProviderStatus("OAUTH_GOOGLE_CLIENTID"),
- ClientID: os.Getenv("OAUTH_GOOGLE_CLIENTID"),
- ClientSecret: os.Getenv("OAUTH_GOOGLE_SECRET"),
+ Status: getProviderStatus(env.Config.OAuth.Google.ClientID),
+ ClientID: env.Config.OAuth.Google.ClientID,
+ ClientSecret: env.Config.OAuth.Google.Secret,
Scope: "profile email",
AuthorizeURL: google.Endpoint.AuthURL,
TokenURL: google.Endpoint.TokenURL,
@@ -65,9 +66,9 @@ var (
Provider: oauth.GitHubProvider,
DisplayName: "GitHub",
ProfileURL: "https://api.github.com/user",
- Status: getProviderStatus("OAUTH_GITHUB_CLIENTID"),
- ClientID: os.Getenv("OAUTH_GITHUB_CLIENTID"),
- ClientSecret: os.Getenv("OAUTH_GITHUB_SECRET"),
+ Status: getProviderStatus(env.Config.OAuth.GitHub.ClientID),
+ ClientID: env.Config.OAuth.GitHub.ClientID,
+ ClientSecret: env.Config.OAuth.GitHub.Secret,
Scope: "user:email",
AuthorizeURL: github.Endpoint.AuthURL,
TokenURL: github.Endpoint.TokenURL,
diff --git a/app/pkg/web/util/webutil_test.go b/app/pkg/web/util/webutil_test.go
index d3702c01d..64b1d435f 100644
--- a/app/pkg/web/util/webutil_test.go
+++ b/app/pkg/web/util/webutil_test.go
@@ -2,10 +2,10 @@ package webutil_test
import (
"net/url"
- "os"
"testing"
- "github.com/getfider/fider/app/pkg/web/util"
+ "github.com/getfider/fider/app/pkg/env"
+ webutil "github.com/getfider/fider/app/pkg/web/util"
. "github.com/getfider/fider/app/pkg/assert"
"github.com/getfider/fider/app/pkg/web"
@@ -26,10 +26,10 @@ func TestGetOAuthBaseURL(t *testing.T) {
ctx := newContext("https://mydomain.com/hello-world")
- os.Setenv("HOST_MODE", "multi")
+ env.Config.HostMode = "multi"
Expect(webutil.GetOAuthBaseURL(ctx)).Equals("https://login.test.fider.io")
- os.Setenv("HOST_MODE", "single")
+ env.Config.HostMode = "single"
Expect(webutil.GetOAuthBaseURL(ctx)).Equals("https://mydomain.com")
}
@@ -38,9 +38,9 @@ func TestGetOAuthBaseURL_WithPort(t *testing.T) {
ctx := newContext("http://demo.test.fider.io:3000/hello-world")
- os.Setenv("HOST_MODE", "multi")
+ env.Config.HostMode = "multi"
Expect(webutil.GetOAuthBaseURL(ctx)).Equals("http://login.test.fider.io:3000")
- os.Setenv("HOST_MODE", "single")
+ env.Config.HostMode = "single"
Expect(webutil.GetOAuthBaseURL(ctx)).Equals("http://demo.test.fider.io:3000")
}
diff --git a/app/storage/postgres/tenant_test.go b/app/storage/postgres/tenant_test.go
index 13fbd3206..39862a102 100644
--- a/app/storage/postgres/tenant_test.go
+++ b/app/storage/postgres/tenant_test.go
@@ -2,7 +2,6 @@ package postgres_test
import (
"io/ioutil"
- "os"
"testing"
"time"
@@ -45,7 +44,7 @@ func TestTenantStorage_SingleTenant_Add(t *testing.T) {
SetupDatabaseTest(t)
defer TeardownDatabaseTest()
- os.Setenv("HOST_MODE", "single")
+ env.Config.HostMode = "single"
tenant, err := tenants.Add("My Domain Inc.", "mydomain", models.TenantPending)
Expect(err).IsNil()
diff --git a/main.go b/main.go
index 015c3fa47..80a4b9e36 100644
--- a/main.go
+++ b/main.go
@@ -29,9 +29,9 @@ func main() {
BuildTime: buildtime,
Version: version,
Compiler: runtime.Version(),
- Environment: env.Current(),
- GoogleAnalytics: env.GetEnvOrDefault("GOOGLE_ANALYTICS", ""),
- Mode: env.Mode(),
+ Environment: env.Config.Environment,
+ GoogleAnalytics: env.Config.GoogleAnalytics,
+ Mode: env.Config.HostMode,
Domain: env.MultiTenantDomain(),
HasLegal: env.HasLegal(),
}
diff --git a/vendor/github.com/joeshaw/envdecode/.gitignore b/vendor/github.com/joeshaw/envdecode/.gitignore
new file mode 100644
index 000000000..1faf59edf
--- /dev/null
+++ b/vendor/github.com/joeshaw/envdecode/.gitignore
@@ -0,0 +1,2 @@
+
+example/example
diff --git a/vendor/github.com/joeshaw/envdecode/.travis.yml b/vendor/github.com/joeshaw/envdecode/.travis.yml
new file mode 100644
index 000000000..9b84a963c
--- /dev/null
+++ b/vendor/github.com/joeshaw/envdecode/.travis.yml
@@ -0,0 +1,5 @@
+language: go
+go:
+ - 1.9.x
+ - master
+
diff --git a/vendor/github.com/joeshaw/envdecode/LICENSE b/vendor/github.com/joeshaw/envdecode/LICENSE
new file mode 100644
index 000000000..5869b24ec
--- /dev/null
+++ b/vendor/github.com/joeshaw/envdecode/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2014 Joe Shaw
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/vendor/github.com/joeshaw/envdecode/README.md b/vendor/github.com/joeshaw/envdecode/README.md
new file mode 100644
index 000000000..c51467e53
--- /dev/null
+++ b/vendor/github.com/joeshaw/envdecode/README.md
@@ -0,0 +1,89 @@
+# envdecode [![Travis-CI](https://travis-ci.org/joeshaw/envdecode.svg)](https://travis-ci.org/joeshaw/envdecode) [![GoDoc](https://godoc.org/github.com/joeshaw/envdecode?status.svg)](https://godoc.org/github.com/joeshaw/envdecode)
+
+`envdecode` is a Go package for populating structs from environment
+variables.
+
+`envdecode` uses struct tags to map environment variables to fields,
+allowing you you use any names you want for environment variables.
+`envdecode` will recurse into nested structs, including pointers to
+nested structs, but it will not allocate new pointers to structs.
+
+## API
+
+Full API docs are available on
+[godoc.org](https://godoc.org/github.com/joeshaw/envdecode).
+
+Define a struct with `env` struct tags:
+
+```go
+type Config struct {
+ Hostname string `env:"SERVER_HOSTNAME,default=localhost"`
+ Port uint16 `env:"SERVER_PORT,default=8080"`
+
+ AWS struct {
+ ID string `env:"AWS_ACCESS_KEY_ID"`
+ Secret string `env:"AWS_SECRET_ACCESS_KEY,required"`
+ SnsTopics []string `env:"AWS_SNS_TOPICS"`
+ }
+
+ Timeout time.Duration `env:"TIMEOUT,default=1m,strict"`
+}
+```
+
+Fields *must be exported* (i.e. begin with a capital letter) in order
+for `envdecode` to work with them. An error will be returned if a
+struct with no exported fields is decoded (including one that contains
+no `env` tags at all).
+Default values may be provided by appending ",default=value" to the
+struct tag. Required values may be marked by appending ",required" to the
+struct tag. Strict values may be marked by appending ",strict" which will
+return an error on Decode if there is an error while parsing.
+
+Then call `envdecode.Decode`:
+
+```go
+var cfg Config
+err := envdecode.Decode(&cfg)
+```
+
+If you want all fields to act `strict`, you may use `envdecode.StrictDecode`:
+
+```go
+var cfg Config
+err := envdecode.StrictDecode(&cfg)
+```
+
+All parse errors will fail fast and return an error in this mode.
+
+## Supported types
+
+* Structs (and pointer to structs)
+* Slices of below defined types, separated by semicolon
+* `bool`
+* `float32`, `float64`
+* `int`, `int8`, `int16`, `int32`, `int64`
+* `uint`, `uint8`, `uint16`, `uint32`, `uint64`
+* `string`
+* `time.Duration`, using the [`time.ParseDuration()` format](http://golang.org/pkg/time/#ParseDuration)
+* `*url.URL`, using [`url.Parse()`](https://godoc.org/net/url#Parse)
+* Types those implement a `Decoder` interface
+
+## Custom `Decoder`
+
+If you want a field to be decoded with custom behavior, you may implement the interface `Decoder` for the filed type.
+
+```go
+type Config struct {
+ IPAddr IP `env:"IP_ADDR"`
+}
+
+type IP net.IP
+
+// Decode implements the interface `envdecode.Decoder`
+func (i *IP) Decode(repl string) error {
+ *i = net.ParseIP(repl)
+ return nil
+}
+```
+
+`Decoder` is the interface implemented by an object that can decode an environment variable string representation of itself.
diff --git a/vendor/github.com/joeshaw/envdecode/envdecode.go b/vendor/github.com/joeshaw/envdecode/envdecode.go
new file mode 100644
index 000000000..0ad8aad9f
--- /dev/null
+++ b/vendor/github.com/joeshaw/envdecode/envdecode.go
@@ -0,0 +1,417 @@
+// Package envdecode is a package for populating structs from environment
+// variables, using struct tags.
+package envdecode
+
+import (
+ "errors"
+ "fmt"
+ "log"
+ "net/url"
+ "os"
+ "reflect"
+ "sort"
+ "strconv"
+ "strings"
+ "time"
+)
+
+// ErrInvalidTarget indicates that the target value passed to
+// Decode is invalid. Target must be a non-nil pointer to a struct.
+var ErrInvalidTarget = errors.New("target must be non-nil pointer to struct that has at least one exported field with a valid env tag.")
+var ErrNoTargetFieldsAreSet = errors.New("none of the target fields were set from environment variables")
+
+// FailureFunc is called when an error is encountered during a MustDecode
+// operation. It prints the error and terminates the process.
+//
+// This variable can be assigned to another function of the user-programmer's
+// design, allowing for graceful recovery of the problem, such as loading
+// from a backup configuration file.
+var FailureFunc = func(err error) {
+ log.Fatalf("envdecode: an error was encountered while decoding: %v\n", err)
+}
+
+// Decoder is the interface implemented by an object that can decode an
+// environment variable string representation of itself.
+type Decoder interface {
+ Decode(string) error
+}
+
+// Decode environment variables into the provided target. The target
+// must be a non-nil pointer to a struct. Fields in the struct must
+// be exported, and tagged with an "env" struct tag with a value
+// containing the name of the environment variable. An error is
+// returned if there are no exported members tagged.
+//
+// Default values may be provided by appending ",default=value" to the
+// struct tag. Required values may be marked by appending ",required"
+// to the struct tag. It is an error to provide both "default" and
+// "required". Strict values may be marked by appending ",strict" which
+// will return an error on Decode if there is an error while parsing.
+// If everything must be strict, consider using StrictDecode instead.
+//
+// All primitive types are supported, including bool, floating point,
+// signed and unsigned integers, and string. Boolean and numeric
+// types are decoded using the standard strconv Parse functions for
+// those types. Structs and pointers to structs are decoded
+// recursively. time.Duration is supported via the
+// time.ParseDuration() function and *url.URL is supported via the
+// url.Parse() function. Slices are supported for all above mentioned
+// primitive types. Semicolon is used as delimiter in environment variables.
+func Decode(target interface{}) error {
+ nFields, err := decode(target, false)
+ if err != nil {
+ return err
+ }
+
+ // if we didn't do anything - the user probably did something
+ // wrong like leave all fields unexported.
+ if nFields == 0 {
+ return ErrNoTargetFieldsAreSet
+ }
+
+ return nil
+}
+
+// StrictDecode is similar to Decode except all fields will have an implicit
+// ",strict" on all fields.
+func StrictDecode(target interface{}) error {
+ nFields, err := decode(target, true)
+ if err != nil {
+ return err
+ }
+
+ // if we didn't do anything - the user probably did something
+ // wrong like leave all fields unexported.
+ if nFields == 0 {
+ return ErrInvalidTarget
+ }
+
+ return nil
+}
+
+func decode(target interface{}, strict bool) (int, error) {
+ s := reflect.ValueOf(target)
+ if s.Kind() != reflect.Ptr || s.IsNil() {
+ return 0, ErrInvalidTarget
+ }
+
+ s = s.Elem()
+ if s.Kind() != reflect.Struct {
+ return 0, ErrInvalidTarget
+ }
+
+ t := s.Type()
+ setFieldCount := 0
+ for i := 0; i < s.NumField(); i++ {
+ // Localize the umbrella `strict` value to the specific field.
+ strict := strict
+
+ f := s.Field(i)
+
+ switch f.Kind() {
+ case reflect.Ptr:
+ if f.Elem().Kind() != reflect.Struct {
+ break
+ }
+
+ f = f.Elem()
+ fallthrough
+
+ case reflect.Struct:
+ if !f.Addr().CanInterface() {
+ continue
+ }
+
+ ss := f.Addr().Interface()
+ _, custom := ss.(Decoder)
+ if custom {
+ break
+ }
+
+ n, err := decode(ss, strict)
+ if err != nil {
+ return 0, err
+ }
+ setFieldCount += n
+ }
+
+ if !f.CanSet() {
+ continue
+ }
+
+ tag := t.Field(i).Tag.Get("env")
+ if tag == "" {
+ continue
+ }
+
+ parts := strings.Split(tag, ",")
+ env := os.Getenv(parts[0])
+
+ required := false
+ hasDefault := false
+ defaultValue := ""
+
+ for _, o := range parts[1:] {
+ if !required {
+ required = strings.HasPrefix(o, "required")
+ }
+ if strings.HasPrefix(o, "default=") {
+ hasDefault = true
+ defaultValue = o[8:]
+ }
+ if !strict {
+ strict = strings.HasPrefix(o, "strict")
+ }
+ }
+
+ if required && hasDefault {
+ panic(`envdecode: "default" and "required" may not be specified in the same annotation`)
+ }
+ if env == "" && required {
+ return 0, fmt.Errorf("the environment variable \"%s\" is missing", parts[0])
+ }
+ if env == "" {
+ env = defaultValue
+ }
+ if env == "" {
+ continue
+ }
+
+ setFieldCount++
+
+ decoder, custom := f.Addr().Interface().(Decoder)
+ if custom {
+ if err := decoder.Decode(env); err != nil {
+ return 0, err
+ }
+ } else if f.Kind() == reflect.Slice {
+ decodeSlice(&f, env)
+ } else {
+ if err := decodePrimitiveType(&f, env); err != nil && strict {
+ return 0, err
+ }
+ }
+ }
+
+ return setFieldCount, nil
+}
+
+func decodeSlice(f *reflect.Value, env string) {
+ parts := strings.Split(env, ";")
+
+ values := parts[:0]
+ for _, x := range parts {
+ if x != "" {
+ values = append(values, strings.TrimSpace(x))
+ }
+ }
+
+ valuesCount := len(values)
+ slice := reflect.MakeSlice(f.Type(), valuesCount, valuesCount)
+ if valuesCount > 0 {
+ for i := 0; i < valuesCount; i++ {
+ e := slice.Index(i)
+ decodePrimitiveType(&e, values[i])
+ }
+ }
+
+ f.Set(slice)
+}
+
+func decodePrimitiveType(f *reflect.Value, env string) error {
+ switch f.Kind() {
+ case reflect.Bool:
+ v, err := strconv.ParseBool(env)
+ if err != nil {
+ return err
+ }
+ f.SetBool(v)
+
+ case reflect.Float32, reflect.Float64:
+ bits := f.Type().Bits()
+ v, err := strconv.ParseFloat(env, bits)
+ if err != nil {
+ return err
+ }
+ f.SetFloat(v)
+
+ case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
+ if t := f.Type(); t.PkgPath() == "time" && t.Name() == "Duration" {
+ v, err := time.ParseDuration(env)
+ if err != nil {
+ return err
+ }
+ f.SetInt(int64(v))
+ } else {
+ bits := f.Type().Bits()
+ v, err := strconv.ParseInt(env, 0, bits)
+ if err != nil {
+ return err
+ }
+ f.SetInt(v)
+ }
+ case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
+ bits := f.Type().Bits()
+ v, err := strconv.ParseUint(env, 0, bits)
+ if err != nil {
+ return err
+ }
+ f.SetUint(v)
+
+ case reflect.String:
+ f.SetString(env)
+
+ case reflect.Ptr:
+ if t := f.Type().Elem(); t.Kind() == reflect.Struct && t.PkgPath() == "net/url" && t.Name() == "URL" {
+ v, err := url.Parse(env)
+ if err != nil {
+ return err
+ }
+ f.Set(reflect.ValueOf(v))
+ }
+ }
+ return nil
+}
+
+// MustDecode calls Decode and terminates the process if any errors
+// are encountered.
+func MustDecode(target interface{}) {
+ err := Decode(target)
+ if err != nil {
+ FailureFunc(err)
+ }
+}
+
+// MustStrictDecode calls StrictDecode and terminates the process if any errors
+// are encountered.
+func MustStrictDecode(target interface{}) {
+ err := StrictDecode(target)
+ if err != nil {
+ FailureFunc(err)
+ }
+}
+
+//// Configuration info for Export
+
+type ConfigInfo struct {
+ Field string
+ EnvVar string
+ Value string
+ DefaultValue string
+ HasDefault bool
+ Required bool
+ UsesEnv bool
+}
+
+type ConfigInfoSlice []*ConfigInfo
+
+func (c ConfigInfoSlice) Less(i, j int) bool {
+ return c[i].EnvVar < c[j].EnvVar
+}
+func (c ConfigInfoSlice) Len() int {
+ return len(c)
+}
+func (c ConfigInfoSlice) Swap(i, j int) {
+ c[i], c[j] = c[j], c[i]
+}
+
+// Returns a list of final configuration metadata sorted by envvar name
+func Export(target interface{}) ([]*ConfigInfo, error) {
+ s := reflect.ValueOf(target)
+ if s.Kind() != reflect.Ptr || s.IsNil() {
+ return nil, ErrInvalidTarget
+ }
+
+ cfg := []*ConfigInfo{}
+
+ s = s.Elem()
+ if s.Kind() != reflect.Struct {
+ return nil, ErrInvalidTarget
+ }
+
+ t := s.Type()
+ for i := 0; i < s.NumField(); i++ {
+ f := s.Field(i)
+ fName := t.Field(i).Name
+
+ fElem := f
+ if f.Kind() == reflect.Ptr {
+ fElem = f.Elem()
+ }
+ if fElem.Kind() == reflect.Struct {
+ ss := fElem.Addr().Interface()
+ subCfg, err := Export(ss)
+ if err != ErrInvalidTarget {
+ f = fElem
+ for _, v := range subCfg {
+ v.Field = fmt.Sprintf("%s.%s", fName, v.Field)
+ cfg = append(cfg, v)
+ }
+ }
+ }
+
+ tag := t.Field(i).Tag.Get("env")
+ if tag == "" {
+ continue
+ }
+
+ parts := strings.Split(tag, ",")
+
+ ci := &ConfigInfo{
+ Field: fName,
+ EnvVar: parts[0],
+ UsesEnv: os.Getenv(parts[0]) != "",
+ }
+
+ for _, o := range parts[1:] {
+ if strings.HasPrefix(o, "default=") {
+ ci.HasDefault = true
+ ci.DefaultValue = o[8:]
+ } else if strings.HasPrefix(o, "required") {
+ ci.Required = true
+ }
+ }
+
+ if f.Kind() == reflect.Ptr && f.IsNil() {
+ ci.Value = ""
+ } else if stringer, ok := f.Interface().(fmt.Stringer); ok {
+ ci.Value = stringer.String()
+ } else {
+ switch f.Kind() {
+ case reflect.Bool:
+ ci.Value = strconv.FormatBool(f.Bool())
+
+ case reflect.Float32, reflect.Float64:
+ bits := f.Type().Bits()
+ ci.Value = strconv.FormatFloat(f.Float(), 'f', -1, bits)
+
+ case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
+ ci.Value = strconv.FormatInt(f.Int(), 10)
+
+ case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
+ ci.Value = strconv.FormatUint(f.Uint(), 10)
+
+ case reflect.String:
+ ci.Value = f.String()
+
+ case reflect.Slice:
+ ci.Value = fmt.Sprintf("%v", f.Interface())
+
+ default:
+ // Unable to determine string format for value
+ return nil, ErrInvalidTarget
+ }
+ }
+
+ cfg = append(cfg, ci)
+ }
+
+ // No configuration tags found, assume invalid input
+ if len(cfg) == 0 {
+ return nil, ErrInvalidTarget
+ }
+
+ sort.Sort(ConfigInfoSlice(cfg))
+
+ return cfg, nil
+}