-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.mjs
57 lines (51 loc) · 1.44 KB
/
server.mjs
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
// SPDX-FileCopyrightText: © 2024 Sebastian Davids <[email protected]>
// SPDX-License-Identifier: Apache-2.0
import { readFileSync } from 'node:fs';
import process from 'node:process';
['uncaughtException', 'unhandledRejection'].forEach((s) =>
process.once(s, (e) => {
console.error(e);
process.exit(70); // EX_SOFTWARE
}),
);
['SIGINT', 'SIGTERM'].forEach((s) => process.once(s, () => process.exit(0)));
const port = 3000;
let options = {};
// eslint-disable-next-line init-declarations
let engine;
try {
const key = readFileSync('key.pem');
const cert = readFileSync('cert.pem');
try {
engine = await import('node:https');
} catch {
console.error('https support is disabled');
process.exit(78); // EX_CONFIG
}
options = {
key,
cert,
};
} catch {
engine = await import('node:http');
}
const server = engine.createServer(options, ({ url }, res) => {
if (url === '/' || url === '/index.html') {
res
.writeHead(200)
.end(
'<!doctype html><title>sdavids-docker-healthcheck-js-nodejs</title><h1>sdavids-docker-healthcheck-js-nodejs</h1>',
);
} else if (url === '/-/health/liveness') {
res.writeHead(200).end('{}');
} else {
res.writeHead(404).end('Not found');
}
});
server.keepAliveTimeout = 5000;
server.requestTimeout = 5000;
server.timeout = 5000;
server.listen(port);
console.log(
`Listen local: http${Object.hasOwn(options, 'key') ? 's' : ''}://localhost:${port}`,
);