-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathconfig.go
More file actions
107 lines (80 loc) · 2.53 KB
/
Copy pathconfig.go
File metadata and controls
107 lines (80 loc) · 2.53 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package vertex
import (
"github.com/EverythingMe/gofigure"
"github.com/EverythingMe/gofigure/autoflag"
"github.com/dvirsky/go-pylog/logging"
"gopkg.in/yaml.v2"
)
type serverConfig struct {
// Listening address for the server, e.g. ":8080"
ListenAddr string `yaml:"listen"`
// Should we allow non http access to the API? use only on dev machines
AllowInsecure bool `yaml:"allow_insecure"`
// The location of the console UI html files on the local machine
ConsoleFilesPath string `yaml:"console_files_path"`
// Minimal logging level [DEBUG | INFO | WARN | ERROR | CRITICAL]
LoggingLevel string `yaml:"logging_level"`
// Disconnect idle clients after T seconds
ClientTimeout int `yaml:"client_timeout_sec"`
}
// General-purpose to just protect some urls
type authConfig struct {
User string `yaml:"user"`
Password string `yaml:"password"`
}
type confType struct {
Server serverConfig `yaml:"server"`
Auth authConfig `yaml:"auth"`
APIConfigs map[string]interface{} `yaml:"apis"`
}
var Config = struct {
Server serverConfig `yaml:"server"`
Auth authConfig `yaml:"auth"`
APIConfigs map[string]interface{} `yaml:"apis,flow"`
apiconfs map[string]interface{}
}{
Server: serverConfig{
ListenAddr: ":9944",
AllowInsecure: false,
ConsoleFilesPath: "../console",
LoggingLevel: "INFO",
ClientTimeout: 60,
},
Auth: authConfig{
User: "vertext",
Password: "xetrev",
},
APIConfigs: make(map[string]interface{}),
apiconfs: make(map[string]interface{}),
}
// registerAPIConfig registers the configurations for a specific api, under the path of /apis/<api_name>. e.g
// apis:
// myApi:
// foo: bar
func registerAPIConfig(name string, conf interface{}) {
Config.apiconfs[name] = conf
}
func ReadConfigs() error {
if err := autoflag.Load(gofigure.DefaultLoader, &Config); err != nil {
logging.Error("Error loading configs: %v", err)
return err
}
logging.Info("Read configs: %#v", &Config)
for k, m := range Config.APIConfigs {
if conf, found := Config.apiconfs[k]; found && conf != nil {
b, err := yaml.Marshal(m)
if err == nil {
if err := yaml.Unmarshal(b, conf); err != nil {
logging.Error("Error reading config for API %s: %s", k, err)
} else {
logging.Debug("Unmarshaled API config for %s: %#v", k, conf)
}
} else {
logging.Error("Error marshalling config for API %s: %s", k, err)
}
} else {
logging.Warning("API Section %s in config file not registered with server", k)
}
}
return nil
}