diff --git a/config/config-sample.yaml b/config/config-sample.yaml new file mode 100644 index 0000000..239a9a4 --- /dev/null +++ b/config/config-sample.yaml @@ -0,0 +1,11 @@ +app: + environment: "devel" + port: 8080 + +db: + type: "type" + host: "host" + port: 1234 + user: USER + password: PASSWORD + database: gogmagog diff --git a/config/config.go b/config/config.go index f48d341..578764a 100644 --- a/config/config.go +++ b/config/config.go @@ -7,40 +7,54 @@ import ( "github.com/spf13/viper" ) -// Key represents a particular config flag. -type Key string - -// These constants are the list of keys that can be used. -const ( - Environment Key = `app.environment` - Port Key = `app.port` - DBType Key = `db.type` - DBHost Key = `db.host` - DBPort Key = `db.port` - DBUser Key = `db.user` - DBPassword Key = `db.password` - DBDatabase Key = `db.database` -) - -// GetString returns the string value of the provided config key. -func (k Key) GetString() string { - return viper.GetString(string(k)) +// AppConfig represents the config flags for the base application. +type AppConfig struct { + Environment string + Port string } -// Init sets all of the viper config parameters. -func Init() { +// DBConfig is the config for the DB connection. +type DBConfig struct { + Type string + Host string + Port string + User string + Password string + Database string +} - viper.SetDefault(string(Environment), "local") - viper.SetDefault(string(Port), "8080") +// Conf represents the overall configuration of the application. +type Conf struct { + App AppConfig + Db DBConfig +} - log.Print("Initialising config...") - viper.SetConfigName("config") - viper.SetConfigType("yaml") - viper.AddConfigPath(".") +// GetConf returns config values +func GetConf(filename string) *Conf { - err := viper.ReadInConfig() + cnf := &Conf{ + App: AppConfig{ + Environment: "local", + Port: "8080", + }, + } + + vv := viper.New() + vv.SetConfigName(filename) + vv.SetConfigType("yaml") + vv.AddConfigPath(".") + log.Printf("Initialising config with filename %s", filename) + + err := vv.ReadInConfig() if err != nil { - log.Fatal("Could not read config file: \n", err) + log.Fatal("Could not load config file: \n", err) os.Exit(1) } + + err = vv.Unmarshal(&cnf) + if err != nil { + log.Fatal("Could not read config file into struct: \n", err) + os.Exit(1) + } + return cnf } diff --git a/config/config_test.go b/config/config_test.go index 2ad4d0a..8b8c903 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,10 +1,26 @@ package config_test import ( - "log" "testing" + "github.com/stretchr/testify/assert" + + "gitea.deepak.science/deepak/gogmagog/config" ) func TestTrue(t *testing.T) { - log.Print("no real test") + assert := assert.New(t) + + conf := config.GetConf("config-sample") + + appConf := conf.App + assert.Equal("devel", appConf.Environment) + assert.Equal("8080", appConf.Port) + + dbConf := conf.Db + assert.Equal("type", dbConf.Type) + assert.Equal("host", dbConf.Host) + assert.Equal("1234", dbConf.Port) + assert.Equal("USER", dbConf.User) + assert.Equal("PASSWORD", dbConf.Password) + assert.Equal("gogmagog", dbConf.Database) } diff --git a/db/postgres.go b/db/postgres.go index 33a40fe..38bbe04 100644 --- a/db/postgres.go +++ b/db/postgres.go @@ -23,19 +23,19 @@ type postgresStore struct { } // GetStore provides a store to back the model based on a postgres connection. -func GetStore() models.Store { +func GetStore(dbConf *config.DBConfig) models.Store { - if config.DBType.GetString() != "postgres" { - log.Fatalf("Unsupported database type: " + config.DBType.GetString()) + if dbConf.Type != "postgres" { + log.Fatalf("Unsupported database type: " + dbConf.Type) os.Exit(1) } connStr := fmt.Sprintf("user=%s password=%s host=%s port=%s database=%s sslmode=disable", - config.DBUser.GetString(), - config.DBPassword.GetString(), - config.DBHost.GetString(), - config.DBPort.GetString(), - config.DBDatabase.GetString()) + dbConf.User, + dbConf.Password, + dbConf.Host, + dbConf.Port, + dbConf.Database) tmp, err := sql.Open("pgx", connStr) if err != nil { diff --git a/db/postgres_test.go b/db/postgres_test.go index e3451e8..ae1eb85 100644 --- a/db/postgres_test.go +++ b/db/postgres_test.go @@ -1,10 +1 @@ package db_test - -import ( - "log" - "testing" -) - -func TestTrue(t *testing.T) { - log.Print("no real test") -} diff --git a/main.go b/main.go index 92bab00..d734f34 100644 --- a/main.go +++ b/main.go @@ -9,15 +9,17 @@ import ( func main() { // Config - config.Init() - port := config.Port.GetString() - env := config.Environment.GetString() + conf := config.GetConf("config") + + appConf := conf.App + port := appConf.Port + env := appConf.Environment log.Print("Running server on " + port) log.Print("App environment is " + env) // DB - store := db.GetStore() + store := db.GetStore(&conf.Db) if store != nil { log.Print("Got DB connection")