+20
server/index.js
+20
server/index.js
···
3
3
import { createRemoteJWKSet } from 'jose';
4
4
import fs from 'node:fs';
5
5
import { randomBytes } from 'node:crypto';
6
+
import https from 'node:https';
6
7
import webpush from 'web-push';
7
8
import { DB } from './db.js';
8
9
import { connectSpacedust } from './notifications.js';
···
26
27
return secrets;
27
28
}
28
29
30
+
function startHealthcheckPing(endpoint) {
31
+
const next = () => setTimeout(() => startHealthcheckPing(endpoint), 90 * 1000);
32
+
33
+
https
34
+
.get(endpoint, res => {
35
+
if (res.statusCode !== 200) console.warn('non-200 health check response', res.statusCode);
36
+
res
37
+
.on('data', () => {})
38
+
.on('end', next);
39
+
})
40
+
.on('error', err => {
41
+
console.warn('healthcheck request errored', err);
42
+
next();
43
+
});
44
+
}
45
+
29
46
const main = env => {
30
47
if (!env.ADMIN_DID) throw new Error('ADMIN_DID is required to run');
31
48
const adminDid = env.ADMIN_DID;
···
53
70
const port = parseInt(env.PORT ?? 8000, 10);
54
71
55
72
const allowedOrigin = env.ALLOWED_ORIGIN ?? 'http://127.0.0.1:5173';
73
+
74
+
if (env.HEALTHCHECK) startHealthcheckPing(env.HEALTHCHECK);
75
+
else console.warn('no HEALTHCHECK in env, not sending healthcheck pings');
56
76
57
77
server(secrets, jwks, allowedOrigin, whoamiHost, db, updateSubs, push, adminDid).listen(
58
78
port,