Compare commits
2 Commits
da50161a92
...
538e23708a
| Author | SHA1 | Date | |
|---|---|---|---|
|
538e23708a
|
|||
|
8cc2de5c2a
|
5
config/config-missing-fields.yaml
Normal file
5
config/config-missing-fields.yaml
Normal file
@@ -0,0 +1,5 @@
|
||||
app:
|
||||
environment: "missingfield"
|
||||
|
||||
db:
|
||||
type: "typical"
|
||||
@@ -1,11 +1,11 @@
|
||||
app:
|
||||
environment: "devel"
|
||||
port: 8080
|
||||
port: 5151
|
||||
|
||||
db:
|
||||
type: "type"
|
||||
host: "host"
|
||||
type: "aoeu"
|
||||
host: "aeihn"
|
||||
port: 1234
|
||||
user: USER
|
||||
password: PASSWORD
|
||||
database: gogmagog
|
||||
database: g2
|
||||
|
||||
@@ -2,7 +2,6 @@ package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@@ -29,32 +28,45 @@ type Conf struct {
|
||||
Db DBConfig
|
||||
}
|
||||
|
||||
// GetConf returns config values
|
||||
func GetConf(filename string) *Conf {
|
||||
|
||||
func createDefaultConf() *Conf {
|
||||
cnf := &Conf{
|
||||
App: AppConfig{
|
||||
Environment: "local",
|
||||
Port: "8080",
|
||||
},
|
||||
Db: DBConfig{
|
||||
Type: "postgres",
|
||||
Host: "localhost",
|
||||
Port: "5432",
|
||||
User: "<user>",
|
||||
Password: "<password>",
|
||||
Database: "gogmagog",
|
||||
},
|
||||
}
|
||||
|
||||
return cnf
|
||||
}
|
||||
|
||||
// GetConf returns config values
|
||||
func GetConf(filename string) (*Conf, error) {
|
||||
|
||||
cnf := createDefaultConf()
|
||||
|
||||
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 load config file: \n", err)
|
||||
os.Exit(1)
|
||||
log.Print("Could not load config file: \n", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = vv.Unmarshal(&cnf)
|
||||
if err != nil {
|
||||
log.Fatal("Could not read config file into struct: \n", err)
|
||||
os.Exit(1)
|
||||
log.Print("Could not read config file into struct: \n", err)
|
||||
return nil, err
|
||||
}
|
||||
return cnf
|
||||
return cnf, nil
|
||||
}
|
||||
|
||||
@@ -1,26 +1,54 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
|
||||
"gitea.deepak.science/deepak/gogmagog/config"
|
||||
)
|
||||
|
||||
func TestTrue(t *testing.T) {
|
||||
func TestSample(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
conf := config.GetConf("config-sample")
|
||||
conf, err := config.GetConf("config-sample")
|
||||
assert.Nil(err)
|
||||
|
||||
appConf := conf.App
|
||||
assert.Equal("devel", appConf.Environment)
|
||||
assert.Equal("8080", appConf.Port)
|
||||
assert.Equal("5151", appConf.Port)
|
||||
|
||||
dbConf := conf.Db
|
||||
assert.Equal("type", dbConf.Type)
|
||||
assert.Equal("host", dbConf.Host)
|
||||
assert.Equal("aoeu", dbConf.Type)
|
||||
assert.Equal("aeihn", dbConf.Host)
|
||||
assert.Equal("1234", dbConf.Port)
|
||||
assert.Equal("USER", dbConf.User)
|
||||
assert.Equal("PASSWORD", dbConf.Password)
|
||||
assert.Equal("g2", dbConf.Database)
|
||||
}
|
||||
|
||||
func TestDefault(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
conf, err := config.GetConf("config-missing-fields")
|
||||
assert.Nil(err)
|
||||
|
||||
appConf := conf.App
|
||||
assert.Equal("missingfield", appConf.Environment)
|
||||
assert.Equal("8080", appConf.Port)
|
||||
|
||||
dbConf := conf.Db
|
||||
assert.Equal("typical", dbConf.Type)
|
||||
assert.Equal("localhost", dbConf.Host)
|
||||
assert.Equal("5432", dbConf.Port)
|
||||
assert.Equal("<user>", dbConf.User)
|
||||
assert.Equal("<password>", dbConf.Password)
|
||||
assert.Equal("gogmagog", dbConf.Database)
|
||||
}
|
||||
|
||||
func TestMissingFile(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
conf, err := config.GetConf("non-existent")
|
||||
assert.Nil(conf)
|
||||
assert.NotNil(err)
|
||||
}
|
||||
|
||||
7
main.go
7
main.go
@@ -5,11 +5,16 @@ import (
|
||||
"gitea.deepak.science/deepak/gogmagog/db"
|
||||
"gitea.deepak.science/deepak/gogmagog/models"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Config
|
||||
conf := config.GetConf("config")
|
||||
conf, err := config.GetConf("config")
|
||||
if err != nil {
|
||||
log.Fatal("Could not get config", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
appConf := conf.App
|
||||
port := appConf.Port
|
||||
|
||||
Reference in New Issue
Block a user