diff options
Diffstat (limited to 'config.go')
-rw-r--r-- | config.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/config.go b/config.go new file mode 100644 index 0000000..d9a2e30 --- /dev/null +++ b/config.go | |||
@@ -0,0 +1,74 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "encoding/base64" | ||
5 | "encoding/json" | ||
6 | "log" | ||
7 | "net/url" | ||
8 | "os" | ||
9 | "regexp" | ||
10 | ) | ||
11 | |||
12 | type userToken struct { | ||
13 | Hash []byte `json:"hash"` | ||
14 | Salt []byte `json:"salt"` | ||
15 | } | ||
16 | |||
17 | type config struct { | ||
18 | CalendarURL url.URL `json:"calendar_url"` | ||
19 | Ignore ignoreRules `json:"ignore"` | ||
20 | Port string `json:"port"` | ||
21 | UserTokens map[string]userToken `json:"user_tokens"` | ||
22 | } | ||
23 | |||
24 | type ignoreRules struct { | ||
25 | LocationRegexes []regexp.Regexp `json:"location_regexes"` | ||
26 | SummaryRegexes []regexp.Regexp `json:"summary_regexes"` | ||
27 | } | ||
28 | |||
29 | func printConfig(cfg *config) { | ||
30 | b64 := base64.StdEncoding | ||
31 | |||
32 | log.Print("Loaded configuration: ") | ||
33 | log.Print(" HTTP Port: ", cfg.Port) | ||
34 | log.Print(" User Tokens:") | ||
35 | for user, token := range cfg.UserTokens { | ||
36 | log.Print(" User ", user, ":") | ||
37 | log.Print(" Hash: ", b64.EncodeToString(token.Hash)) | ||
38 | log.Print(" Salt: ", b64.EncodeToString(token.Salt)) | ||
39 | } | ||
40 | if len(cfg.UserTokens) == 0 { | ||
41 | log.Print(" <no users configured>") | ||
42 | } | ||
43 | log.Print(" Ignoring:") | ||
44 | for _, entry := range cfg.Ignore.LocationRegexes { | ||
45 | log.Printf(" Events with locations matching %s", entry.String()) | ||
46 | } | ||
47 | for _, entry := range cfg.Ignore.SummaryRegexes { | ||
48 | log.Printf(" Events with summaries matching %s", entry.String()) | ||
49 | } | ||
50 | if len(cfg.Ignore.LocationRegexes)+len(cfg.Ignore.SummaryRegexes) == 0 { | ||
51 | log.Printf(" <no ignore rules configured>") | ||
52 | } | ||
53 | } | ||
54 | |||
55 | func loadConfigFrom(filename string) config { | ||
56 | bytes, err := os.ReadFile(filename) | ||
57 | if err != nil { | ||
58 | log.Fatal("Could not read config file (at ", filename, ")") | ||
59 | } | ||
60 | var cfg config | ||
61 | if err = json.Unmarshal(bytes, &cfg); err != nil { | ||
62 | log.Fatalln("Could not parse config file:", err) | ||
63 | } | ||
64 | return cfg | ||
65 | } | ||
66 | |||
67 | func loadConfig() config { | ||
68 | configFile := os.Getenv("CONFIG_FILE") | ||
69 | if configFile == "" { | ||
70 | log.Fatal("Environment variable CONFIG_FILE not specified") | ||
71 | } | ||
72 | log.Print("Loading configuration from ", configFile) | ||
73 | return loadConfigFrom(configFile) | ||
74 | } | ||