forked from daimus/dnms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
67 lines (61 loc) · 1.94 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
const express = require('express');
const expressSession = require('express-session');
const flash = require('connect-flash');
const dotenv = require('dotenv').config();
const methodOverride = require('method-override');
const jsend = require('jsend');
const cookieParser = require('cookie-parser');
const passport = require('passport');
const passportConfig = require('./config/passport');
const {logger} = require('./config/logger');
const sharedSession = require('express-socket.io-session');
const MySQLStore = require('connect-mysql')(expressSession);
const app = express();
const httpServer = require("http").createServer(app);
const io = require("socket.io")(httpServer);
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
app.use(express.static('public'));
app.set('view engine', 'ejs');
app.use(jsend.middleware);
app.use(methodOverride('_method'));
app.use(cookieParser(process.env.SESSION_SECRET));
const session = expressSession({
resave: true,
saveUninitialized: true,
secret: process.env.SESSION_SECRET,
cookie: {
maxAge: 2678400000
},
store: new MySQLStore({
config: {
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME
}
})
});
app.use(session);
app.use(flash());
app.use((req, res, next) => {
res.locals.alert = req.flash('alert');
next();
});
app.use(passport.initialize());
app.use(passport.session());
const routes = require('./routes');
routes(app);
io.use(sharedSession(session));
io.on("connection", (socket) => {
const socketAction = require('./config/socket-action');
socketAction.init(socket);
logger.silly(`incoming socket connection: ${socket.id}`);
socket.on('disconnect', () => {
logger.silly(`socket connection terminated: ${socket.id}`);
});
});
httpServer.listen(process.env.APP_PORT, () => {
logger.info(`server is running`);
});