-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (55 loc) · 2.26 KB
/
app.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
const express = require('express');
const app = express();
require("dotenv").config();
const isProduction = process.env.NODE_ENV === "production";
const fs = require('fs');
const path = require('path');
const http = isProduction ? require('http') : require('httpolyglot');
/*
const options = {
key: fs.readFileSync(path.join(__dirname,'.','ssl','key.pem'), 'utf-8'),
cert: fs.readFileSync(path.join(__dirname,'.','ssl','cert.pem'), 'utf-8')
}
*/
//const { socketIo } = require('./utilities/socket.io/socket.io');
const session = require('cookie-session');
const flash = require('express-flash');
const methodOverride = require('method-override')
const ejsMate = require('ejs-mate');
const { hasRoom, getUserStats } = require('./middleware/hasRoom');
const passport = require('passport');
const initializePassport = require('./passportConfig');
initializePassport(passport);
const userRoutes = require('./routes/users');
const roomRoutes = require('./routes/rooms');
/* Middleware */
app.engine('ejs', ejsMate);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.urlencoded({ extended: false }));
app.use(methodOverride('_method'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({ secret: 'secret', resave: false, saveUninitialized: false }));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(async (req, res, next) => {
res.locals.authenticatedUser = req.user;
res.locals.currentUrl = req.url;
if (req.user) {
res.locals.authenticatedUser.username += ` ${await getUserStats(req.user.id)} `;
const userRoomId = await hasRoom(req.user.id);
res.locals.authenticatedUserRoomId = (userRoomId == undefined) ? false : userRoomId.id;
}
next();
});
app.get('/', (req, res) => {
res.render('index');
});
// Routes
app.use('/users', userRoutes);
app.use('/rooms', roomRoutes);
const httpsServer = isProduction ? http.createServer(app) : http.createServer({ key: fs.readFileSync(path.join(__dirname,'.','ssl','key.pem'), 'utf-8'), cert: fs.readFileSync(path.join(__dirname,'.','ssl','cert.pem'), 'utf-8') }, app);
const io = require('socket.io')(httpsServer);
require('./utilities/socket.io/socketController')(io);
module.exports = httpsServer;