| 
									
										
										
										
											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-25 11:15:03 -04:00
										 |  |  | 	RedisStore = require('connect-redis')(express); | 
					
						
							| 
									
										
										
										
											2013-04-22 15:17:41 -04:00
										 |  |  |     config = require('../config.js'); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-24 22:20:05 -04:00
										 |  |  | 	function requireAuth(req, res, next) { | 
					
						
							|  |  |  | 		// Include this middleware if the endpoint requires a logged in user to view
 | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 		if (!global.uid) { | 
					
						
							|  |  |  | 			res.redirect('/403'); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			next(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											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)
 | 
					
						
							|  |  |  | 	app.use(express.bodyParser());	// Puts POST vars in request.body
 | 
					
						
							|  |  |  | 	app.use(express.cookieParser());	// If you want to parse cookies (res.cookies)
 | 
					
						
							| 
									
										
										
										
											2013-04-25 11:15:03 -04:00
										 |  |  | 	app.use(express.session({ | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 		store: new RedisStore({ | 
					
						
							|  |  |  | 			ttl: 60*60*24*14 | 
					
						
							|  |  |  | 		}), | 
					
						
							| 
									
										
										
										
											2013-04-25 11:15:03 -04:00
										 |  |  | 		secret: 'nodebb', | 
					
						
							|  |  |  | 		key: 'express.sid' | 
					
						
							|  |  |  | 	})); | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 	app.use(function(req, res, next) { | 
					
						
							|  |  |  | 		if (global.uid === undefined) { | 
					
						
							|  |  |  | 			console.log('info: [Auth] First load, retrieving uid...'); | 
					
						
							|  |  |  | 			global.modules.user.get_uid_by_session(req.sessionID, function(uid) { | 
					
						
							|  |  |  | 				global.uid = uid; | 
					
						
							|  |  |  | 				if (global.uid !== null) console.log('info: [Auth] uid ' + global.uid + ' found. Welcome back.'); | 
					
						
							|  |  |  | 				else console.log('info: [Auth] No login session found.'); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			console.log('info: [Auth] Ping from uid ' + global.uid); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		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 12:59:31 -04:00
										 |  |  | 	app.get('/', function(req, res) { | 
					
						
							| 
									
										
										
										
											2013-04-24 22:24:20 +00:00
										 |  |  | 		global.modules.topics.generate_forum_body(function(forum_body) { | 
					
						
							|  |  |  | 			res.send(templates['header'] + forum_body + templates['footer']);	 | 
					
						
							| 
									
										
										
										
											2013-04-25 19:13:23 +00:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	app.get('/api/:method', function(req, res) { | 
					
						
							|  |  |  | 		switch(req.params.method) { | 
					
						
							|  |  |  | 			case 'home' : | 
					
						
							|  |  |  | 					global.modules.topics.get(function(data) { | 
					
						
							|  |  |  | 						res.send(JSON.stringify(data)); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			default : | 
					
						
							|  |  |  | 				res.send('{}'); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 	app.get('/login', function(req, res) { | 
					
						
							| 
									
										
										
										
											2013-04-22 17:31:51 -04:00
										 |  |  | 		res.send(templates['header'] + templates['login'] + templates['footer']); | 
					
						
							| 
									
										
										
										
											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 + ')'); | 
					
						
							|  |  |  | 		global.modules.user.logout(function(logout) { | 
					
						
							|  |  |  | 			if (logout === true) req.session.destroy(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		res.send(templates['header'] + templates['logout'] + templates['footer']); | 
					
						
							| 
									
										
										
										
											2013-04-22 19:01:45 +00: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-04-24 22:20:05 -04:00
										 |  |  | 	app.get('/account', requireAuth, function(req, res) { | 
					
						
							| 
									
										
										
										
											2013-04-24 16:42:12 -04:00
										 |  |  | 		refreshTemplates(); | 
					
						
							|  |  |  | 		res.send(templates['header'] + templates['account_settings'] + templates['footer']); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-24 22:20:05 -04:00
										 |  |  | 	app.get('/403', function(req, res) { | 
					
						
							| 
									
										
										
										
											2013-04-25 14:21:00 -04:00
										 |  |  | 		res.send(templates['header'] + templates['403'] + templates['footer']); | 
					
						
							| 
									
										
										
										
											2013-04-24 22:20:05 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 	module.exports.init = function() { | 
					
						
							|  |  |  | 		// todo move some of this stuff into config.json
 | 
					
						
							|  |  |  | 		app.configure(function() { | 
					
						
							|  |  |  | 			app.use(express.static(global.configuration.ROOT_DIRECTORY + '/public'));  | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }(WebServer)); | 
					
						
							| 
									
										
										
										
											2013-04-22 15:17:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | server.listen(config.port); | 
					
						
							| 
									
										
										
										
											2013-04-22 15:23:02 -04:00
										 |  |  | global.server = server; |