-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
57 lines (46 loc) · 1.11 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
package config
import (
"github.com/achushu/libs/out"
"github.com/achushu/tpz/data"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
var (
Settings *Config
)
// Config contains all the user configurable settings for the Server
type Config struct {
Port int
HomeDir string
StaticDir string
Database *data.Config
Competition Competition
}
type Competition struct {
Name string
}
func LoadConfigFile(filename string) (*Config, error) {
viper.SetConfigFile(filename)
err := viper.ReadInConfig()
if err != nil {
return nil, err
}
v := viper.GetViper()
viper.SetDefault("server.port", 8000)
cfg := &Config{
Port: viper.GetInt("server.port"),
HomeDir: viper.GetString("server.home"),
StaticDir: viper.GetString("server.static"),
Database: data.MustGetConfigFromMap(v.Sub("database").AllSettings()),
Competition: Competition{
Name: viper.GetString("competition.name"),
},
}
// Keep an eye on the configuration file
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
out.Println("Config file changed:", e.Name)
})
Settings = cfg
return cfg, nil
}