forked from VolantMQ/volantmq
/
config.go
58 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
58
package configuration
import (
"flag"
"io/ioutil"
"os"
"github.com/VolantMQ/vlapi/plugin"
"gopkg.in/yaml.v2"
)
// PluginState status
type PluginState struct {
Plugin vlplugin.Plugin
Errors []error
}
// DefaultConfig Load minimum working configuration to allow
// server start without user provided one
func DefaultConfig() *Config {
c := Config{}
if err := yaml.Unmarshal(defaultConfig, &c); err != nil {
panic(err.Error())
}
return &c
}
// ReadConfig read service configuration
func ReadConfig() *Config {
log := GetHumanLogger()
log.Info("loading config")
flag.Parse()
c := DefaultConfig()
if len(configFile) == 0 {
log.Info("No config file provided\nuse --config option or VOLANTMQ_CONFIG environment variable to provide own")
log.Info("default config: \n", string(defaultConfig))
} else {
if _, err := os.Stat(configFile); os.IsNotExist(err) {
log.Error("config not found", "file", configFile)
return nil
}
data, err := ioutil.ReadFile(configFile)
if err != nil {
panic(err.Error())
}
if err = yaml.Unmarshal(data, c); err != nil {
panic(err.Error())
}
}
return c
}