-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (44 loc) · 1.4 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
import './config/config.js';
import express from 'express';
import path, { dirname } from 'path';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
import cors from 'cors';
import { fileURLToPath } from 'url';
import database from './db/index.js';
import routes from './routes/index.js';
import prometheusRouter from './routes/prometheusRoutes.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// adding routes
app.use(process.env.BASE_ENDPOINT, routes);
app.use(prometheusRouter);
const connectWithRetry = async () => {
try {
await database.authenticate();
console.log('Database connected');
// Migrate if there are any pending migrations
database.sync({ alter: true })
.then(() => {
console.log('Database migrated');
app.emit('dbReady');
})
.catch((err) => {
console.error('Unable to migrate:', err);
});
} catch (error) {
console.log('Unable to connect to the database');
console.log('Retrying in 5 seconds...');
setTimeout(connectWithRetry, 5000);
}
};
app.listen(process.env.PORT, () => {
console.log('Server is up on port', (process.env.PORT));
connectWithRetry();
});
export default app;