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:
|
app:
|
||||||
environment: "devel"
|
environment: "devel"
|
||||||
port: 8080
|
port: 5151
|
||||||
|
|
||||||
db:
|
db:
|
||||||
type: "type"
|
type: "aoeu"
|
||||||
host: "host"
|
host: "aeihn"
|
||||||
port: 1234
|
port: 1234
|
||||||
user: USER
|
user: USER
|
||||||
password: PASSWORD
|
password: PASSWORD
|
||||||
database: gogmagog
|
database: g2
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
@@ -29,32 +28,45 @@ type Conf struct {
|
|||||||
Db DBConfig
|
Db DBConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetConf returns config values
|
func createDefaultConf() *Conf {
|
||||||
func GetConf(filename string) *Conf {
|
|
||||||
|
|
||||||
cnf := &Conf{
|
cnf := &Conf{
|
||||||
App: AppConfig{
|
App: AppConfig{
|
||||||
Environment: "local",
|
Environment: "local",
|
||||||
Port: "8080",
|
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 := viper.New()
|
||||||
vv.SetConfigName(filename)
|
vv.SetConfigName(filename)
|
||||||
vv.SetConfigType("yaml")
|
vv.SetConfigType("yaml")
|
||||||
vv.AddConfigPath(".")
|
vv.AddConfigPath(".")
|
||||||
log.Printf("Initialising config with filename %s", filename)
|
|
||||||
|
|
||||||
err := vv.ReadInConfig()
|
err := vv.ReadInConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Could not load config file: \n", err)
|
log.Print("Could not load config file: \n", err)
|
||||||
os.Exit(1)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = vv.Unmarshal(&cnf)
|
err = vv.Unmarshal(&cnf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Could not read config file into struct: \n", err)
|
log.Print("Could not read config file into struct: \n", err)
|
||||||
os.Exit(1)
|
return nil, err
|
||||||
}
|
}
|
||||||
return cnf
|
return cnf, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,54 @@
|
|||||||
package config_test
|
package config_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
|
||||||
"gitea.deepak.science/deepak/gogmagog/config"
|
"gitea.deepak.science/deepak/gogmagog/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTrue(t *testing.T) {
|
func TestSample(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
conf := config.GetConf("config-sample")
|
conf, err := config.GetConf("config-sample")
|
||||||
|
assert.Nil(err)
|
||||||
|
|
||||||
appConf := conf.App
|
appConf := conf.App
|
||||||
assert.Equal("devel", appConf.Environment)
|
assert.Equal("devel", appConf.Environment)
|
||||||
assert.Equal("8080", appConf.Port)
|
assert.Equal("5151", appConf.Port)
|
||||||
|
|
||||||
dbConf := conf.Db
|
dbConf := conf.Db
|
||||||
assert.Equal("type", dbConf.Type)
|
assert.Equal("aoeu", dbConf.Type)
|
||||||
assert.Equal("host", dbConf.Host)
|
assert.Equal("aeihn", dbConf.Host)
|
||||||
assert.Equal("1234", dbConf.Port)
|
assert.Equal("1234", dbConf.Port)
|
||||||
assert.Equal("USER", dbConf.User)
|
assert.Equal("USER", dbConf.User)
|
||||||
assert.Equal("PASSWORD", dbConf.Password)
|
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)
|
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/db"
|
||||||
"gitea.deepak.science/deepak/gogmagog/models"
|
"gitea.deepak.science/deepak/gogmagog/models"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Config
|
// 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
|
appConf := conf.App
|
||||||
port := appConf.Port
|
port := appConf.Port
|
||||||
|
|||||||
Reference in New Issue
Block a user