-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
98 lines (82 loc) · 2.94 KB
/
index.js
File metadata and controls
98 lines (82 loc) · 2.94 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
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
const app = require('express')();
const mysql = require('mysql');
const { createServer } = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('./certificates/selfsigned.key'),
cert: fs.readFileSync('./certificates/selfsigned.crt'),
};
const httpsServer = createServer(options, app);
const { Server } = require('socket.io');
const io = new Server(httpsServer, {
serveClient: false,
/*cors: {
origin: '*',
},*/
});
/* ******************************************************************* */
/* *********************** MIDDLEWARE & ROUTES *********************** */
/* ******************************************************************* */
const { ioAuth, corsAuth } = require('./middleware/auth');
const authRouter = require('./routes/auth');
const animationsRouter = require('./routes/animations');
const animateursRouter = require('./routes/animateurs');
const clientsRouter = require('./routes/clients');
const lieuxRouter = require('./routes/lieux');
/* ********************************************************* */
/* *********************** DATABASE ************************ */
/* ********************************************************* */
const db_config = {
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DATABASE,
};
var db = null;
function handleDisconnect() {
db = mysql.createConnection(db_config);
db.connect(err => {
if (err) {
console.log('> Error when connecting to Database : ', err);
setTimeout(handleDisconnect, 2000);
} else console.log('> SQL Database Ready');
});
db.on('error', function (err) {
console.log('> Database Error : ', err);
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
console.log('> Reconnecting to Database...');
handleDisconnect();
} else throw err;
});
}
handleDisconnect();
setInterval(() => {
db !== null ? db.query('SELECT 1') : null;
}, 5000);
/* **************************************************************** */
/* *************************** API ROUTES ************************* */
/* **************************************************************** */
app.set('io', io)
.use(corsAuth)
.use('/auth', authRouter(db))
.use('/api', animationsRouter(app, db))
.use('/api', animateursRouter(app, db))
.use('/api', clientsRouter(app, db))
.use('/api', lieuxRouter(app, db))
.route('*')
.all((req, res, next) => {
res.sendStatus(404);
});
/* **************************************************************** */
/* *************************** SOCKET IO ************************** */
/* **************************************************************** */
io.on('connection', socket => {
ioAuth(socket, io);
});
/* **************************************************************** */
/* *************************** SERVER START ************************ */
/* **************************************************************** */
httpsServer.listen('passenger', err => {
if (err) throw err;
console.log(`> Server Ready`);
});