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
|
package main
import (
"encoding"
"encoding/base64"
"encoding/json"
"log"
"net/url"
"os"
"regexp"
)
type userToken struct {
Hash []byte `json:"hash"`
Salt []byte `json:"salt"`
}
type jsonURL struct{ v url.URL }
func (u *jsonURL) UnmarshalText(v []byte) error {
parsed, err := url.Parse(string(v))
if err != nil {
return err
}
u.v = *parsed
return nil
}
var _ encoding.TextUnmarshaler = new(jsonURL)
type config struct {
CalendarURL jsonURL `json:"calendar_url"`
Ignore ignoreRules `json:"ignore"`
Port string `json:"port"`
UserTokens map[string]userToken `json:"user_tokens"`
}
type ignoreRules struct {
LocationRegexes []regexp.Regexp `json:"location_regexes"`
SummaryRegexes []regexp.Regexp `json:"summary_regexes"`
}
func printConfig(cfg *config) {
b64 := base64.StdEncoding
log.Print("Loaded configuration: ")
log.Print(" Calendar URL: ", cfg.CalendarURL.v.String())
log.Print(" HTTP Port: ", cfg.Port)
log.Print(" Ignoring:")
for _, entry := range cfg.Ignore.LocationRegexes {
log.Printf(" Events with locations matching %s", entry.String())
}
for _, entry := range cfg.Ignore.SummaryRegexes {
log.Printf(" Events with summaries matching %s", entry.String())
}
if len(cfg.Ignore.LocationRegexes)+len(cfg.Ignore.SummaryRegexes) == 0 {
log.Printf(" <no ignore rules configured>")
}
log.Print(" User Tokens:")
for user, token := range cfg.UserTokens {
log.Print(" User ", user, ":")
log.Print(" Hash: ", b64.EncodeToString(token.Hash))
log.Print(" Salt: ", b64.EncodeToString(token.Salt))
}
if len(cfg.UserTokens) == 0 {
log.Print(" <no users configured>")
}
}
func loadConfigFrom(filename string) config {
bytes, err := os.ReadFile(filename)
if err != nil {
log.Fatal("Could not read config file (at ", filename, ")")
}
var cfg config
if err = json.Unmarshal(bytes, &cfg); err != nil {
log.Fatalln("Could not parse config file:", err)
}
return cfg
}
func loadConfig() config {
configFile := os.Getenv("CONFIG_FILE")
if configFile == "" {
log.Fatal("Environment variable CONFIG_FILE not specified")
}
log.Print("Loading configuration from ", configFile)
return loadConfigFrom(configFile)
}
|