-
Notifications
You must be signed in to change notification settings - Fork 2
/
serve
executable file
·61 lines (49 loc) · 1.98 KB
/
serve
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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const routes = require('./server/routes');
const federate = require('./server/federate');
const {
constants,
findKnownKeys
} = require('./common');
const {
ttlKiller,
refreshPubBoards
} = require('./server/content');
const app = require('fastify')({ logger: true });
app.register(require('@fastify/cors'), constants.corsOptions);
const DefaultScheme = 'https';
const DefaultHost = 'localhost';
const DefaultPort = 1783;
async function main () {
const contentDir = path.resolve(process.env.SPRING83_CONTENT_DIR || path.join(__dirname, constants.defaultContentPath));
const contactAddr = process.env.SPRING83_CONTACT_ADDR;
let fqdn = process.env.SPRING83_FQDN || constants.defaultFQDN;
await fs.promises.mkdir(contentDir, { recursive: true });
const knownKeys = await findKnownKeys(contentDir);
await ttlKiller(knownKeys, contentDir, app);
await refreshPubBoards(contentDir);
federate.init(app, contentDir, knownKeys, fqdn);
['SIGINT', 'SIGTERM'].forEach((sig) => process.on(sig, () => {
app.log.info('Stopping...');
process.exit(0);
}));
const listenSpec = {
port: process.env.SPRING83_BIND_PORT || DefaultPort,
host: process.env.SPRING83_BIND_HOST || DefaultHost
};
let scheme = DefaultScheme;
if (fqdn === constants.defaultFQDN && listenSpec.host === DefaultHost && listenSpec.port === DefaultPort) {
fqdn = `${listenSpec.host}:${listenSpec.port}`;
scheme = 'http';
}
app.register(require('fastify-favicon'), { path: path.join(__dirname, 'client') });
app.addContentTypeParser('text/html', { parseAs: 'string' }, (req, body, done) => done(null, body));
await routes.attach(app, knownKeys, fqdn, contentDir, contactAddr, scheme);
await app.listen(listenSpec);
app.log.info(`Found ${Object.keys(knownKeys).length} pre-existing boards in ${contentDir}`);
app.log.info(`Listening on ${listenSpec.host}:${listenSpec.port} with FQDN ${fqdn}`);
}
main();