-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
120 lines (93 loc) · 2.78 KB
/
server.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import express from 'express';
import cors from 'cors';
import { getUserId, getRDSUserSelfDetails } from './utils.js';
import {
CORS_OPTIONS,
UPDATE_SSE_EVENTS_TIME,
SSE_RESPONSE_HEADER,
LOCAL_PORT,
} from './constants.js';
const server_port = process.env.PORT || LOCAL_PORT;
const app = express();
app.use(express.json());
app.use(cors(CORS_OPTIONS));
app.get('/', async (req, res) => {
try {
res.status(200).send('Ok');
} catch (error) {
console.error(error);
res.status(500).send({
error: 'Internal server error',
message: 'Some this went wrong please contact admin',
});
}
});
const users = {};
app.get('/online-members', async (req, res) => {
try {
const rdsCookie = req.headers.cookie;
const response = await getRDSUserSelfDetails(rdsCookie);
if (response.status !== 200) {
return res.status(response.status).send({ ...response.data });
}
const userId = await getUserId(response);
// data for sending
let data;
users[userId] = req;
res.writeHead(200, SSE_RESPONSE_HEADER);
const intervalId = setInterval(() => {
console.log(`*** Interval loop. userId: "${userId}"`);
data = {
users: Object.keys(users),
};
if (!data) {
res.write(`:\n\n`);
} else {
res.write(`data: ${JSON.stringify(data)}\n\n`);
}
}, UPDATE_SSE_EVENTS_TIME);
// Note: Heatbeat for avoidance of client's request timeout of first time (30 sec)
res.write(`:\n\n`);
req.on('close', () => {
console.log(`*** Close. userId: "${userId}"`);
// Breaks the interval loop on client disconnected
clearInterval(intervalId);
// Remove from connections
delete users[userId];
});
req.on('end', () => {
console.log(`*** End. userId: "${userId}"`);
delete users[userId];
});
} catch (error) {
console.error(error);
res.status(500).send({
error: 'Internal server error',
message: 'Some this went wrong please contact admin',
});
}
});
app.get('/health-check', async (req, res) => {
try {
const rdsCookie = req.headers.cookie;
const response = await getRDSUserSelfDetails(rdsCookie);
res.status(response.status).send({ ...response.data });
} catch (error) {
console.error(error);
res.status(500).send({
error: 'Internal server error',
message: 'Some this went wrong please contact admin',
});
}
});
app.all('*', async(req,res)=>{
res.status(404).send({staus: 404, message: 'Route not found'});
})
app.use((error, request, response, next) => {
console.error( `error ${error.message}`)
const status = error.status || 400
response.status(status).send(error.message)
})
app.listen(server_port, () => {
console.log(`Example app listening on port ${server_port}`);
});