-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (50 loc) · 1.54 KB
/
server.js
File metadata and controls
59 lines (50 loc) · 1.54 KB
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
'use strict';
require('./src/load-env');
const { config } = require('./src/config');
const { createApp } = require('./src/app');
const { createMonitor } = require('./src/monitor');
const { createStore } = require('./src/store');
const { shutdownTls } = require('./src/hcaptcha');
const startedAt = new Date().toISOString();
let timer;
let store;
async function shutdown(exitCode) {
if (timer) {
clearInterval(timer);
}
if (store) {
await store.save().catch(() => {});
}
await shutdownTls();
process.exit(exitCode);
}
async function boot() {
store = await createStore(config.stateFile);
const monitor = createMonitor(config, store);
const app = createApp(config, store, monitor, startedAt);
app.listen(config.port, '0.0.0.0', async () => {
process.stdout.write(`hcaptcha helper listening on ${config.port}\n`);
process.stdout.write(`state file: ${config.stateFile}\n`);
process.stdout.write(`targets loaded: ${config.targets.length}\n`);
await monitor.runChecks().catch(async (error) => {
store.db.data.lastError = error.message;
await store.save();
});
timer = setInterval(() => {
monitor.runChecks().catch(async (error) => {
store.db.data.lastError = error.message;
await store.save();
});
}, config.pollIntervalMs);
});
}
process.on('SIGINT', () => {
shutdown(0).catch(() => process.exit(1));
});
process.on('SIGTERM', () => {
shutdown(0).catch(() => process.exit(1));
});
boot().catch((error) => {
process.stderr.write(`${error.message}\n`);
process.exit(1);
});