/
config.go
52 lines (43 loc) · 882 Bytes
/
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
package config
import (
"errors"
"sync"
"github.com/BurntSushi/toml"
)
const (
defaultServerPort = 80
defaultShutdownTimeoutInSecond = 60
)
type config struct {
Server ServerConfig `toml:"server"`
}
type ServerConfig struct {
Port int `toml:"port"`
ShutdownTimeoutInSecond int `toml:"shutdown_timeout_in_second"`
}
var conf *config
var once sync.Once
func newDefaultConfig() *config {
return &config{
Server: ServerConfig{
Port: defaultServerPort,
ShutdownTimeoutInSecond: defaultShutdownTimeoutInSecond,
},
}
}
func InitializeConfig(path string) {
once.Do(func() {
conf = newDefaultConfig()
if path != "" {
if _, err := toml.DecodeFile(path, &conf); err != nil {
panic(err)
}
}
})
}
func GetConfig() *config {
if conf == nil {
panic(errors.New("config is nil"))
}
return conf
}