| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | var express = require('express'), | 
					
						
							| 
									
										
										
										
											2013-04-22 15:17:41 -04:00
										 |  |  | 	WebServer = express(), | 
					
						
							|  |  |  | 	server = require('http').createServer(WebServer), | 
					
						
							| 
									
										
										
										
											2013-04-28 13:58:19 -04:00
										 |  |  | 	RedisStore = require('connect-redis')(express), | 
					
						
							|  |  |  | 	path = require('path'), | 
					
						
							| 
									
										
										
										
											2013-05-01 12:54:04 -04:00
										 |  |  |     config = require('../config.js'), | 
					
						
							|  |  |  |     redis = require('redis'), | 
					
						
							| 
									
										
										
										
											2013-05-01 21:03:37 -04:00
										 |  |  | 	redisServer = redis.createClient(config.redis.port, config.redis.host, config.redis.options), | 
					
						
							|  |  |  | 	passport = require('passport'), | 
					
						
							| 
									
										
										
										
											2013-05-02 11:11:10 -04:00
										 |  |  | 	passportLocal = require('passport-local').Strategy, | 
					
						
							| 
									
										
										
										
											2013-05-02 12:13:06 -04:00
										 |  |  | 	passportTwitter = require('passport-twitter').Strategy, | 
					
						
							|  |  |  | 	login_strategies = []; | 
					
						
							| 
									
										
										
										
											2013-05-01 21:03:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | passport.use(new passportLocal(function(user, password, next) { | 
					
						
							|  |  |  | 	global.modules.user.loginViaLocal(user, password, function(login) { | 
					
						
							|  |  |  | 		if (login.status === 'ok') next(null, login.user); | 
					
						
							|  |  |  | 		else next(null, false, login); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-02 12:13:06 -04:00
										 |  |  | if (config.twitter.key.length > 0 && config.twitter.secret.length > 0) { | 
					
						
							|  |  |  | 	passport.use(new passportTwitter({ | 
					
						
							|  |  |  | 		consumerKey: config.twitter.key, | 
					
						
							|  |  |  | 		consumerSecret: config.twitter.secret, | 
					
						
							|  |  |  | 		callbackURL: config.url + "auth/twitter/callback" | 
					
						
							|  |  |  | 	}, function(token, tokenSecret, profile, done) { | 
					
						
							|  |  |  | 		global.modules.user.loginViaTwitter(profile.id, profile.username, function(err, user) { | 
					
						
							|  |  |  | 			if (err) { return done(err); } | 
					
						
							|  |  |  | 			done(null, user); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	})); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	login_strategies.push('twitter'); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-05-02 11:11:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 21:03:37 -04:00
										 |  |  | passport.serializeUser(function(user, done) { | 
					
						
							|  |  |  | 	done(null, user.uid); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | passport.deserializeUser(function(uid, done) { | 
					
						
							|  |  |  | 	done(null, { | 
					
						
							|  |  |  | 		uid: uid | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | (function(app) { | 
					
						
							|  |  |  | 	var templates = global.templates; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-23 19:38:48 +00:00
										 |  |  | 	function refreshTemplates() { | 
					
						
							|  |  |  | 		//need a better solution than copying this code on every call. is there an "onconnect" event?
 | 
					
						
							|  |  |  | 		if (DEVELOPMENT === true) { | 
					
						
							|  |  |  | 			// refreshing templates
 | 
					
						
							|  |  |  | 			modules.templates.init(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-04-24 16:42:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Middlewares
 | 
					
						
							|  |  |  | 	app.use(express.favicon());	// 2 args: string path and object options (i.e. expire time etc)
 | 
					
						
							| 
									
										
										
										
											2013-05-02 09:40:44 -04:00
										 |  |  | 	app.use(require('less-middleware')({ src: path.join(__dirname, '../', '/public') })); | 
					
						
							| 
									
										
										
										
											2013-04-28 13:58:19 -04:00
										 |  |  | 	app.use(express.static(path.join(__dirname, '../', 'public'))); | 
					
						
							| 
									
										
										
										
											2013-04-24 16:42:12 -04:00
										 |  |  | 	app.use(express.bodyParser());	// Puts POST vars in request.body
 | 
					
						
							|  |  |  | 	app.use(express.cookieParser());	// If you want to parse cookies (res.cookies)
 | 
					
						
							| 
									
										
										
										
											2013-04-28 13:58:19 -04:00
										 |  |  | 	app.use(express.compress()); | 
					
						
							| 
									
										
										
										
											2013-04-25 11:15:03 -04:00
										 |  |  | 	app.use(express.session({ | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 		store: new RedisStore({ | 
					
						
							| 
									
										
										
										
											2013-05-01 12:54:04 -04:00
										 |  |  | 			client: redisServer, | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 			ttl: 60*60*24*14 | 
					
						
							|  |  |  | 		}), | 
					
						
							| 
									
										
										
										
											2013-05-01 16:27:57 -04:00
										 |  |  | 		secret: config.secret, | 
					
						
							| 
									
										
										
										
											2013-04-25 11:15:03 -04:00
										 |  |  | 		key: 'express.sid' | 
					
						
							|  |  |  | 	})); | 
					
						
							| 
									
										
										
										
											2013-05-01 21:03:37 -04:00
										 |  |  | 	app.use(passport.initialize()); | 
					
						
							|  |  |  | 	app.use(passport.session()); | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 	app.use(function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2013-04-28 13:58:19 -04:00
										 |  |  | 		// Don't bother with session handling for API requests
 | 
					
						
							| 
									
										
										
										
											2013-04-28 22:26:27 -04:00
										 |  |  | 		if (/^\/api\//.test(req.url)) return next(); | 
					
						
							| 
									
										
										
										
											2013-04-28 21:15:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 21:03:37 -04:00
										 |  |  | 		if (req.user && req.user.uid) { | 
					
						
							|  |  |  | 			global.modules.user.session_ping(req.sessionID, req.user.uid); | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-28 13:58:19 -04:00
										 |  |  | 		// (Re-)register the session as active
 | 
					
						
							|  |  |  | 		global.modules.user.active.register(req.sessionID); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 		next(); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-04-24 16:42:12 -04:00
										 |  |  | 	// Dunno wtf this does
 | 
					
						
							|  |  |  | 	//	app.use(express.logger({ format: '\x1b[1m:method\x1b[0m \x1b[33m:url\x1b[0m :response-time ms' }));
 | 
					
						
							|  |  |  | 	// Useful if you want to use app.put and app.delete (instead of app.post all the time)
 | 
					
						
							|  |  |  | 	//	app.use(express.methodOverride());
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 19:13:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-02 09:13:09 -04:00
										 |  |  | 	app.get('/403', function(req, res) { | 
					
						
							|  |  |  | 		res.send(templates['header'] + templates['403'] + templates['footer']); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 22:19:54 +00:00
										 |  |  | 	// Basic Routes (entirely client-side parsed, goal is to move the rest of the crap in this file into this one section)
 | 
					
						
							|  |  |  | 	(function() { | 
					
						
							|  |  |  | 		var routes = ['', 'login', 'register']; | 
					
						
							| 
									
										
										
										
											2013-04-25 21:55:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 22:19:54 +00:00
										 |  |  | 		for (var i=0, ii=routes.length; i<ii; i++) { | 
					
						
							|  |  |  | 			(function(route) { | 
					
						
							|  |  |  | 				app.get('/' + route, function(req, res) { | 
					
						
							|  |  |  | 					res.send(templates['header'] + '<script>templates.ready(function(){ajaxify.go("' + route + '");});</script>' + templates['footer']); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}(routes[i])); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}()); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-04-25 21:55:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 21:26:47 +00:00
										 |  |  | 	function generate_topic_body(req, res) { | 
					
						
							| 
									
										
										
										
											2013-04-25 21:55:11 +00:00
										 |  |  | 		global.modules.topics.generate_topic_body(function(topic_body) { | 
					
						
							|  |  |  | 			res.send(templates['header'] + topic_body + templates['footer']); | 
					
						
							| 
									
										
										
										
											2013-05-01 21:26:47 +00:00
										 |  |  | 		}, req.params.topic_id); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	app.get('/topic/:topic_id', generate_topic_body); | 
					
						
							|  |  |  | 	app.get('/topic/:topic_id*', generate_topic_body); | 
					
						
							| 
									
										
										
										
											2013-04-25 21:55:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-28 20:07:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 21:26:47 +00:00
										 |  |  | 	function api_method(req, res) { | 
					
						
							| 
									
										
										
										
											2013-04-25 19:13:23 +00:00
										 |  |  | 		switch(req.params.method) { | 
					
						
							|  |  |  | 			case 'home' : | 
					
						
							|  |  |  | 					global.modules.topics.get(function(data) { | 
					
						
							|  |  |  | 						res.send(JSON.stringify(data)); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2013-05-01 21:26:47 +00:00
										 |  |  | 			case 'topic' : | 
					
						
							|  |  |  | 					global.modules.posts.get(function(data) { | 
					
						
							|  |  |  | 						res.send(JSON.stringify(data)); | 
					
						
							|  |  |  | 					}, req.params.id); | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2013-04-25 19:13:23 +00:00
										 |  |  | 			default : | 
					
						
							|  |  |  | 				res.send('{}'); | 
					
						
							| 
									
										
										
										
											2013-04-28 13:28:20 -04:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2013-04-25 19:13:23 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-05-01 21:26:47 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	app.get('/api/:method', api_method); | 
					
						
							|  |  |  | 	app.get('/api/:method/:id', api_method); | 
					
						
							|  |  |  | 	app.get('/api/:method/:id*', api_method); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 21:03:37 -04:00
										 |  |  | 	app.post('/login', passport.authenticate('local', { | 
					
						
							|  |  |  | 		successRedirect: '/', | 
					
						
							|  |  |  | 		failureRedirect: '/login' | 
					
						
							|  |  |  | 	})); | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	app.get('/logout', function(req, res) { | 
					
						
							|  |  |  | 		console.log('info: [Auth] Session ' + res.sessionID + ' logout (uid: ' + global.uid + ')'); | 
					
						
							| 
									
										
										
										
											2013-04-28 13:28:20 -04:00
										 |  |  | 		global.modules.user.logout(req.sessionID, function(logout) { | 
					
						
							| 
									
										
										
										
											2013-05-01 21:03:37 -04:00
										 |  |  | 			req.logout(); | 
					
						
							|  |  |  | 			res.send(templates['header'] + templates['logout'] + templates['footer']); | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-04-22 19:01:45 +00:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-02 12:13:06 -04:00
										 |  |  | 	if (login_strategies.indexOf('twitter') !== -1) { | 
					
						
							|  |  |  | 		app.get('/auth/twitter', passport.authenticate('twitter')); | 
					
						
							| 
									
										
										
										
											2013-05-02 11:11:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-02 12:13:06 -04:00
										 |  |  | 		app.get('/auth/twitter/callback', passport.authenticate('twitter', { | 
					
						
							|  |  |  | 			successRedirect: '/', | 
					
						
							|  |  |  | 			failureRedirect: '/login' | 
					
						
							|  |  |  | 		})); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-02 11:11:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-23 16:18:43 -04:00
										 |  |  | 	app.get('/reset/:code', function(req, res) { | 
					
						
							| 
									
										
										
										
											2013-04-23 20:25:01 -04:00
										 |  |  | 		res.send(templates['header'] + templates['reset_code'].parse({ reset_code: req.params.code }) + templates['footer']); | 
					
						
							| 
									
										
										
										
											2013-04-23 16:18:43 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-22 17:31:51 -04:00
										 |  |  | 	app.get('/reset', function(req, res) { | 
					
						
							|  |  |  | 		res.send(templates['header'] + templates['reset'] + templates['footer']); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	app.get('/register', function(req, res) { | 
					
						
							| 
									
										
										
										
											2013-04-22 14:37:13 -04:00
										 |  |  | 		res.send(templates['header'] + templates['register'] + templates['footer']); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-02 10:15:55 -04:00
										 |  |  | 	app.post('/register', function(req, res) { | 
					
						
							|  |  |  | 		global.modules.user.create(req.body.username, req.body.password, req.body.email, function(err, uid) { | 
					
						
							|  |  |  | 			if (err === null) { | 
					
						
							|  |  |  | 				req.login({ | 
					
						
							|  |  |  | 					uid: uid | 
					
						
							|  |  |  | 				}, function() { | 
					
						
							|  |  |  | 					res.redirect('/'); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				res.redirect('/register'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-28 21:15:47 -04:00
										 |  |  | 	app.get('/account', function(req, res) { | 
					
						
							| 
									
										
										
										
											2013-04-24 16:42:12 -04:00
										 |  |  | 		refreshTemplates(); | 
					
						
							|  |  |  | 		res.send(templates['header'] + templates['account_settings'] + templates['footer']); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-02 09:13:09 -04:00
										 |  |  | 	app.get('/users', function(req, res) { | 
					
						
							|  |  |  | 		// Render user list
 | 
					
						
							|  |  |  | 		res.send('User list'); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	app.get('/users/:username', function(req, res) { | 
					
						
							|  |  |  | 		global.modules.user.get_uid_by_username(req.params.username, function(uid) { | 
					
						
							|  |  |  | 			res.send(templates['header'] + templates['user_profile'].parse({ uid: uid }) + templates['footer']); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-04-24 22:20:05 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | }(WebServer)); | 
					
						
							| 
									
										
										
										
											2013-04-22 15:17:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | server.listen(config.port); | 
					
						
							| 
									
										
										
										
											2013-04-22 15:23:02 -04:00
										 |  |  | global.server = server; |