aboutsummaryrefslogtreecommitdiffstats
path: root/config.go
blob: d9a2e30a481b55a5fe26a6641ac4dc1364ba82ee (plain) (blame)
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
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("    <no users configured>")
	}
	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>")
	}
}

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)
}