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"
)
// 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
}

View File

@ -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)
}

View File

@ -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 {

View File

@ -1,10 +1 @@
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() {
// 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")