/
conf.go
63 lines (59 loc) · 1.38 KB
/
conf.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
package tfe
import (
"github.com/xianxu/gostrich"
"log"
"strings"
)
var (
confs map[string]func() map[string]Rules
)
/*
* Those functions are not thread safe, only meant to be called in init() function.
*/
func GetRules(name string, portOffset int) func() map[string]Rules {
names := strings.Split(name, ",")
for i, v := range names {
names[i] = strings.TrimSpace(v)
}
if confs == nil {
confs = make(map[string]func() map[string]Rules)
return nil
}
return func() map[string]Rules {
result := make(map[string]Rules)
for _, n := range names {
if fn, ok := confs[n]; ok {
rules := fn()
for port, r := range rules {
newPort := gostrich.UpdatePort(port, portOffset)
if rs, ok := result[newPort]; ok {
//TODO: duplication detection
result[newPort] = append([]Rule(rs), []Rule(r)...)
} else {
result[newPort] = r
}
}
} else {
log.Printf("Unknown rule named %v", n)
}
}
for k, v := range result {
n := make([]string, len(v))
for i, r := range v {
n[i] = "\"" + r.GetName() + "\""
}
log.Printf("Serving %v rules: %v on port %v", len(v), strings.Join(n, ", "), k)
}
return result
}
}
func AddRules(name string, rules func() map[string]Rules) bool {
if confs == nil {
confs = make(map[string]func() map[string]Rules)
}
if _, ok := confs[name]; ok {
return false
}
confs[name] = rules
return true
}