-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
32 lines (30 loc) · 1.12 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main
import (
"log"
"github.com/spf13/viper"
)
// define a struct that will contain the allowed configurations.
// In our case, it is the port number and the MySQL connection string.
type Config struct {
Port string `mapstructure:"port"`
ConnectionString string `mapstructure:"connection_string"`
}
// define the AppConfig variable that will be accessed by other files and packages within the application code.
var AppConfig *Config
// use Viper to load configurations from the config.json file
// and assign its values to the AppConfig variable.
// it will call the LoadAppConfig function from the main program which in turn will be loading the data from the JSON file into the AppConfig variable. Pretty neat,
func LoadAppConfig(){
log.Println("Loading Server Configurations...")
viper.AddConfigPath(".")
viper.SetConfigName("config")//tells Viper that our configuration file is named config.
viper.SetConfigType("json")//Tells Viper that our config file is of JSON type.
err := viper.ReadInConfig()
if err != nil {
log.Fatal(err)
}
err = viper.Unmarshal(&AppConfig)
if err != nil {
log.Fatal(err)
}
}