-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
224 lines (182 loc) · 6.52 KB
/
Copy pathserver.js
File metadata and controls
224 lines (182 loc) · 6.52 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
214
215
216
217
218
219
220
221
222
223
224
// https://github.com/nko4/website/blob/master/module/README.md#nodejs-knockout-deploy-check-ins
require('nko')('QGxy2-Aqj_HFjEI2');
//setup Dependencies
var connect = require('connect'),
express = require('express'),
socketio = require('socket.io'),
socketHandler = require('./socketHandler'),
mongoose = require('mongoose'),
passport = require('passport'),
GitHubStrategy = require('passport-github').Strategy,
parseCookie = require('connect').utils.parseCookie,
MemoryStore = express.session.MemoryStore,
sessionStore = new MemoryStore();
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
passport.use(new GitHubStrategy({
clientID: '32a5a6f2539e177a2d34',
clientSecret: 'b52a62cf75d3bd6371477315a0202a43e465c631',
callbackURL: "http://team-snacky-cake.2013.nodeknockout.com/callback"
},
function(accessToken, refreshToken, profile, done) {
return done(null, profile);
}
));
mongoose.connect('mongodb://localhost/tandem');
//Setup Express
var isProduction = (process.env.NODE_ENV === 'production'),
port = (isProduction ? 80 : 8000),
server = express.createServer();
server.configure(function () {
server.set('views', __dirname + '/views');
server.set('view options', {layout: false});
server.use(connect.bodyParser());
server.use(express.cookieParser());
server.use(connect['static'](__dirname + '/staticfiles'));
server.use(express.methodOverride());
server.use(express.session({secret:'monkey man', store: sessionStore, key: 'express.sid'}));
server.use(passport.initialize());
server.use(passport.session());
server.use(server.router);
});
//setup the errors
server.error(function (err, req, res, next) {
if (err instanceof NotFound) {
res.render('404.jade', {
locals: {
title : '404 - Not Found',
description: '',
author: '',
analyticssiteid: 'XXXXXXX'
},
status: 404
});
} else {
res.render('500.jade', {
locals: {
title : 'The Server Encountered an Error',
description: '',
author: '',
analyticssiteid: 'XXXXXXX',
error: err
}, status: 500
});
}
});
server.listen(port);
io = socketio.listen(server);
io.set('authorization', function (data, accept) {
if (data.headers.cookie) {
data.cookie = parseCookie(data.headers.cookie);
data.sessionID = data.cookie['express.sid'];
// (literally) get the session data from the session store
sessionStore.get(data.sessionID, function (err, session) {
if (err || !session) {
// if we cannot grab a session, turn down the connection
accept('Error', false);
} else {
// save the session data and accept the connection
data.session = session;
accept(null, true);
}
});
} else {
return accept('No cookie transmitted.', false);
}
});
allClients = {};
io.sockets.on('connection', function(socket) {
socket.user = socket.handshake.session.passport.user;
allClients[socket.user.username] = socket;
socketHandler.init(socket, allClients);
socket.on('disconnect', function() {
if (socket.user) {
delete allClients[socket.user.username];
}
});
console.log('A socket with sessionID ' + socket.handshake.sessionID + ' connected!');
});
server.get('/', function (req, res) {
res.render('index.jade', {
locals : {
title : 'Tandem :: Co-Authoring Done Right',
description: 'Take co-authoring to the next level with Tandem. Virtually collaborate on your next masterpiece with authors around the world.',
author: 'Team Snacky Cake',
analyticssiteid: 'XXXXXXX'
}
});
});
var Author = require('./models/Author');
server.get('/app', function (req, res) {
if (!req.session.passport.user) {
res.redirect('/');
} else {
Author.findOne({username: req.user.username}, function (err, u) {
if (err) throw err;
if (!u) {
u = new Author({username: req.user.username});
u.save();
}
res.render('app.jade', {
locals : {
title : 'Tandem :: Do A Thing',
uid: u._id.toString(),
description: 'Take co-authoring to the next level with Tandem. Virtually collaborate on your next masterpiece with authors around the world.',
author: 'Team Snacky Cake',
analyticssiteid: 'XXXXXXX'
}
});
});
}
});
server.get('/login', passport.authenticate('github', {
scope: [
'public_repo'
]
}));
server.get('/callback', passport.authenticate('github', {
failureRedirect: '/'
}),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/app');
});
server.get('/logout', function (req, res) {
req.logout();
delete req.session;
res.redirect('/');
});
//A Route for Creating a 500 Error (Useful to keep around)
server.get('/500', function (req, res) {
throw new Error('This is a 500 Error');
});
//The 404 Route (ALWAYS Keep this as the last route)
server.get('/*', function (req, res) {
throw new NotFound();
});
function NotFound (msg) {
this.name = 'NotFound';
Error.call(this, msg);
Error.captureStackTrace(this, arguments.callee);
}
console.log('Listening on http://localhost:' + port);
// http.createServer(function (req, res) {
// // http://blog.nodeknockout.com/post/35364532732/protip-add-the-vote-ko-badge-to-your-app
// var voteko = '<iframe src="http://nodeknockout.com/iframe/team-snacky-cake" frameborder=0 scrolling=no allowtransparency=true width=115 height=25></iframe>';
// res.writeHead(200, {'Content-Type': 'text/html'});
// res.end('<html><body>' + voteko + '</body></html>\n');
// }).listen(port, function(err) {
// if (err) { console.error(err); process.exit(-1); }
// // if run as root, downgrade to the owner of this file
// if (process.getuid() === 0) {
// require('fs').stat(__filename, function(err, stats) {
// if (err) { return console.error(err); }
// process.setuid(stats.uid);
// });
// }
// console.log('Server running at http://0.0.0.0:' + port + '/');
// });