-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
64 lines (54 loc) · 2.02 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
'use strict';
var path = require('path');
var http = require('http');
require('dotenv').config();
// Handles API middleware
var oas3Tools = require('oas3-tools');
const serverPort = process.env.PORT;
const { connectToDb } = require('./utils/mongo');
const { exit } = require('process');
const { connectToRedis, getRedisClient } = require('./utils/ratelimiter');
const { TIMEOUT } = require('dns');
// swaggerRouter configuration
var options = {
routing: {
controllers: path.join(__dirname, './controllers')
},
};
// OAS3 handles routing middleware for APi endpoints by using openAPI.yaml document
var expressAppConfig = oas3Tools.expressAppConfig(path.join(__dirname, 'api/openapi.yaml'), options);
var app = expressAppConfig.getApp();
async function startServer() {
let count = 1;
let connected = false;
//run connection in a loop to try to fix docker compose connection error
//ensures database is running before trying to connect
while(!connected){
try {
console.log(`Attempt number ${count}`)
await connectToDb(function (err) {
if (err) {
console.error('Failed to connect to MongoDB:', err);
throw err;
}
});
connected = true;
await connectToRedis();
// Initialize the Swagger middleware
http.createServer(app).listen(serverPort, function () {
console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort);
});
} catch (error) {
console.error('Failed to start server:', error);
if (count >= 100){
exit(1);
}
count++;
await new Promise((resolve) => {
setTimeout(resolve, 3000); //passes resolve, will return sucessfully after 3 seconds
});
}
}
}
startServer();