-
Notifications
You must be signed in to change notification settings - Fork 4
/
conf.js
328 lines (280 loc) · 7.87 KB
/
conf.js
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import { deepMerge, envLoadSync, flagsParse, jsoncParse } from './deps.ts'
const args = flagsParse(Deno.args, {
string: [
'config',
'port',
'hostname',
'db',
'db-stats',
'db-limits',
'dotenv',
'plugs-dir',
'plugs-builtin-use',
'__compiled-version',
],
boolean: ['init', 'help', 'version'],
alias: {
c: 'config',
p: 'port',
b: 'hostname',
d: 'db',
s: 'db-stats',
l: 'db-limits',
e: 'dotenv',
i: 'init',
v: 'version',
h: 'help',
},
unknown: (arg) => {
console.log(`error: unexpected argument '${arg}'`)
console.log()
console.log(`For more information, try '--help'.`)
Deno.exit(1)
},
})
const VERSION = args['__compiled-version'] ?? 'X.X.X'
const CONFIG = `// default configuration file for booger
// booger will look for this file in the directory it is run from
// or you can specify a path with the --config flag
// you can always generate a default config file with the --init flag
{
// the port to listen on (precedence cli > env(PORT) > config file)
"port": 8006,
// the ip or hostname to listen on (precedence cli > env(HOSTNAME) > config file)
"hostname": "127.0.0.1",
// postgres url for nostr data (precedence cli > env(DB) > config file)
// if this db does not exist, booger will try to create it for you
"db": "postgres://127.0.0.1:5432/booger",
// exactly how booger will respond to nip-11 requests
"nip11": {
"name": "booger",
"description": "a booger relay",
"pubkey": "",
"contact": "",
"supported_nips": [
1,
2,
4,
9,
11,
12,
15,
16,
20,
26,
28,
33,
40
],
"software": "https://github.com/stackernews/booger",
"version": "${VERSION}"
},
// configuration related to booger plugs
"plugs": {
// directory to load plugs from
"dir" : "./plugs",
// the builtin plugs that booger will use
"builtin" : {
// the default builtin plugs that booger will use
// omit any or all (empty array) if you don't want to use them
"use" : [
"validate",
"stats",
"limits"],
// configuration for the validate plug
"validate": {
// min prefix length for ids and authors
"minPrefixLength": 4,
// min subscription id length
"minSubscriptionIdLength": 1,
// max subscription id length
"maxSubscriptionIdLength": 255,
// min createdAt allowed
"minCreatedAt": 0,
// max createdAt allowed
"maxCreatedAt": 2147483647,
// max tag id length, i.e. [LEN(tagId), tadData, ...]
"maxTagIdLength": 255,
// max tag data length, i.e. [tagId, LEN(tadData), ...]
"maxTagDataLength": 1024,
// max number of tags allowed, i.e. LEN([[tagId, tadData, ...], ...])
"maxTagCount": 2500,
// max content field size
"maxContentSize": 102400,
// max ids in filter
"maxIds": 1000,
// max authors in filter
"maxAuthors": 1000,
// max kinds in filter
"maxKinds": 100,
// min limit in filter
"minLimit": 0,
// max limit in filter
"maxLimit": 5000
},
// configuration for the stats plug
"stats": {
// postgres url for stats booger plug (precedence cli > env(DB_STATS) > config file)
// if this db does not exist, booger will try to create it for you
"db" : "postgres://127.0.0.1:5432/booger_stats"
},
// configuration for the limits plug
"limits": {
// postgres url for limits booger plug (precedence cli > env(DB_LIMITS) > config file)
// if this db does not exist, booger will try to create it for you
"db" : "postgres://127.0.0.1:5432/booger_limits",
// max events per interval with option to prevent duplicates
// in the same interval
"maxEvents": {
// interval length in seconds
"interval": 60,
// max events per interval
"count": 100,
// min allowable duplicate length in interval (null to disable duplicate checks)
"duplicateContentIgnoreLen": null
},
// max simultaneous subscriptions per ip
"maxSubscriptions": 100,
// max simultaneous filters per ip
"maxFilters": 1000,
// max simultaneous connections per ip
"maxConnections": 20
}
}
}
}`
const config = jsoncParse(CONFIG)
if (args.help) {
const HELP = `booger - a nostr relay ${VERSION}
Docs: https://github.com/stackernews/booger/blob/main/README.md
Bugs: https://github.com/stackernews/booger/issues
Usage:
booger [options]
Options:
-i, --init
write default config to ./booger.jsonc
-c, --config <path>
path to booger config file (default: ./booger.jsonc)
-b, --hostname <ip or hostname>
interface to listen on (default: ${config.hostname})
0.0.0.0 for all interfaces
-p, --port <port>
port to listen on (default: ${config.port})
-d, --db <postgres url>
postgres url for nostr data (default: ${config.db})
-s, --db-stats <postgres url>
postgres url for stats booger data (default: ${config.plugs.builtin.stats.db})
-l, --db-limits <postgres url>
postgres url for limits booger data (default: ${config.plugs.builtin.limits.db})
-e, --dotenv <path>
path to .env file (default: none)
--plugs-dir <path>
path to plugs directory (default: ${config.plugs.dir})
--plugs-builtin-use <plugs>
comma seperated list of builtin plugs to use (default: ${
config.plugs.builtin.use.join(',')
})
-h, --help
print help
-v, --version
print version
`
console.log(HELP)
Deno.exit(0)
}
if (args.version) {
console.log(`booger ${VERSION}`)
console.log(
`deno ${Deno.version.deno} (${Deno.build.arch}-${Deno.build.vendor}-${Deno.build.os})`,
)
console.log(`v8 ${Deno.version.v8}`)
Deno.exit(0)
}
if (args.init) {
Deno.writeFileSync('./booger.jsonc', new TextEncoder().encode(CONFIG), {
createNew: true,
})
console.log(`booger's default config file was written to ./booger.jsonc`)
Deno.exit(0)
}
// get any user defined config files
let fileConfig = {}
if (args.config) {
try {
fileConfig = jsoncParse(
new TextDecoder('utf-8').decode(Deno.readFileSync(args.config)),
)
} catch (e) {
if (e instanceof Deno.errors.NotFound) {
throw new Error(`config file not found: ${args.config}`)
}
throw e
}
} else {
try {
fileConfig = jsoncParse(
new TextDecoder('utf-8').decode(Deno.readFileSync('./booger.jsonc')),
)
} catch (e) {
if (!(e instanceof Deno.errors.NotFound)) {
throw e
}
}
}
// remove undefined values from configs before merging
// this is a slow hack but its simple
const delUndefined = (obj) => JSON.parse(JSON.stringify(obj))
const cliConfig = delUndefined({
port: args.port,
hostname: args.hostname,
db: args['db'],
plugs: {
dir: args['plugs'],
builtin: {
use: args['plugs-builtin-use']?.split(','),
stats: {
db: args['db-stats'],
},
limits: {
db: args['db-limits'],
},
},
},
})
if (args.dotenv) {
envLoadSync({
export: true,
envPath: args.dotenv,
restrictEnvAccessTo: [
'PORT',
'HOSTNAME',
'DB',
'DB_STATS',
'DB_LIMITS',
],
})
}
const envConfig = delUndefined({
version: VERSION,
port: Deno.env.get('PORT'),
hostname: Deno.env.get('HOSTNAME'),
db: Deno.env.get('DB'),
plugs: {
builtin: {
stats: {
db: Deno.env.get('DB_STATS'),
},
limits: {
db: Deno.env.get('DB_LIMITS'),
},
},
},
})
export default [fileConfig, envConfig, cliConfig].reduce(
(acc, cur) =>
deepMerge(acc, cur, {
arrays: 'replace',
maps: 'merge',
}),
config,
)