tweed
a Twitch RSS feed generator
git clone https://jhhm.no/repos/tweed.git
tweed/config.go
get raw file: config.go
1package main 2 3import ( 4 "errors" 5 "fmt" 6 "io/fs" 7 "os" 8 "path/filepath" 9 "strconv" 10 "strings" 11) 12 13type Config struct { 14 AppID string 15 AppSecret string 16 RefreshMinutes int 17 TTLMinutes int 18 Channels []string 19 CDNDomains []string 20 Port int 21 DataDir string 22 ServeFeeds bool 23} 24 25var defaultConfigDirs = []string{ 26 "/etc/tweed", 27 "/usr/local/etc/tweed", 28} 29 30var defaultDataDirs = []string{ 31 "/var/db/tweed", 32 "/var/lib/tweed", 33} 34 35func findConfigDir(given string) (string, error) { 36 if given != "" { 37 return given, nil 38 } 39 for _, d := range defaultConfigDirs { 40 if _, err := os.Stat(filepath.Join(d, "config.ini")); !errors.Is(err, os.ErrNotExist) { 41 return d, nil 42 } 43 } 44 return "", fmt.Errorf("no config.ini found (looked in %s)", 45 strings.Join(defaultConfigDirs, ", ")) 46} 47 48func findDataDir() (string, error) { 49 for _, d := range defaultDataDirs { 50 fi, err := os.Stat(d) 51 if errors.Is(err, fs.ErrNotExist) { 52 continue 53 } 54 if err != nil { 55 return "", fmt.Errorf("data dir %s: %w", d, err) 56 } 57 if !fi.IsDir() { 58 return "", fmt.Errorf("data dir %s: not a directory", d) 59 } 60 return d, nil 61 } 62 return "", fmt.Errorf("no data directory found (looked for %s)", 63 strings.Join(defaultDataDirs, ", ")) 64} 65 66func defaultCDNDomains() []string { 67 return []string{ 68 "vod-secure.twitch.tv", 69 "vod-metro.twitch.tv", 70 "vod-pop-secure.twitch.tv", 71 "d2vi6trrdongqn.cloudfront.net", 72 "d2e2de1etea730.cloudfront.net", 73 "dqrpb9wgowsf5.cloudfront.net", 74 "ds0h3roq6wcgc.cloudfront.net", 75 "d2nvs31859zcd8.cloudfront.net", 76 "d2aba1wr3818hz.cloudfront.net", 77 "d3c27h4odz752x.cloudfront.net", 78 "dgeft87wbj63p.cloudfront.net", 79 "d1m7jfoe9zdc1j.cloudfront.net", 80 "d1ymi26ma8va5x.cloudfront.net", 81 "d2vjef5jvl6bfs.cloudfront.net", 82 "d3vd9lfkzbru3h.cloudfront.net", 83 "d1mhjrowxxagfy.cloudfront.net", 84 "ddacn6pr5v0tl.cloudfront.net", 85 "d3aqoihi2n8ty8.cloudfront.net", 86 "d1xhnb4ptk05mw.cloudfront.net", 87 "d6tizftlrpuof.cloudfront.net", 88 "d36nr0u3xmc4mm.cloudfront.net", 89 "d1oca24q5dwo6d.cloudfront.net", 90 "d2um2qdswy1tb0.cloudfront.net", 91 "d1w2poirtb3as9.cloudfront.net", 92 "d6d4ismr40iw.cloudfront.net", 93 "d1g1f25tn8m2e6.cloudfront.net", 94 "dykkng5hnh52u.cloudfront.net", 95 "d2dylwb3shzel1.cloudfront.net", 96 "d2xmjdvx03ij56.cloudfront.net", 97 "d3fi1amfgojobc.cloudfront.net", 98 } 99} 100 101func loadCDNDomains(dir string) []string { 102 p := filepath.Join(dir, "cdns") 103 data, err := os.ReadFile(p) 104 if err != nil { 105 if !errors.Is(err, os.ErrNotExist) { 106 logWarn("read cdns file %s: %v", p, err) 107 } 108 return defaultCDNDomains() 109 } 110 111 var candidates []string 112 for l := range strings.SplitSeq(string(data), "\n") { 113 l = strings.TrimSpace(l) 114 if l == "" || strings.HasPrefix(l, "#") { 115 continue 116 } 117 candidates = append(candidates, l) 118 } 119 if len(candidates) == 0 { 120 return defaultCDNDomains() 121 } 122 return candidates 123} 124 125func parsePositiveInt(val string) (int, error) { 126 n, err := strconv.Atoi(val) 127 if err != nil || n < 1 { 128 return 0, fmt.Errorf("%q is not a positive integer", val) 129 } 130 return n, nil 131} 132 133func loadConfig(dir string) (Config, error) { 134 b, err := os.ReadFile(filepath.Join(dir, "config.ini")) 135 if err != nil { 136 return Config{}, fmt.Errorf("reading config: %w", err) 137 } 138 lines := strings.Split(string(b), "\n") 139 cfg := Config{ServeFeeds: true} 140 141 for lineNum, raw := range lines { 142 line := strings.TrimSpace(raw) 143 if line == "" || strings.HasPrefix(line, "#") { 144 continue 145 } 146 parts := strings.SplitN(line, "=", 2) 147 if len(parts) != 2 { 148 return Config{}, fmt.Errorf("line %d: illegal formatting, expected key = value", lineNum) 149 } 150 key := strings.TrimSpace(parts[0]) 151 val := strings.TrimSpace(parts[1]) 152 switch key { 153 case "appId": 154 cfg.AppID = val 155 case "appSecret": 156 cfg.AppSecret = val 157 case "refreshMinutes": 158 n, err := parsePositiveInt(val) 159 if err != nil { 160 return Config{}, fmt.Errorf("line %d: illegal refreshMinutes: %w", lineNum, err) 161 } 162 cfg.RefreshMinutes = n 163 case "ttlMinutes": 164 n, err := parsePositiveInt(val) 165 if err != nil { 166 return Config{}, fmt.Errorf("line %d: illegal ttlMinutes: %w", lineNum, err) 167 } 168 cfg.TTLMinutes = n 169 case "channels": 170 parts := strings.Split(val, ",") 171 for _, p := range parts { 172 p = strings.TrimSpace(p) 173 if p != "" { 174 cfg.Channels = append(cfg.Channels, p) 175 } 176 } 177 case "port": 178 n, err := parsePositiveInt(val) 179 if err != nil || n > 65535 { 180 return Config{}, fmt.Errorf("line %d: illegal port: %q must be 1-65535", lineNum, val) 181 } 182 cfg.Port = n 183 case "dataDir": 184 if val == "" { 185 return Config{}, fmt.Errorf("line %d: empty dataDir value", lineNum) 186 } 187 if !filepath.IsAbs(val) { 188 return Config{}, fmt.Errorf("line %d: illegal dataDir: path must be absolute", lineNum) 189 } 190 cfg.DataDir = val 191 case "serveFeeds": 192 b, err := strconv.ParseBool(val) 193 if err != nil { 194 return Config{}, fmt.Errorf("line %d: illegal serveFeeds: %q is not a boolean", lineNum, val) 195 } 196 cfg.ServeFeeds = b 197 default: 198 return Config{}, fmt.Errorf("line %d: illegal key %q", lineNum, key) 199 } 200 } 201 202 if cfg.AppID == "" || cfg.AppSecret == "" { 203 return cfg, errors.New("missing required appId and/or appSecret in config") 204 } 205 if cfg.RefreshMinutes == 0 { 206 cfg.RefreshMinutes = 5 207 } 208 if cfg.TTLMinutes == 0 { 209 cfg.TTLMinutes = 30 210 } 211 if cfg.Port == 0 { 212 cfg.Port = 8081 213 } 214 if cfg.DataDir == "" { 215 cfg.DataDir, err = findDataDir() 216 if err != nil { 217 return Config{}, err 218 } 219 } 220 if len(cfg.Channels) == 0 { 221 return Config{}, errors.New("found no channels, no point in running") 222 } 223 224 cfg.CDNDomains = loadCDNDomains(dir) 225 226 return cfg, nil 227}