-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a config object to get the env vars related to the db conn
- Loading branch information
1 parent
5721850
commit 3af82ba
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package config | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// Config struct is used to store the db connection data. | ||
type Config struct { | ||
dbUser string | ||
dbPswd string | ||
dbHost string | ||
dbPort string | ||
dbName string | ||
dbDriver string | ||
testDBHost string | ||
testDBName string | ||
apiPort string | ||
migrate string | ||
} | ||
|
||
// Get returns a config object with the db connection data already in place. | ||
func Get() *Config { | ||
conf := &Config{} | ||
|
||
flag.StringVar(&conf.dbUser, "dbuser", os.Getenv("DB_USER"), "DB user name") | ||
flag.StringVar(&conf.dbPswd, "dbpswd", os.Getenv("DB_PASSWORD"), "DB pass") | ||
flag.StringVar(&conf.dbPort, "dbport", os.Getenv("DB_PORT"), "DB port") | ||
flag.StringVar(&conf.dbHost, "dbhost", os.Getenv("DB_HOST"), "DB host") | ||
flag.StringVar(&conf.dbName, "dbname", os.Getenv("DB_NAME"), "DB name") | ||
flag.StringVar(&conf.dbDriver, "dbdriver", os.Getenv("DB_DRIVER"), "DB driver") | ||
flag.StringVar(&conf.testDBHost, "testdbhost", os.Getenv("TEST_DB_HOST"), "test database host") | ||
flag.StringVar(&conf.testDBName, "testdbname", os.Getenv("TEST_DB_NAME"), "test database name") | ||
flag.StringVar(&conf.apiPort, "apiPort", os.Getenv("API_PORT"), "API Port") | ||
flag.StringVar(&conf.migrate, "migrate", "up", "specify if we should be migrating DB 'up' or 'down'") | ||
|
||
flag.Parse() | ||
|
||
return conf | ||
} | ||
|
||
// GetDBConnStr returns the connection string. | ||
func (c *Config) GetDBConnStr() string { | ||
return c.getDBConnStr(c.dbHost, c.dbName) | ||
} | ||
|
||
// GetTestDBConnStr returns the test connection string. | ||
func (c *Config) GetTestDBConnStr() string { | ||
return c.getDBConnStr(c.testDBHost, c.testDBName) | ||
} | ||
|
||
// getDBConnStr returns the connection string based on the provied host and db name. | ||
func (c *Config) getDBConnStr(dbhost, dbname string) string { | ||
switch c.dbDriver { | ||
case "postgres": | ||
return fmt.Sprintf("%s://%s:%s@%s:%s/%s?sslmode=mode", c.dbDriver, c.dbUser, c.dbPswd, dbhost, c.dbPort, dbname) | ||
case "mysql": | ||
return fmt.Sprintf("%s://%s:%s@tcp(%s:%s)/%s", c.dbDriver, c.dbUser, c.dbPswd, dbhost, c.dbPort, dbname) | ||
default: | ||
return "" | ||
} | ||
} | ||
|
||
// GetMigration return up or down string to instruct the program if it should migrate database up or down. | ||
func (c *Config) GetMigration() string { | ||
return c.migrate | ||
} | ||
|
||
// Driver returns the db driver from config. | ||
func (c *Config) Driver() string { | ||
return c.dbDriver | ||
} |