-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathhttp-server.ts
More file actions
63 lines (52 loc) · 1.85 KB
/
Copy pathhttp-server.ts
File metadata and controls
63 lines (52 loc) · 1.85 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
60
61
62
63
import type { AccessTokenPayload } from '@deeplib/misc';
import { mainLogger } from '@stdlib/misc';
import cookie from 'cookie';
import type { IncomingMessage } from 'http';
import { createServer } from 'http';
import jwt from 'jsonwebtoken';
import { once } from 'lodash';
import type { Socket } from 'net';
import { collectDefaultMetrics, Registry } from 'prom-client';
import { wsServer } from 'src/ws-server';
const moduleLogger = mainLogger.sub('http-server.ts');
const prometheusRegistry = new Registry();
collectDefaultMetrics({ register: prometheusRegistry });
export const httpServer = once(() =>
createServer((req, res) => {
if (req.url === '/metrics') {
res.writeHead(200, { 'Content-Type': prometheusRegistry.contentType });
res.end(prometheusRegistry.metrics());
} else {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Okay');
}
}),
);
httpServer().on('upgrade', (req: IncomingMessage, socket: Socket, head) => {
const funcLogger = moduleLogger.sub('Upgrade');
const cookies = cookie.parse(req.headers.cookie ?? '');
funcLogger.info('Access token: %o', cookies['accessToken']);
funcLogger.info('Logged in: %o', cookies['loggedIn']);
if (cookies['accessToken'] != null && cookies['loggedIn'] === 'true') {
try {
const jwtPayload = jwt.verify(
cookies['accessToken'],
process.env.ACCESS_SECRET,
) as unknown as AccessTokenPayload;
req.sessionId = jwtPayload.sid;
moduleLogger.info(
`${socket.remoteAddress}${req.url}: Authentication successful`,
);
} catch (error) {
//
}
}
if (req.sessionId == null) {
moduleLogger.info(
`${socket.remoteAddress}${req.url}: Unauthenticated connection`,
);
}
wsServer().handleUpgrade(req, socket, head, (websocket) => {
wsServer().emit('connection', websocket, req);
});
});