-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
88 lines (74 loc) · 2.14 KB
/
config.go
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
package main
import (
"fmt"
"os"
"sync"
"time"
"go.mau.fi/zeroconfig"
"gopkg.in/yaml.v3"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
"maunium.net/go/mautrix/util"
)
type Config struct {
HomeserverURL string `yaml:"homeserver_url"`
MediaURL string `yaml:"media_url"`
UserID id.UserID `yaml:"user_id"`
Password string `yaml:"password"`
LogConfig zeroconfig.Config `yaml:"logging"`
ListenAddress string `yaml:"listen_address"`
PublicURL string `yaml:"public_url"`
CloudflareZoneID string `yaml:"cloudflare_zone_id"`
CloudflareToken string `yaml:"cloudflare_token"`
Feeds map[string]*FeedConfig `yaml:"feeds"`
feedsByRoomID map[id.RoomID]*FeedConfig
homeserverDomain string
}
type FeedConfig struct {
RoomAlias id.RoomAlias `yaml:"room_alias"`
RoomID id.RoomID `yaml:"room_id"`
MaxEntries int `yaml:"max_entries"`
Homepage string `yaml:"homepage"`
Language string `yaml:"language"`
id string
title string
description string
icon string
iconMXC id.ContentURI
authors map[id.UserID]JSONFeedAuthor
powers *event.PowerLevelsEventContent
entries *util.RingBuffer[id.EventID, *event.Event]
lastUpdate time.Time
updateLock sync.RWMutex
rss []byte
rssHash string
atom []byte
atomHash string
json []byte
jsonHash string
}
func loadConfig() (*Config, error) {
cfgPath := os.Getenv("FEEDSERV_CONFIG_PATH")
if cfgPath == "" {
cfgPath = "config.yaml"
}
file, err := os.Open(cfgPath)
if err != nil {
return nil, fmt.Errorf("failed to read config: %w", err)
}
var config Config
err = yaml.NewDecoder(file).Decode(&config)
if err != nil {
return nil, fmt.Errorf("failed to parse config: %w", err)
}
if passwordEnv := os.Getenv("FEEDSERV_PASSWORD"); passwordEnv != "" {
config.Password = passwordEnv
} else if passwordFileEnv := os.Getenv("FEEDSERV_PASSWORD_FILE"); passwordFileEnv != "" {
pwBytes, err := os.ReadFile(passwordFileEnv)
if err != nil {
return nil, fmt.Errorf("failed to read password from file: %w", err)
}
config.Password = string(pwBytes)
}
return &config, nil
}