Skip to content

Commit

Permalink
fix: refactor config package to return map, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dara Hayes committed Feb 22, 2018
1 parent 0fa5084 commit 6fb382e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 20 deletions.
6 changes: 3 additions & 3 deletions cmd/metrics-api/metrics-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {

dbHandler := dao.DatabaseHandler{}

err := dbHandler.Connect(config.DBHost, config.DBUser, config.DBPassword, config.DBName, config.SSLMode)
err := dbHandler.Connect(config["DBHost"], config["DBUser"], config["DBPassword"], config["DBName"], config["SSLMode"])

if err != nil {
panic("failed to connect to sql database : " + err.Error())
Expand All @@ -42,10 +42,10 @@ func main() {
web.HealthzRoute(router, healthHandler)
}

log.Printf("Starting application... going to listen on %v", config.ListenAddress)
log.Printf("Starting application... going to listen on %v", config["ListenAddress"])

//start
if err := http.ListenAndServe(config.ListenAddress, router); err != nil {
if err := http.ListenAndServe(config["ListenAddress"], router); err != nil {
panic("failed to start " + err.Error())
}
}
25 changes: 8 additions & 17 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ import (
"os"
)

type config struct {
DBHost string
DBUser string
DBPassword string
DBName string
SSLMode string
ListenAddress string
}

// Simple helper function to read an environment or return a default value
func getEnv(key string, defaultVal string) string {
if value := os.Getenv(key); value != "" {
Expand All @@ -22,13 +13,13 @@ func getEnv(key string, defaultVal string) string {
return defaultVal
}

func GetConfig() *config {
return &config{
DBHost: getEnv("PGHOST", "localhost"),
DBUser: getEnv("PGUSER", "postgres"),
DBPassword: getEnv("PGPASSWORD", "postgres"),
DBName: getEnv("PGDATABASE", "aerogear_mobile_metrics"),
SSLMode: getEnv("PGSSLMODE", "disable"),
ListenAddress: fmt.Sprintf(":%s", getEnv("PORT", "3000")),
func GetConfig() map[string]string {
return map[string]string{
"DBHost": getEnv("PGHOST", "localhost"),
"DBUser": getEnv("PGUSER", "postgres"),
"DBPassword": getEnv("PGPASSWORD", "postgres"),
"DBName": getEnv("PGDATABASE", "aerogear_mobile_metrics"),
"SSLMode": getEnv("PGSSLMODE", "disable"),
"ListenAddress": fmt.Sprintf(":%s", getEnv("PORT", "3000")),
}
}
81 changes: 81 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package config

import (
"fmt"
"os"
"reflect"
"testing"
)

func TestgetEnv(t *testing.T) {

}

func TestGetConfig(t *testing.T) {
expected := map[string]string{
"DBHost": "localhost",
"DBUser": "postgres",
"DBPassword": "postgres",
"DBName": "aerogear_mobile_metrics",
"SSLMode": "disable",
"ListenAddress": ":3000",
}

config := GetConfig()

if !reflect.DeepEqual(config, expected) {
t.Error("GetConfig() did not return expected result")
}
}

func TestGetConfigCustomEnvVariables(t *testing.T) {
expected := map[string]string{
"DBHost": "testing",
"DBUser": "testing",
"DBPassword": "testing",
"DBName": "testing",
"SSLMode": "testing",
"ListenAddress": ":testing",
}

os.Setenv("PGHOST", "testing")
os.Setenv("PGUSER", "testing")
os.Setenv("PGPASSWORD", "testing")
os.Setenv("PGDATABASE", "testing")
os.Setenv("PGSSLMODE", "testing")
os.Setenv("PORT", "testing")

config := GetConfig()

fmt.Println(config)

if !reflect.DeepEqual(config, expected) {
t.Error("GetConfig() did not return expected result")
}
}

func TestGetConfigEmptyEnvVariables(t *testing.T) {
expected := map[string]string{
"DBHost": "localhost",
"DBUser": "postgres",
"DBPassword": "postgres",
"DBName": "aerogear_mobile_metrics",
"SSLMode": "disable",
"ListenAddress": ":3000",
}

os.Setenv("PGHOST", "")
os.Setenv("PGUSER", "")
os.Setenv("PGPASSWORD", "")
os.Setenv("PGDATABASE", "")
os.Setenv("PGSSLMODE", "")
os.Setenv("PORT", "")

config := GetConfig()

fmt.Println(config)

if !reflect.DeepEqual(config, expected) {
t.Error("GetConfig() did not return expected result")
}
}

0 comments on commit 6fb382e

Please sign in to comment.