-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
248 lines (210 loc) · 5.88 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/BurntSushi/toml"
"github.com/ushis/gesundheit/check"
"github.com/ushis/gesundheit/db"
"github.com/ushis/gesundheit/filter"
"github.com/ushis/gesundheit/handler"
"github.com/ushis/gesundheit/http"
"github.com/ushis/gesundheit/input"
"github.com/ushis/gesundheit/node"
)
type config struct {
Node node.Info
Log logConfig
Http httpConfig
Database databaseConfig
Modules modulesConfig
}
type logConfig struct {
Path string
Timestamps bool
}
type httpConfig struct {
Enabled bool
Listen string
}
type databaseConfig struct {
Module string
Config toml.Primitive
}
type modulesConfig struct {
Config string
}
type modConfig struct {
Check *checkConfig
Handler *handlerConfig
Input *inputConfig
}
type checkConfig struct {
Module string
Description string
Interval string
Config toml.Primitive
}
type handlerConfig struct {
Module string
Filter []*filterConfig
Config toml.Primitive
}
type filterConfig struct {
Module string
Config toml.Primitive
}
type inputConfig struct {
Module string
Config toml.Primitive
}
func loadConf(path string) (config, db.Database, *http.Server, error) {
conf := config{
Log: logConfig{Path: "-", Timestamps: false},
Http: httpConfig{Enabled: false, Listen: "127.0.0.1:8080"},
Database: databaseConfig{Module: "memory"},
Modules: modulesConfig{Config: "modules.d/*.toml"},
}
meta, err := toml.DecodeFile(path, &conf)
if err != nil {
return conf, nil, nil, err
}
dbFunc, err := db.Get(conf.Database.Module)
if err != nil {
return conf, nil, nil, fmt.Errorf("failed to load database config: %s: %s", path, err)
}
db, err := dbFunc(func(cfg interface{}) error {
return meta.PrimitiveDecode(conf.Database.Config, cfg)
})
if err != nil {
return conf, nil, nil, fmt.Errorf("failed to load database config: %s: %s", path, err)
}
var httpServer *http.Server
if conf.Http.Enabled {
httpServer = http.New(db, conf.Http.Listen)
}
if len(meta.Undecoded()) > 0 {
return conf, nil, nil, fmt.Errorf("failed to load config: %s: unknown field %s", path, meta.Undecoded()[0])
}
if conf.Node.Name == "" {
hostname, err := os.Hostname()
if err != nil {
return conf, nil, nil, fmt.Errorf("failed to determine hostname: %s", err)
}
conf.Node.Name = hostname
}
return conf, db, httpServer, nil
}
type modConfLoader struct {
node node.Info
hub *hub
db db.Database
}
func newModConfLoader(node node.Info, hub *hub, db db.Database) modConfLoader {
return modConfLoader{node, hub, db}
}
func (l modConfLoader) loadAll(glob string) error {
paths, err := filepath.Glob(glob)
if err != nil {
return err
}
for _, path := range paths {
if err := l.load(path); err != nil {
return err
}
}
return nil
}
func (l modConfLoader) load(path string) error {
mod := modConfig{}
meta, err := toml.DecodeFile(path, &mod)
if err != nil {
return err
}
if mod.Check != nil {
return l.loadCheck(mod.Check, path, meta)
}
if mod.Handler != nil {
return l.loadHandler(mod.Handler, path, meta)
}
if mod.Input != nil {
return l.loadInput(mod.Input, path, meta)
}
return fmt.Errorf("failed to load module config: %s: missing module configuration", path)
}
func (l modConfLoader) loadCheck(conf *checkConfig, path string, meta toml.MetaData) error {
fn, err := check.Get(conf.Module)
if err != nil {
return fmt.Errorf("failed to load check config: %s: %s", path, err.Error())
}
chk, err := fn(l.db, func(cfg interface{}) error {
return meta.PrimitiveDecode(conf.Config, cfg)
})
if err != nil {
return fmt.Errorf("failed to load check config: %s: %s", path, err.Error())
}
if len(meta.Undecoded()) > 0 {
return fmt.Errorf("failed to load check config: %s: unknown field %s", path, meta.Undecoded()[0])
}
if conf.Description == "" {
return fmt.Errorf("failed to load check config: %s: missing Description", path)
}
interval, err := time.ParseDuration(conf.Interval)
if err != nil {
return fmt.Errorf("failed to load check config: %s: %s", path, err.Error())
}
l.hub.registerProducer(check.NewRunner(l.node, filepath.Base(path), conf.Description, interval, chk))
return nil
}
func (l modConfLoader) loadHandler(conf *handlerConfig, path string, meta toml.MetaData) error {
fn, err := handler.Get(conf.Module)
if err != nil {
return fmt.Errorf("failed to load handler config: %s: %s", path, err.Error())
}
hdl, err := fn(func(cfg interface{}) error {
return meta.PrimitiveDecode(conf.Config, cfg)
})
if err != nil {
return fmt.Errorf("failed to load handler config: %s: %s", path, err.Error())
}
filters := []filter.Filter{}
for _, cfg := range conf.Filter {
f, err := l.loadFilter(cfg, path, meta)
if err != nil {
return fmt.Errorf("failed to load handler config: %s: %s", path, err.Error())
}
filters = append(filters, f)
}
l.hub.registerConsumer(filter.Handler(hdl, filters))
if len(meta.Undecoded()) > 0 {
return fmt.Errorf("failed to load handler config: %s: unknown field %s", path, meta.Undecoded()[0])
}
return nil
}
func (l modConfLoader) loadFilter(conf *filterConfig, path string, meta toml.MetaData) (filter.Filter, error) {
fn, err := filter.Get(conf.Module)
if err != nil {
return nil, err
}
return fn(l.db, func(cfg interface{}) error {
return meta.PrimitiveDecode(conf.Config, cfg)
})
}
func (l modConfLoader) loadInput(conf *inputConfig, path string, meta toml.MetaData) error {
fn, err := input.Get(conf.Module)
if err != nil {
return fmt.Errorf("failed to load input config: %s: %s", path, err.Error())
}
in, err := fn(func(cfg interface{}) error {
return meta.PrimitiveDecode(conf.Config, cfg)
})
if err != nil {
return fmt.Errorf("failed to load input config: %s: %s", path, err.Error())
}
if len(meta.Undecoded()) > 0 {
return fmt.Errorf("failed to load input config: %s: unknown field %s", path, meta.Undecoded()[0])
}
l.hub.registerProducer(in)
return nil
}