Makes config testable
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good

This commit is contained in:
Deepak Mallubhotla 2020-12-28 16:30:10 -06:00
parent 3977d395f3
commit da50161a92
Signed by: deepak
GPG Key ID: 64BF53A3369104E7
6 changed files with 85 additions and 51 deletions

11
config/config-sample.yaml Normal file
View File

@ -0,0 +1,11 @@
app:
environment: "devel"
port: 8080
db:
type: "type"
host: "host"
port: 1234
user: USER
password: PASSWORD
database: gogmagog

View File

@ -7,40 +7,54 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
// Key represents a particular config flag. // AppConfig represents the config flags for the base application.
type Key string type AppConfig struct {
Environment string
// These constants are the list of keys that can be used. Port string
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))
} }
// Init sets all of the viper config parameters. // DBConfig is the config for the DB connection.
func Init() { type DBConfig struct {
Type string
Host string
Port string
User string
Password string
Database string
}
viper.SetDefault(string(Environment), "local") // Conf represents the overall configuration of the application.
viper.SetDefault(string(Port), "8080") type Conf struct {
App AppConfig
Db DBConfig
}
log.Print("Initialising config...") // GetConf returns config values
viper.SetConfigName("config") func GetConf(filename string) *Conf {
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
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 { if err != nil {
log.Fatal("Could not read config file: \n", err) log.Fatal("Could not load config file: \n", err)
os.Exit(1) 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
} }

View File

@ -1,10 +1,26 @@
package config_test package config_test
import ( import (
"log"
"testing" "testing"
"github.com/stretchr/testify/assert"
"gitea.deepak.science/deepak/gogmagog/config"
) )
func TestTrue(t *testing.T) { 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)
} }

View File

@ -23,19 +23,19 @@ type postgresStore struct {
} }
// GetStore provides a store to back the model based on a postgres connection. // 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" { if dbConf.Type != "postgres" {
log.Fatalf("Unsupported database type: " + config.DBType.GetString()) log.Fatalf("Unsupported database type: " + dbConf.Type)
os.Exit(1) os.Exit(1)
} }
connStr := fmt.Sprintf("user=%s password=%s host=%s port=%s database=%s sslmode=disable", connStr := fmt.Sprintf("user=%s password=%s host=%s port=%s database=%s sslmode=disable",
config.DBUser.GetString(), dbConf.User,
config.DBPassword.GetString(), dbConf.Password,
config.DBHost.GetString(), dbConf.Host,
config.DBPort.GetString(), dbConf.Port,
config.DBDatabase.GetString()) dbConf.Database)
tmp, err := sql.Open("pgx", connStr) tmp, err := sql.Open("pgx", connStr)
if err != nil { if err != nil {

View File

@ -1,10 +1 @@
package db_test package db_test
import (
"log"
"testing"
)
func TestTrue(t *testing.T) {
log.Print("no real test")
}

10
main.go
View File

@ -9,15 +9,17 @@ import (
func main() { func main() {
// Config // Config
config.Init() conf := config.GetConf("config")
port := config.Port.GetString()
env := config.Environment.GetString() appConf := conf.App
port := appConf.Port
env := appConf.Environment
log.Print("Running server on " + port) log.Print("Running server on " + port)
log.Print("App environment is " + env) log.Print("App environment is " + env)
// DB // DB
store := db.GetStore() store := db.GetStore(&conf.Db)
if store != nil { if store != nil {
log.Print("Got DB connection") log.Print("Got DB connection")