/
config.go
60 lines (53 loc) · 1.48 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package config
import (
"fmt"
"log"
"github.com/fsnotify/fsnotify"
"github.com/globalsign/mgo"
"github.com/spf13/viper"
)
type Constants struct {
PORT string
Mongo struct {
URL string
DBName string
}
}
type Config struct {
Constants
Database *mgo.Database
}
// NewConfig is used to generate a configuration instance which will be passed around the codebase
func New() (*Config, error) {
config := Config{}
constants, err := initViper()
config.Constants = constants
if err != nil {
return &config, err
}
dbSession, err := mgo.Dial(config.Constants.Mongo.URL)
if err != nil {
return &config, err
}
config.Database = dbSession.DB(config.Constants.Mongo.DBName)
return &config, err
}
func initViper() (Constants, error) {
viper.SetConfigName("todo.config") // Configuration fileName without the .TOML or .YAML extension
viper.AddConfigPath(".") // Search the root directory for the configuration file
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
return Constants{}, err
}
viper.WatchConfig() // Watch for changes to the configuration file and recompile
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
})
viper.SetDefault("PORT", "8080")
if err = viper.ReadInConfig(); err != nil {
log.Panicf("Error reading config file, %s", err)
}
var constants Constants
err = viper.Unmarshal(&constants)
return constants, err
}