Skip to content

Commit

Permalink
Merge pull request showwin#4 from luvtechno/load_env
Browse files Browse the repository at this point in the history
Load database credentials from env vars
  • Loading branch information
showwin authored May 2, 2018
2 parents 72f593f + 7417e0d commit c1a9408
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
17 changes: 17 additions & 0 deletions admin/benchmark.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"database/sql"
"flag"
"fmt"
"log"
"os"
"strconv"
"sync"
"time"
Expand All @@ -29,6 +31,21 @@ func loopBakugaiScenario(wg *sync.WaitGroup, m *sync.Mutex, finishTime time.Time
}
}

func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}

func getDB() (*sql.DB, error) {
user := getEnv("ISHOCON1_DB_USER", "ishocon")
pass := getEnv("ISHOCON1_DB_PASSWORD", "ishocon")
dbname := getEnv("ISHOCON1_DB_NAME", "ishocon1")
db, err := sql.Open("mysql", user+":"+pass+"@/"+dbname)
return db, err
}

func startBenchmark(workload int) {
getInitialize()
log.Print("Benchmark Start! Workload: " + strconv.Itoa(workload))
Expand Down
3 changes: 1 addition & 2 deletions admin/support.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"database/sql"
"math/rand"
"time"

Expand All @@ -20,7 +19,7 @@ func getUserInfo(id int) (int, string, string) {
id = getRand(1, 5000)
}
var email, password string
db, err := sql.Open("mysql", "ishocon:ishocon@/ishocon1")
db, err := getDB()
if err != nil {
panic(err.Error())
}
Expand Down
3 changes: 1 addition & 2 deletions admin/validator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"database/sql"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -224,7 +223,7 @@ func validateUsers(id int, loggedIn bool) {
}

func getTotalPay(userID int) string {
db, err := sql.Open("mysql", "ishocon:ishocon@/ishocon1")
db, err := getDB()
if err != nil {
panic(err.Error())
}
Expand Down
13 changes: 10 additions & 3 deletions webapp/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ import (

var db *sql.DB

func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}

func main() {
// database setting
user := os.Getenv("ISHOCON1_DB_USER")
pass := os.Getenv("ISHOCON1_DB_PASSWORD")
dbname := "ishocon1"
user := getEnv("ISHOCON1_DB_USER", "ishocon")
pass := getEnv("ISHOCON1_DB_PASSWORD", "ishocon")
dbname := getEnv("ISHOCON1_DB_NAME", "ishocon1")
db, _ = sql.Open("mysql", user+":"+pass+"@/"+dbname)
db.SetMaxIdleConns(5)

Expand Down

0 comments on commit c1a9408

Please sign in to comment.