Use Golang to develop web service-2

2019-10-21
golang

opensource software

github.com/spf13/viper

Load configuration file

viper is configuration framework and it’s widely used in other opensource golang software such as hugo, kubernets.

1
go get "github.com/spf13/viper"

load json format

config file is like

1
2
3
4
5
json config example,
{
"server": "127.0.0.1:4000",
"appName": "autotesting"
}
1
2
3
4
5
6
7
8

//with following two lines, viper will load config.json file
//and parse the file
viper.SetConfigFile("$PATH/go/autoproxy/config.json")
viper.ReadInConfig()

server := viper.Get("server")
fmt.Println(server)

nested config with json format

nested json config example

1
2
3
4
5
6
7
8
{
"server": "127.0.0.1:4000",
"appName": "autotesting",
"nestedA": {
"addr": "127.0.0.1:6379",
"password": "test1234"
}
}
1
2
3
4
//code example to load config
nestedA := viper.GetStringMap("nestedA")
fmt.Println(nestedA["addr"].(string))
fmt.Println(nestedA["password"].(string))

load yaml format

config.yaml config example

1
2
3
4
5
6
7
environments:
dev:
server: http://dev.bar.com
name: Developer Setup
prod:
url: http://foo.bar.com
name: My Cool App
viper.SetConfigFile("$PATH/config.yaml")
viper.ReadInConfig()

//get the environments part
server := viper.Get("environments").(map[string]interface {})


//get the dev part
dev := server["dev"].(map[string]interface{})
fmt.Println("reading server:", dev["server"].(string))

`