Files
NodeBB/src/webserver.js

321 lines
9.0 KiB
JavaScript
Raw Normal View History

2013-04-22 16:51:32 +00:00
var express = require('express'),
WebServer = express(),
server = require('http').createServer(WebServer),
RedisStore = require('connect-redis')(express),
path = require('path'),
config = require('../config.js'),
redis = require('redis'),
redisServer = redis.createClient(config.redis.port, config.redis.host, config.redis.options),
user = require('./user.js'),
utils = require('./utils.js'),
admin = require('./routes/admin.js'),
auth = require('./routes/authentication.js');
2013-05-02 15:57:43 -04:00
2013-04-22 16:51:32 +00:00
(function(app) {
var templates = global.templates;
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(require('less-middleware')({ src: path.join(__dirname, '../', '/public') }));
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)
app.use(express.compress());
app.use(express.session({
2013-04-25 12:59:31 -04:00
store: new RedisStore({
client: redisServer,
2013-04-25 12:59:31 -04:00
ttl: 60*60*24*14
}),
secret: config.secret,
key: 'express.sid'
}));
auth.initialize(app);
2013-04-25 12:59:31 -04:00
app.use(function(req, res, next) {
// Don't bother with session handling for API requests
if (/^\/api\//.test(req.url)) return next();
2013-04-28 21:15:47 -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
}
// (Re-)register the session as active
global.modules.user.active.register(req.sessionID);
2013-04-25 12:59:31 -04:00
next();
});
auth.create_routes(app);
admin.create_routes(app);
2013-04-24 16:42:12 -04:00
2013-05-02 09:13:09 -04:00
app.create_route = function(url, tpl) { // to remove
return '<script>templates.ready(function(){ajaxify.go("' + url + '", null, "' + tpl + '");});</script>';
};
// 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', 'account', 'latest', 'popular', 'active', '403'];
for (var i=0, ii=routes.length; i<ii; i++) {
(function(route) {
app.get('/' + route, function(req, res) {
if ((route === 'login' || route ==='register') && (req.user && req.user.uid > 0)) {
res.redirect('/account');
return;
}
res.send(templates['header'] + app.create_route(route) + templates['footer']);
});
}(routes[i]));
}
}());
// Complex Routes
app.get('/topic/:topic_id/:slug?', function(req, res) {
2013-05-07 19:22:38 +00:00
var topic_url = req.params.topic_id + (req.params.slug ? '/' + req.params.slug : '');
res.send(templates['header'] + '<script>templates.ready(function(){ajaxify.go("topic/' + topic_url + '");});</script>' + templates['footer']);
});
app.get('/category/:category_id/:slug?', function(req, res) {
2013-05-07 19:22:38 +00:00
var category_url = req.params.category_id + (req.params.slug ? '/' + req.params.slug : '');
res.send(templates['header'] + '<script>templates.ready(function(){ajaxify.go("category/' + category_url + '");});</script>' + templates['footer']);
});
app.get('/confirm/:code', function(req, res) {
2013-05-07 19:22:38 +00:00
res.send(templates['header'] + '<script>templates.ready(function(){ajaxify.go("confirm/' + req.params.code + '");});</script>' + templates['footer']);
});
// These functions are called via ajax once the initial page is loaded to populate templates with data
function api_method(req, res) {
switch(req.params.method) {
case 'home' :
global.modules.categories.get(function(data) {
res.send(JSON.stringify(data));
});
break;
case 'login' :
var data = {},
login_strategies = auth.get_login_strategies(),
num_strategies = login_strategies.length;
if (num_strategies == 0) {
data = {
'login_window:spansize': 'span12',
'alternate_logins:display': 'none'
};
} else {
data = {
'login_window:spansize': 'span6',
'alternate_logins:display': 'block'
}
for (var i=0, ii=num_strategies; i<ii; i++) {
data[login_strategies[i] + ':display'] = 'active';
}
}
res.send(JSON.stringify(data));
break;
case 'topic' :
global.modules.posts.get(function(data) {
res.send(JSON.stringify(data));
}, req.params.id, (req.user) ? req.user.uid : 0);
break;
case 'category' :
global.modules.topics.get(function(data) {
res.send(JSON.stringify(data));
}, req.params.id);
break;
case 'latest' :
global.modules.topics.get(function(data) {
res.send(JSON.stringify(data));
});
break;
case 'popular' :
global.modules.topics.get(function(data) {
res.send(JSON.stringify(data));
});
break;
case 'active' :
global.modules.topics.get(function(data) {
res.send(JSON.stringify(data));
});
break;
case 'users' :
2013-05-07 19:18:13 -04:00
if (!req.params.section && !req.params.id) {
get_users_fn(req, res, function(userData) {
res.send(JSON.stringify(userData));
});
}
else if (String(req.params.section).toLowerCase() === 'edit') {
get_account_fn(req, res, function(userData) {
res.send(JSON.stringify(userData));
2013-05-08 14:49:33 -04:00
});
2013-05-07 21:17:22 +00:00
} else {
get_account_fn(req, res, function(userData) {
res.send(JSON.stringify(userData));
});
}
2013-05-07 19:18:13 -04:00
break;
case 'confirm':
global.modules.user.email.confirm(req.params.id, function(data) {
if (data.status === 'ok') {
res.send(JSON.stringify({
'alert-class': 'alert-success',
title: 'Email Confirmed',
text: 'Thank you for vaidating your email. Your account is now fully activated.'
}));
} else {
res.send(JSON.stringify({
'alert-class': 'alert-error',
title: 'An error occurred...',
text: 'There was a problem validating your email address. Perhaps the code was invalid or has expired.'
}));
}
});
break;
default :
res.send('{}');
2013-04-28 13:28:20 -04:00
break;
}
}
2013-05-07 21:17:22 +00:00
app.get('/api/:method', api_method);
app.get('/api/:method/:id', api_method);
2013-05-07 21:17:22 +00:00
// ok fine MUST ADD RECURSION style. I'll look for a better fix in future but unblocking baris for this:
app.get('/api/:method/:id/:section?', api_method);
app.get('/api/:method/:id*', api_method);
2013-05-07 21:17:22 +00:00
2013-04-22 16:51:32 +00:00
// TODO move user related logic into another file vvvvvvvvvvvvvvvvvvvv
2013-05-08 13:43:56 -04:00
app.post('/edituser', function(req, res){
2013-05-08 16:00:37 -04:00
2013-05-08 14:49:33 -04:00
if(!req.user)
return res.redirect('/403');
2013-05-08 14:54:07 -04:00
2013-05-08 16:00:37 -04:00
if(req.user.uid != req.body.uid)
2013-05-08 14:49:33 -04:00
return res.redirect('/');
2013-05-08 13:43:56 -04:00
user.updateUserFields(req.user.uid, req.body);
res.redirect('/');
});
//to baris, move this into account.js or sth later - just moved this out here for you to utilize client side tpl parsing
//I didn't want to change too much so you should probably sort out the params etc
function get_account_fn(req, res, callback) {
var username = req.params.id;
user.get_uid_by_username(username, function(uid) {
2013-05-08 11:48:21 -04:00
user.getUserData(uid, function(data) {
if(data)
{
data.joindate = utils.relativeTime(data.joindate);
2013-05-08 13:43:56 -04:00
data.age = new Date().getFullYear() - new Date(data.birthday).getFullYear();;
2013-05-08 11:48:21 -04:00
data.uid = uid;
2013-05-08 14:49:33 -04:00
callback({
yourid: (req.user)?req.user.uid : 0,
theirid: uid,
user: data
2013-05-08 14:49:33 -04:00
});
}
else
callback({user:{}});
});
});
2013-05-07 19:18:13 -04:00
}
function get_users_fn(req, res, callback) {
user.getUserList(function(data){
callback({users:data});
});
}
2013-05-05 18:40:04 -04:00
app.get('/uid/:uid', function(req, res) {
if(!req.params.uid)
return res.redirect('/403');
2013-05-05 18:40:04 -04:00
user.getUserData(req.params.uid, function(data){
2013-05-05 19:35:17 -04:00
if(data)
res.send(data);
else
res.send("User doesn't exist!");
2013-05-05 18:40:04 -04:00
});
2013-04-24 16:42:12 -04:00
});
2013-05-02 09:13:09 -04:00
app.get('/users', function(req, res) {
2013-05-07 19:18:13 -04:00
2013-05-07 17:16:57 -04:00
user.getUserList(function(data){
2013-05-07 19:18:13 -04:00
res.send(templates['header'] + app.create_route("users", "users") + templates['footer']);
2013-05-07 17:16:57 -04:00
});
2013-05-02 09:13:09 -04:00
});
2013-05-07 16:49:18 -04:00
app.get('/users/:uid/edit', function(req, res){
2013-05-07 13:55:02 -04:00
2013-05-08 14:26:29 -04:00
if(!req.user)
return res.redirect('/403');
user.getUserField(req.user.uid, 'username', function(username) {
if(req.params.uid && username === req.params.uid)
res.send(templates['header'] + app.create_route('users/'+req.params.uid+'/edit','accountedit') + templates['footer']);
2013-05-08 14:26:29 -04:00
else
return res.redirect('/403');
});
2013-05-07 13:55:02 -04:00
});
app.get('/users/:username*', handleUserProfile);
function handleUserProfile(req, res) {
if(!req.params.username) {
res.send("User doesn't exist!");
return;
}
user.get_uid_by_username(req.params.username, function(uid) {
2013-05-08 11:48:21 -04:00
if(!uid) {
res.redirect('/403');
return;
}
user.getUserData(uid, function(data) {
if(data) {
res.send(templates['header'] + app.create_route('users/'+data.username, 'account') + templates['footer']);
}
else {
2013-05-08 11:48:21 -04:00
res.redirect('/403');
}
});
2013-05-02 09:13:09 -04:00
});
}
2013-05-06 10:05:00 -04:00
// TODO move user related logic into another file ^^^^^^^^^^^^^^^^^^^^^^^
2013-04-22 16:51:32 +00:00
}(WebServer));
server.listen(config.port);
global.server = server;