/
config.go
173 lines (149 loc) · 4.3 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
// Copyright (c) 2014-2015 Ludovic Fauvet
// Licensed under the MIT license
package main
import (
"fmt"
"io/ioutil"
"launchpad.net/goyaml"
"os"
"strings"
"sync"
)
var (
defaultConfig = configuration{
Repository: "",
Templates: "",
OutputMode: "auto",
ListenAddress: ":8080",
Gzip: false,
RedisAddress: "127.0.0.1:6379",
RedisPassword: "",
LogDir: "",
GeoipDatabasePath: "/usr/share/GeoIP/",
ConcurrentSync: 2,
ScanInterval: 30,
CheckInterval: 1,
Hashes: hashing{
SHA1: true,
SHA256: false,
MD5: false,
},
DisallowRedirects: false,
WeightDistributionRange: 1.5,
DisableOnMissingFile: false,
}
config *configuration
configMutex sync.RWMutex
)
type configuration struct {
Repository string `yaml:"Repository"`
Templates string `yaml:"Templates"`
OutputMode string `yaml:"OutputMode"`
ListenAddress string `yaml:"ListenAddress"`
Gzip bool `yaml:"Gzip"`
RedisAddress string `yaml:"RedisAddress"`
RedisPassword string `yaml:"RedisPassword"`
LogDir string `yaml:"LogDir"`
GeoipDatabasePath string `yaml:"GeoipDatabasePath"`
ConcurrentSync int `yaml:"ConcurrentSync"`
ScanInterval int `yaml:"ScanInterval"`
CheckInterval int `yaml:"CheckInterval"`
Hashes hashing `yaml:"Hashes"`
DisallowRedirects bool `yaml:"DisallowRedirects"`
WeightDistributionRange float32 `yaml:"WeightDistributionRange"`
DisableOnMissingFile bool `yaml:"DisableOnMissingFile"`
Fallbacks []fallback `yaml:"Fallbacks"`
RedisSentinelMasterName string `yaml:"RedisSentinelMasterName"`
RedisSentinels []sentinels `yaml:"RedisSentinels"`
}
type fallback struct {
Url string `yaml:"URL"`
CountryCode string `yaml:"CountryCode"`
ContinentCode string `yaml:"ContinentCode"`
}
type sentinels struct {
Host string `yaml:"Host"`
}
type hashing struct {
SHA1 bool `yaml:"SHA1"`
SHA256 bool `yaml:"SHA256"`
MD5 bool `yaml:"MD5"`
}
// LoadConfig loads the configuration file if it has not yet been loaded
func LoadConfig() {
if config != nil {
return
}
err := ReloadConfig()
if err != nil {
log.Fatal(err)
}
}
// ReloadConfig reloads the configuration file and update it globally
func ReloadConfig() error {
if configFile == "" {
if fileExists("./mirrorbits.conf") {
configFile = "./mirrorbits.conf"
} else if fileExists("/etc/mirrorbits.conf") {
configFile = "/etc/mirrorbits.conf"
}
}
content, err := ioutil.ReadFile(configFile)
if err != nil {
fmt.Println("Configuration could not be found.\n\tUse -config <path>")
os.Exit(-1)
}
if os.Getenv("DEBUG") != "" {
fmt.Println("Reading configuration from", configFile)
}
c := defaultConfig
// Overload the default configuration with the user's one
err = goyaml.Unmarshal(content, &c)
if err != nil {
return fmt.Errorf("%s in %s", err, configFile)
}
// Sanitize
if c.WeightDistributionRange <= 0 {
return fmt.Errorf("WeightDistributionRange must be > 0")
}
if !isInSlice(c.OutputMode, []string{"auto", "json", "redirect"}) {
return fmt.Errorf("Config: outputMode can only be set to 'auto', 'json' or 'redirect'")
}
c.Repository = strings.TrimRight(c.Repository, "/")
if config != nil &&
(c.RedisAddress != config.RedisAddress ||
c.RedisPassword != config.RedisPassword ||
!testSentinelsEq(c.RedisSentinels, config.RedisSentinels)) {
// TODO reload redis connections
// Currently established connections will be updated only in case of disconnection
}
configMutex.Lock()
config = &c
configMutex.Unlock()
return nil
}
// GetConfig returns a pointer to a configuration object
// FIXME reading from the pointer could cause a race!
func GetConfig() *configuration {
configMutex.RLock()
defer configMutex.RUnlock()
if config == nil {
panic("Configuration not loaded")
}
return config
}
func fileExists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func testSentinelsEq(a, b []sentinels) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i].Host != b[i].Host {
return false
}
}
return true
}