-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
110 lines (86 loc) · 3.29 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
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
99
100
101
102
103
104
105
106
107
108
109
110
var express = require('express');
var passport = require('passport');
var webpack = require('webpack');
var app = express();
if (process.env.NODE_ENV !== 'production') {
// Для автоматического обновления модулей на клиенте
var webpackDevMiddleware = require('webpack-dev-middleware');
var webpackHotMiddleware = require('webpack-hot-middleware');
var compiler = webpack(require('./webpack.config'));
app.use(webpackDevMiddleware(compiler, {noInfo: true, publicPath: '/dist/'}));
app.use(webpackHotMiddleware(compiler));
}
app.use(express.static('public/js'));
app.use(express.static('public/css'));
app.set('json spaces', 40);
// Set up the Session middleware using a MongoDB session store
expressSession = require("express-session");
var sessionMiddleware = expressSession({
secret: "vk-assistant",
store: new (require("connect-mongo")(expressSession))({
url: process.env.MONGOLAB_URI || "mongodb://localhost/vk-assistant"
}),
resave: true,
saveUninitialized: true
});
app.use(sessionMiddleware);
// init middleware
var bootstrap = require('./app/bootstrap');
new bootstrap(app);
// create server base on app
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.use(function(socket, next){
// Wrap the express middleware
sessionMiddleware(socket.request, {}, next);
});
let User = require('./app/models/user').User;
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
err ? done(err) : done(null, user);
});
});
// processing ws connections
io.on('connection', (socket) => {
socket.on('clients', (data, cb) => {
cb('23');
});
require('./app/controllers/ws/profile').respond(socket);
require('./app/controllers/ws/user').respond(socket);
require('./app/controllers/ws/dialogs').respond(socket);
});
// authentication middleware
app.use(passport.initialize());
app.use(passport.session());
// pass user data to views
app.use(function(req,res,next){
var user = {
name: req.user ? req.user.username : '',
authorized: req.user ? true : false
};
res.locals.user = user;
res.locals.userJson = JSON.stringify(res.locals.user);
next();
});
var mustBeAuthenticated = function (req, res, next) {
req.isAuthenticated() ? next() : res.json({status: 'err', msg: 'authentication is required for requested action'});
};
app.all('/services', mustBeAuthenticated);
app.all('/services/*', mustBeAuthenticated);
app.get('/', function (req, res) {
res.render('home', {user: req.user});
});
var ServicesController = require('./app/controllers/http/services');
app.get('/services', ServicesController.getHandler('home'));
app.get('/services/msg', ServicesController.getHandler('msg'));
app.get('/services/profile', ServicesController.getHandler('profile'));
app.get('/services/dialogs', ServicesController.getHandler('dialogs'));
app.get('/services/send', ServicesController.getHandler('send'));
app.post('/services/setLike', ServicesController.getHandler('setLike'));
app.get('/services/photos', ServicesController.getHandler('photos'));
http.listen(process.env.PORT || 3000, function () {
console.log('Example app listening on port 3000!')
});