This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
forked from barkyq/gnost-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
196 lines (182 loc) · 4.2 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"time"
"github.com/fsnotify/fsnotify"
"github.com/valyala/fastjson"
"golang.org/x/time/rate"
)
type Settings struct {
relay_url string
nip11_info_document []byte
max_limit int
subid_max_length int
websocket_rate_limit rate.Limit
websocket_burst int
delete_expired_events_period time.Duration
}
type Config struct {
settings_chan chan *Settings
nip11_chan chan *NIP11_document
done_chan chan struct{}
}
func (c *Config) Settings() *Settings {
return <-c.settings_chan
}
func (c *Config) NIP11() *NIP11_document {
return <-c.nip11_chan
}
// Done() must be called after calling Settings() or NIP11()
func (c *Config) Done() {
c.done_chan <- struct{}{}
}
// Config Hot Reloading
func InitConfig(filename string) (c *Config, e error) {
s := new(Settings)
c = &Config{
settings_chan: make(chan *Settings),
nip11_chan: make(chan *NIP11_document),
done_chan: make(chan struct{}),
}
if e = s.parseSettings(filename); e != nil {
return
}
doc := new(NIP11_document)
if e = doc.Parse(s.nip11_info_document); e != nil {
return
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
e = err
return
}
// Add a path.
if e = watcher.Add("."); e != nil {
return
}
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
panic(ok)
}
if event.Has(fsnotify.Write) && event.Name == "./"+filename {
if e = s.parseSettings(filename); e != nil {
panic(e)
}
if e = doc.Parse(s.nip11_info_document); e != nil {
panic(e)
}
}
case _, ok := <-watcher.Errors:
if !ok {
panic(ok)
}
case c.settings_chan <- s:
<-c.done_chan
case c.nip11_chan <- doc:
<-c.done_chan
}
}
}()
return c, e
}
func (s *Settings) parseSettings(filename string) (e error) {
if f, e := os.Open(filename); e != nil {
return e
} else {
defer f.Close()
if b, e := io.ReadAll(f); e != nil {
return e
} else {
if e := s.UnmarshalJSON(b); e != nil {
return e
}
}
}
// require sane defaults
if s.max_limit == 0 {
s.max_limit = 25
}
if s.subid_max_length == 0 {
s.subid_max_length = 32
}
if s.websocket_rate_limit == 0 {
s.websocket_rate_limit = 0.5
}
if s.websocket_burst == 0 {
s.websocket_burst = 4
}
if s.delete_expired_events_period == 0 {
s.delete_expired_events_period = time.Hour
}
return nil
}
func (s *Settings) UnmarshalJSON(payload []byte) error {
var fastjsonParser fastjson.Parser
parsed, err := fastjsonParser.ParseBytes(payload)
if err != nil {
return fmt.Errorf("failed to parse filter: %w", err)
}
obj, err := parsed.Object()
if err != nil {
return fmt.Errorf("filter is not an object")
}
var visiterr error
obj.Visit(func(k []byte, v *fastjson.Value) {
if visiterr != nil {
return
}
key := string(k)
switch key {
case "relay_url":
sb, err := v.StringBytes()
if err != nil {
visiterr = fmt.Errorf("invalid 'relay_url' field: %w", err)
return
}
s.relay_url = string(sb)
case "nip11_info_document":
s.nip11_info_document = make(json.RawMessage, 0)
s.nip11_info_document = v.MarshalTo(s.nip11_info_document)
case "max_limit":
if i, e := v.Int(); e != nil {
visiterr = fmt.Errorf("invalid 'max_limit' field: %w", err)
} else {
s.max_limit = i
}
case "subid_max_length":
if i, e := v.Int(); e != nil {
visiterr = fmt.Errorf("invalid 'subid_max_length' field: %w", err)
} else {
s.subid_max_length = i
}
case "websocket_rate_limit":
if f, e := v.Float64(); e != nil {
visiterr = fmt.Errorf("invalid 'websocket_rate_limit' field: %w", err)
} else {
s.websocket_rate_limit = rate.Limit(f)
}
case "websocket_burst":
if i, e := v.Int(); e != nil {
visiterr = fmt.Errorf("invalid 'websocket_burst' field: %w", err)
} else {
s.websocket_burst = i
}
case "delete_expired_events_period":
if i, e := v.Int64(); e != nil {
visiterr = fmt.Errorf("invalid 'delete_expired_events_period' field: %w", err)
} else {
s.delete_expired_events_period = time.Duration(i) * time.Second
}
}
})
if visiterr != nil {
return visiterr
}
return nil
}