-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRekonAPIGateway.js
370 lines (314 loc) · 14.8 KB
/
RekonAPIGateway.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import ky from 'ky';
import express from 'express';
import cors from 'cors';
import 'dotenv/config';
import * as swaggerUI from 'swagger-ui-express';
import fs from 'fs';
import YAML from 'yaml';
import { Server } from 'socket.io';
import { createServer } from 'node:http';
const swaggerDocument = YAML.parse(fs.readFileSync('./swagger.yaml', 'utf8'));
let app = express();
app.use(express.json({limit: '1mb'}));
app.use(
express.urlencoded({
extended: true,
})
);
app.use((err, req, res, next) => {
// Handle the error here
console.error(err.stack);
res.status(500).send('Something broke!');
});
process.on('uncaughtException', (err) => {
console.error('Uncaught Exception:', err);
});
app.use(cors());
app.use('/docs', swaggerUI.serve, swaggerUI.setup(swaggerDocument));
const server = createServer(app);
const io = new Server(server);
let LAST_HEARTBEATS = {'accounts': null, 'groups': null, 'storage': null, 'auth': null, 'events': null}
const START_TIME = performance.now();
if (process.env.DOCS_ENABLED) {
}
function formatDuration(ms) {
// Convert milliseconds to hours, minutes, seconds
let hours = Math.floor(ms / 3600000); // 1 hour = 3600000 ms
let minutes = Math.floor((ms % 3600000) / 60000); // 1 minute = 60000 ms
let seconds = Math.floor((ms % 60000) / 1000); // 1 second = 1000 ms
let milliseconds = Math.floor(ms % 1000); // Remaining milliseconds
// Format hours, minutes, seconds, and milliseconds
return `${hours}h ${minutes}m ${seconds}s ${milliseconds}ms`;
}
// WebSockets forwarding
io.on('connection', (socket) => {
console.log('socket connected');
});
// HTTP forwarding and delivery
app.get('/', (req, res) => {
res.send(`<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Rekon API Gateway</title>
<style>body, html {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
width: 100%;
height: 100%;
}</style>
</head>
<body>
<!-- partial:index.partial.html -->
<h1>404 - Page not found</h1>
<p>Why are you trying to access this page anyway?</p>
<!-- partial -->
</body>
</html>
`)
})
app.post('/accounts/registerUserAccount', async (req, res) => {
const email = req.body.email;
const password = req.body.password;
console.log(email, password)
const json = await ky.post('http://127.0.0.1:8235/registerUserAccount', {json: {email: email, password: password, secret: process.env.SERVER_SECRET}}).json();
console.log(json)
return res.json(json)
});
app.post('/accounts/verifyEmailCode', async (req, res) => {
const id = req.body.id;
const code = req.body.code;
const verify = req.body.verify;
const json = await ky.post('http://127.0.0.1:8235/verifyEmailCode', {json: {id: id, code: code, verify: verify, secret: process.env.SERVER_SECRET}}).json();
console.log(json);
return res.json(json);
});
app.get('/accounts/getAccountID' , async (req, res) => {
const email = req.query.email;
const json = await ky.get(`http://127.0.0.1:8235/getAccountID?email=${email}&secret=${process.env.SERVER_SECRET}`).json();
return res.json(json);
});
app.post('/accounts/updateUsername', async (req, res) => {
const user_token = req.body.userToken;
const new_username = req.body.newUsername;
const json = await ky.post('http://127.0.0.1:8235/updateUsername', {json: {userToken: user_token, newUsername: new_username, secret: process.env.SERVER_SECRET}}).json();
return res.json(json);
});
app.post('/accounts/updateTeamNumber', async (req, res) => {
const user_token = req.body.userToken;
const new_team_number = req.body.newTeamNumber;
const json = await ky.post('http://127.0.0.1:8235/updateTeamNumber', {json: {userToken: user_token, newTeamNumber: new_team_number, secret: process.env.SERVER_SECRET}}).json();
return res.json(json);
});
app.post('/accounts/updateBio', async (req, res) => {
const user_token = req.body.userToken;
const new_bio = req.body.newBio;
const json = await ky.post('http://127.0.0.1:8235/updateBio', {json: {userToken: user_token, newBio: new_bio, secret: process.env.SERVER_SECRET}}).json();
return res.json(json);
});
app.post('/accounts/loginUserAccount', async (req, res) => {
const email = req.body.email;
const password = req.body.password;
const json = await ky.post('http://127.0.0.1:8235/loginUserAccount', {json: {email: email, password: password, secret: process.env.SERVER_SECRET}}).json();
console.log(json);
return res.json(json);
});
app.post('/accounts/removeGroup', async (req, res) => {
const user_token = req.body.userToken;
const group_token = req.body.groupToken;
const json = await ky.post('http://127.0.0.1:8235/removeGroup', {json: {userToken: user_token, groupToken: group_token, secret: process.env.SERVER_SECRET}}).json();
return res.json(json);
});
app.get('/accounts/getGroups', async (req, res) => {
const user_token = req.query.userToken;
const json = await ky.get(`http://127.0.0.1:8235/getGroups?userToken=${user_token}&secret=${process.env.SERVER_SECRET}`).json();
return res.json(json);
});
app.get('/accounts/getAccountData', async (req, res) => {
const account_id = req.query.account_id;
const json = await ky.get(`http://127.0.0.1:8235/getAccountData?account_id=${account_id}&secret=${process.env.SERVER_SECRET}`).json();
return res.json(json);
});
app.get('/accounts/getIDfromToken', async (req, res) => {
const user_token = req.query.userToken;
const json = await ky.get(`http://127.0.0.1:8235/getIDfromToken?userToken=${user_token}&secret=${process.env.SERVER_SECRET}`).json();
return res.json(json);
});
app.post('/uploads/getUploadToken', async (req, res) => {
const type = req.body.type;
const token = req.body.token;
const numChunks = req.body.numChunks;
const fileType = req.body.fileType;
const json = await ky.post('http://127.0.0.1:8237/getUploadToken', {json: {type: type, token: token, secret: process.env.SERVER_SECRET, numChunks: numChunks, fileType: fileType}}).json();
return res.json(json);
});
app.post('/uploads/completeUpload', async (req, res) => {
const userToken = req.body.userToken;
const uploadToken = req.body.uploadToken;
const numChunks = req.body.numChunks;
const fileType = req.body.fileType;
const json = await ky.post('http://127.0.0.1:8237/completeUpload', {json: {userToken: userToken, uploadToken: uploadToken, numChunks: numChunks, fileType: fileType, secret: process.env.SERVER_SECRET}}).json();
return res.json(json);
});
app.post('/uploads/uploadChunk', async (req, res) => {
const userToken = req.body.userToken;
const uploadToken = req.body.uploadToken;
const chunk = req.body.chunk;
const index = req.body.index;
const json = await ky.post('http://127.0.0.1:8237/uploadChunk', {json: {userToken: userToken, uploadToken: uploadToken, chunk: chunk, index: index, secret: process.env.SERVER_SECRET}}).json();
return res.json(json);
});
app.get('/uploads/getUploadedFile', async (req, res) => {
const userToken = req.query.userToken;
const uploadToken = req.query.uploadToken;
const json = await ky.get(`http://127.0.0.1:8237/getUploadedFile?userToken=${userToken}&uploadToken=${uploadToken}&secret=${process.env.SERVER_SECRET}`).json();
// Map common file extensions to MIME types
const mimeTypes = {
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'png': 'image/png',
'gif': 'image/gif',
'mp4': 'video/mp4',
'webm': 'video/webm',
'pdf': 'application/pdf',
'txt': 'text/plain'
};
// Get the file extension without the dot and convert to lowercase
const fileExt = json.fileType.replace('.', '').toLowerCase();
const mimeType = mimeTypes[fileExt] || 'application/octet-stream';
console.log('File extension:', fileExt);
console.log('MIME type:', mimeType);
console.log('Content length:', json.file.length);
res.set('Content-Type', mimeType);
// Also set Content-Length header
res.set('Content-Length', json.file.length);
// Add Content-Disposition header to suggest filename
res.set('Content-Disposition', `inline; filename="file.${fileExt}"`);
return res.send(Buffer.from(json.file));
});
app.get('/uploads/getProfilePicture', async (req, res) => {
const accountID = req.query.accountID;
const image = await ky.get(`http://127.0.0.1:8237/getProfilePicture?accountID=${accountID}&secret=${process.env.SERVER_SECRET}`).arrayBuffer();
res.set('Content-Type', 'image/jpeg');
return res.send(Buffer.from(image));
});
app.get('/uploads/getUserFiles', async (req, res) => {
const userToken = req.query.userToken;
console.log('getting files for user', userToken);
const json = await ky.get(`http://127.0.0.1:8237/getUserFiles?userToken=${userToken}&secret=${process.env.SERVER_SECRET}`).json();
return res.json(json);
});
app.post('/uploads/deleteFile', async (req, res) => {
const userToken = req.body.userToken;
const uploadToken = req.body.uploadToken;
const json = await ky.post('http://127.0.0.1:8237/deleteFile', {json: {userToken: userToken, uploadToken: uploadToken, secret: process.env.SERVER_SECRET}}).json();
return res.json(json);
});
app.post('/groups/createGroup', async (req, res) => {
const groupName = req.body.group_name;
const groupDescription = req.body.group_description;
const teamNumber = req.body.team_number;
const userToken = req.body.user_token;
const json = await ky.post('http://127.0.0.1:8236/createGroup', {json: {group_name: groupName, group_description: groupDescription, team_number: teamNumber, user_token: userToken, secret: process.env.SERVER_SECRET}}).json();
return res.json(json);
});
app.get('/groups/groupInfo', async (req, res) => {
const groupID = req.query.groupID;
const json = await ky.get(`http://127.0.0.1:8236/groupInfo?groupID=${groupID}&secret=${process.env.SERVER_SECRET}`).json();
return res.json(json);
});
// public status endpoints
app.get('/gateway/status', async (req, res) => {
return res.json({'status': 'healthy', 'description': `The Rekon API Gateway has been online for ${formatDuration(performance.now() - START_TIME)}`})
})
app.get('/accounts/status', async (req, res) => {
const currentTime = performance.now();
if (LAST_HEARTBEATS.accounts == null) {
res.json({'status': 'unknown', 'description': 'No heartbeat has been recieved by the account server.'});
} else if (currentTime - LAST_HEARTBEATS.accounts > 15000 && currentTime - LAST_HEARTBEATS.accounts < 45000) {
res.json({'status': 'healthy', 'description': `The account server hasn't responded in ${currentTime - LAST_HEARTBEATS.accounts}ms, server is likely under heavy load.`});
} else if (currentTime - LAST_HEARTBEATS.accounts > 45000) {
res.json({'status': 'offline', 'description': `The account server has not responded in ${currentTime - LAST_HEARTBEATS.accounts}ms. The service is likely offline.`});
} else if (currentTime - LAST_HEARTBEATS.accounts < 15000) {
res.json({'status': 'healthy', 'description': 'The account server is working as expected!'});
} else {
res.json({'status': 'error', 'description': 'There was an unexpected issue when reading the status of the account server.'});
}
})
app.get('/groups/status', async (req, res) => {
const currentTime = performance.now();
if (LAST_HEARTBEATS.accounts == null) {
res.json({'status': 'unknown', 'description': 'No heartbeat has been recieved by the group management server.'});
} else if (currentTime - LAST_HEARTBEATS.accounts > 15000 && currentTime - LAST_HEARTBEATS.accounts < 45000) {
res.json({'status': 'healthy', 'description': `The group management server hasn't responded in ${currentTime - LAST_HEARTBEATS.accounts}ms, server is likely under heavy load.`});
} else if (currentTime - LAST_HEARTBEATS.accounts > 45000) {
res.json({'status': 'offline', 'description': `The group management server has not responded in ${currentTime - LAST_HEARTBEATS.accounts}ms. The service is likely offline.`});
} else if (currentTime - LAST_HEARTBEATS.accounts < 15000) {
res.json({'status': 'healthy', 'description': 'The group management server is working as expected!'});
} else {
res.json({'status': 'error', 'description': 'There was an unexpected issue when reading the status of the group management server.'});
}
})
// internal heartbeat endpoints, secured with a common secret.
app.put('/accounts/status/heartbeat', async (req, res) => {
if (req.query.secret == process.env.SERVER_SECRET) {
LAST_HEARTBEATS.accounts = performance.now();
res.json({'response': 'Hello account server!!'})
} else {
res.json({'response': "You're not the account server >:("})
}
});
app.put('/groups/status/heartbeat', async (req, res) => {
if (req.query.secret == process.env.SERVER_SECRET) {
LAST_HEARTBEATS.groups = performance.now();
res.json({'response': 'Hello group server!!'})
} else {
res.json({'response': "You're not the group server >:("})
}
});
app.put('/storage/status/heartbeat', async (req, res) => {
if (req.query.secret == process.env.SERVER_SECRET) {
LAST_HEARTBEATS.storage = performance.now();
res.json({'response': 'Hello storage server!!'})
} else {
res.json({'response': "You're not the storage server >:("})
}
});
app.put('/auth/status/heartbeat', async (req, res) => {
if (req.query.secret == process.env.SERVER_SECRET) {
LAST_HEARTBEATS.auth = performance.now();
res.json({'response': 'Hello auth server!!'})
} else {
res.json({'response': "You're not the auth server >:("})
}
});
app.put('/events/status/heartbeat', async (req, res) => {
if (req.query.secret == process.env.SERVER_SECRET) {
LAST_HEARTBEATS.events = performance.now();
res.json({'response': 'Hello events server!!'})
} else {
res.json({'response': "You're not the events server >:("})
}
});
// internal api endpoints, only meant for communication between internal microservices
app.post('/internal/auth/verifyToken', async (req, res) => {
if (req.body.secret != process.env.SERVER_SECRET) {
return res.json({'error': true, 'message': 'This is an internal endpoint. Client applications do not have permission to access this endpoint.', code: 'gateway-internal-endpoint'})
}
const tokenInfo = await ky.post(`http://127.0.0.1:8238/verifyToken`, {json: {secret: process.env.SERVER_SECRET, token: req.body.token}}).json();
return res.json(tokenInfo);
});
app.post('/internal/accounts/addGroup', async (req, res) => {
if (req.body.secret != process.env.SERVER_SECRET) {
return res.json({'error': true, 'message': 'This is an internal endpoint. Client applications do not have permission to access this endpoint.', code: 'gateway-internal-endpoint'})
}
const user_token = req.body.userToken;
const group_token = req.body.groupToken;
const json = await ky.post('http://127.0.0.1:8235/addGroup', {json: {userToken: user_token, groupToken: group_token, secret: process.env.SERVER_SECRET}}).json();
return res.json(json);
});
server.listen(8234, () => {
console.log(`Rekon gateway running at port 8234`);
});