-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.txt
More file actions
213 lines (179 loc) · 6.8 KB
/
app.txt
File metadata and controls
213 lines (179 loc) · 6.8 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
@fileoverview app main file, for initialisation of the server
@author Jacques Dafflon jacques.dafflon@gmail.com
*/
var express = require('express')
, http = require('http')
, path = require('path')
, engine = require('ejs-locals')
, slashes = require("connect-slashes")
, routes = require('./routes')
, flash = require('connect-flash')
, passport = require('passport')
, LocalStrategy = require('passport-local').Strategy
, registration = require('./routes/registration');
// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing.
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
var User = db.model('User', schemas.userSchema);
var out = User.findById(id, function (err, user) {
if (user) {
done(err, user);
} else {
done(null,new Error('User ' + id + ' does not exist'));
}
})
});
// Use the LocalStrategy within Passport.
// Strategies in passport require a `verify` function, which accept
// credentials (in this case, a username and password), and invoke a callback
// with a user object. In the real world, this would query a database;
// however, in this example we are using a baked-in set of users.
passport.use(new LocalStrategy(
function(username, password, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
var User = db.model('User', schemas.userSchema);
var out = User.findOne({ name: username }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false, { message: 'Unknown user ' + username }); }
if (user.password != password) { return done(null, false, { message: 'Invalid password' }); }
return done(null, user);
})
});
}
));
app = express();
app.engine('ejs', engine);
// mongoose, db, and schemas are global
mongoose = require('mongoose');
db = mongoose.createConnection('localhost', 'asq');
schemas = require('./models/models.js');
/** Configure express */
app.configure(function() {
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser({uploadDir: './slides/'}));
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
//necessary initialization for passport plugin
app.use(passport.initialize());
app.use(flash());
app.use(passport.session());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public/'));
app.use(express.static(path.join(__dirname, '/public/')));
//used to append slashes at the end of a url (MUST BE after static)
app.use(slashes());
});
app.configure('development', function(){
app.use(express.errorHandler());
});
/** Routing */
app.get('/', ensureAuthenticated, function(req, res){
res.render('logged');
});
/**
@description Prevent to serve included js files with presentations.
This will serve a custom verion of impress.js and the appropriate websocket
module.
*/
app.get('/:whatever/js/*', function(req, res) {
res.sendfile('./js/' + req.params[0]);
});
app.get('/live/:user/js/*', function(req, res) {
res.sendfile('./js/' + req.params[0]);
});
app.get('/live/:user/', routes.live);
app.get('/live/:user/*', function(req, res) {
res.sendfile('./slides/demo/' + req.params[0]);
});
app.get('/admin/', ensureAuthenticated, routes.admin);
app.get('/admin/*', function(req, res) {
res.sendfile('./slides/demo/' + req.params[0]);
});
app.post('/upload', ensureAuthenticated, routes.upload);
app.get('/upload', ensureAuthenticated, routes.showUpload);
//Someone types /signup URL, which has no meaning. He is redirected.
app.get('/signup', function(req, res){
res.redirect('/');
});
//Registration happened.
app.post('/signup', registration.signup);
//Someone types /user URL, if he's authenticated he sees his profile page, otherwise gets redirected
app.get('/user', ensureAuthenticated, function(req,res) {
res.render('user', { user: req.user, message: req.flash('error') });
});
//Someone tries to Log In, if plugin authenticates the user he sees his profile page, otherwise gets redirected
app.post('/user', passport.authenticate('local', { failureRedirect: '/', failureFlash: true}) ,function(req, res) {
res.redirect('/user');
});
//The user logs out, and get redirected
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
//Serving static files
app.get('/images/:path', registration.get);
// Simple route middleware to ensure user is authenticated.
// Use this route middleware on any resource that needs to be protected. If
// the request is authenticated (typically via a persistent login session),
// the request will proceed. Otherwise, the user will be redirected to the
// login page.
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.render('index', { error: req.flash('error') });
}
/** HTTP Server */
var server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
/**
@description Require socket.io (websocket wrapper) and listen to the server.
This needs to be requierd at this point since we need the server to already
be running.
*/
var io = require('socket.io').listen(server);
var currentSlide = 0;
/**
@description Configure socket server
@todo Handle disconnect, authentification and multiple sessions
*/
io.sockets.on('connection', function(socket){
/** @function Handle connection from viewer. */
socket.on('viewer', function(event) {
socket.join('viewers');
socket.emit('goto', {slide:currentSlide});
io.sockets.in('admins').emit('new', {});
});
/** @function Handle connection from admin. */
socket.on('admin', function(event) {
socket.join('admins');
});
/**
@ function Handle goto event.
When a goto event from an admin is sent, update status of current slide
and informs all viewers of the new current slide
@todo Implement authentification to make sure the event comes from a
valid admin.
*/
socket.on('goto', function(event) {
currentSlide = event.slide;
io.sockets.in('viewers').emit('goto', event);
});
socket.on('impress:start', function(event) {
io.sockets.in('viewers').emit('impress:start', event);
})
});