-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
99 lines (86 loc) · 2.1 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
package main
import (
"encoding/json"
"io/ioutil"
"sync"
)
type Config struct {
sync.RWMutex
Routes map[string]Route
Filters map[string]Filter
Options map[string]string
}
// Add the DROP route. Make it the default if there is no existing default route.
func AddDropRoute() {
dropIsDefault := true
for _, route := range config.Routes {
if route.IsDefault == true {
dropIsDefault = false
}
}
config.Routes["DROP"] = Route{Id: "DROP", Name: "Drop", IsDefault: dropIsDefault}
}
func SetDefaultOptions() {
if _, exists := config.Options["PIDFile"]; !exists {
config.Options["PIDFile"] = ""
}
}
// Load the filter and route configuration from a JSON file.
// Add the drop route as it must always be present.
func LoadConfig() error {
defer func() {
if config.Routes == nil {
config.Routes = map[string]Route{}
}
if config.Filters == nil {
config.Filters = map[string]Filter{}
}
if config.Options == nil {
config.Options = map[string]string{}
}
AddDropRoute()
SetDefaultOptions()
}()
data, err := ioutil.ReadFile(*confFile)
if err != nil {
return err
}
err = json.Unmarshal(data, &config)
if err != nil {
return err
}
return nil
}
// Create a clone of the global config to use in SaveConfig().
func CloneConfig() *Config {
clone := new(Config)
clone.Routes = map[string]Route{}
clone.Filters = map[string]Filter{}
clone.Options = map[string]string{}
for k, v := range config.Routes {
clone.Routes[k] = v
}
for k, v := range config.Filters {
clone.Filters[k] = v
}
for k, v := range config.Options {
clone.Options[k] = v
}
return clone
}
// Save the filter and route configuration to a JSON file.
// Remove the DROP route before marshalling - it is a hardcoded route that should never be in the config file.
// Don't delete DROP from the actual config variable (it might be needed mid-save), make a copy instead.
func SaveConfig() error {
clone := CloneConfig()
delete(clone.Routes, "DROP")
data, err := json.MarshalIndent(clone, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(*confFile, data, 0644)
if err != nil {
return err
}
return nil
}