-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
inlets.go
62 lines (51 loc) · 1.3 KB
/
inlets.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
package inlets
import (
"net/http"
"path/filepath"
)
// TODO: watch inlets.d folder and reload automatically (#61)
import (
"gopkg.in/yaml.v2"
"log"
"os"
"path"
)
type Inlet interface {
Name() string
SupportedMethods() []string
Handler(http.Handler) http.Handler
}
func LoadInlets(dir string) []Inlet {
configPaths, err := filepath.Glob(path.Join(dir, "*.yaml"))
if err != nil {
log.Printf("Warning: Failed to load inlets from config: %v\n", err)
return []Inlet{}
}
names := make(map[string]bool)
inlets := make([]Inlet, 0, len(configPaths))
for _, c := range configPaths {
f, err := os.Open(c)
if err != nil {
log.Printf("Warning: Failed to load inlet '%s' from config: %v\n", c, err)
continue
}
defer f.Close()
var inletConfig InletConfig
if err := yaml.NewDecoder(f).Decode(&inletConfig); err != nil {
log.Printf("Warning: Faield to parse inlet config from '%s': %v\n", c, err)
continue
}
if _, ok := names[inletConfig.Name]; ok {
log.Printf("Warning: Ignoring inlet definition from '%s', because name '%s' was found twice\n", c, inletConfig.Name)
continue
}
names[inletConfig.Name] = true
inlet, err := NewConfigInlet(&inletConfig)
if err != nil {
log.Printf("Warning: %v\n", err)
continue
}
inlets = append(inlets, inlet)
}
return inlets
}