Simplify the env vars (#225)

This commit is contained in:
Sung Won Cho 2019-07-23 15:55:23 +10:00 committed by GitHub
commit 73d66eea07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 43 additions and 69 deletions

View file

@ -1,6 +1,10 @@
DB_HOST=localhost
POSTGRES_DB=dnote
POSTGRES_USER=postgres
GO_ENV=DEVELOPMENT
DBHost=localhost
DBPost=5432
DBName=dnote
DBUser=postgres
DBPassword=
SmtpUsername=mock-SmtpUsername
SmtpPassword=mock-SmtpPassword

11
pkg/server/.env.test Normal file
View file

@ -0,0 +1,11 @@
GO_ENV=TEST
DBHost=localhost
DBPort=5432
DBName=dnote_test
DBUser=postgres
DBPassword=
SmtpUsername=mock-SmtpUsername
SmtpPassword=mock-SmtpPassword
SmtpHost=mock-SmtpHost

View file

@ -1,20 +0,0 @@
Host=http://127.0.0.1
Port=5000
WebPort=3000
GO_ENV=DEVELOPMENT
DB_HOST=localhost
POSTGRES_DB=dnote
POSTGRES_USER=sung
SmtpUsername=mock-SmtpUsername
SmtpPassword=mock-SmtpPassword
SmtpHost=mock-SmtpHost
GithubClientID=mock-github-client-id
GithubClientSecret=mock-github-client-secret
GoogleClientID=mock-google-client-id
GoogleClientSecret=mock-google-client-secret
StripeSecretKey=mock-stripe-secret-key
StripeWebhookSecret=mock-webhook-secret

View file

@ -1,20 +0,0 @@
Host=http://127.0.0.1
Port=5000
WebPort=3000
GO_ENV=DEVELOPMENT
DB_HOST=localhost
POSTGRES_DB=dnote_test
POSTGRES_USER=postgres
SmtpUsername=mock-SmtpUsername
SmtpPassword=mock-SmtpPassword
SmtpHost=mock-SmtpHost
GithubClientID=mock-github-client-id
GithubClientSecret=mock-github-client-secret
GoogleClientID=mock-google-client-id
GoogleClientSecret=mock-google-client-secret
StripeSecretKey=mock-stripe-secret-key
StripeWebhookSecret=mock-webhook-secret

View file

@ -305,7 +305,7 @@ func applyMiddleware(h http.Handler, rateLimit bool) http.Handler {
ret := h
ret = logging(ret)
if rateLimit && os.Getenv("GO_ENV") == "PRODUCTION" {
if rateLimit && os.Getenv("GO_ENV") != "TEST" {
ret = limit(ret)
}

View file

@ -35,7 +35,12 @@ func Init() error {
var endpoint string
if os.Getenv("GO_ENV") == "PRODUCTION" {
endpoint = "logs7.papertrailapp.com:37297"
endpoint = os.Getenv("LogEndpoint")
if endpoint == "" {
log.Println("No log endpoint provided. Not aggregating system logs.")
return nil
}
writer, err = syslog.Dial("udp", endpoint, syslog.LOG_DEBUG|syslog.LOG_KERN, "dnote-api")
if err != nil {

View file

@ -3,11 +3,10 @@
# test-local.sh runs api tests using local setting
set -eux
basePath=$GOPATH/src/github.com/dnote/dnote/pkg/server/api
basePath=$GOPATH/src/github.com/dnote/dnote/pkg/server
set -a
source "$basePath"/.env.test
source "$basePath/.env.test"
set +a
"$basePath"/scripts/test.sh
"$basePath/api/scripts/test.sh"

View file

@ -34,22 +34,13 @@ var (
)
func getPGConnectionString() string {
if os.Getenv("GO_ENV") == "PRODUCTION" {
return fmt.Sprintf(
"host=%s port=%s dbname=%s user=%s password=%s",
os.Getenv("DBHost"),
os.Getenv("DBPort"),
os.Getenv("DBName"),
os.Getenv("DBUser"),
os.Getenv("DBPassword"),
)
}
return fmt.Sprintf(
"host=%s dbname=%s user=%s sslmode=disable",
os.Getenv("DB_HOST"),
os.Getenv("POSTGRES_DB"),
os.Getenv("POSTGRES_USER"),
"host=%s port=%s dbname=%s user=%s password=%s",
os.Getenv("DBHost"),
os.Getenv("DBPort"),
os.Getenv("DBName"),
os.Getenv("DBUser"),
os.Getenv("DBPassword"),
)
}
@ -69,7 +60,9 @@ const (
func InitDB() {
var err error
DBConn, err = gorm.Open("postgres", getPGConnectionString())
connStr := getPGConnectionString()
DBConn, err = gorm.Open("postgres", connStr)
if err != nil {
panic(err)
}

View file

@ -38,7 +38,7 @@ func init() {
// Load env
if os.Getenv("GO_ENV") != "PRODUCTION" {
if err := godotenv.Load("../../api/.env.dev"); err != nil {
if err := godotenv.Load("../../.env.dev"); err != nil {
panic(err)
}
}

View file

@ -1,6 +1,8 @@
DB_HOST=localhost
POSTGRES_USER=sung
POSTGRES_DB=dnote
DBHost=localhost
DBPort=5432
DBName=dnote
DBUser=postgres
DBPassword=
SmtpUsername=mock-SmtpUsername
SmtpPassword=mock-SmtpPassword