From 09af83576a877eda40512bfba30b47a1b13061fb Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Wed, 13 Nov 2024 20:47:40 +0100 Subject: Initial (public) commit --- config.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 config.go (limited to 'config.go') diff --git a/config.go b/config.go new file mode 100644 index 0000000..d9a2e30 --- /dev/null +++ b/config.go @@ -0,0 +1,74 @@ +package main + +import ( + "encoding/base64" + "encoding/json" + "log" + "net/url" + "os" + "regexp" +) + +type userToken struct { + Hash []byte `json:"hash"` + Salt []byte `json:"salt"` +} + +type config struct { + CalendarURL url.URL `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(" HTTP Port: ", cfg.Port) + 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(" ") + } + 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(" ") + } +} + +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) +} -- cgit v1.2.3