diff --git a/blogode.js b/blogode.js index 00f7b5a..6f5aac1 100644 --- a/blogode.js +++ b/blogode.js @@ -5,7 +5,10 @@ var faye = require('faye'); var posts = require('./lib/posts'); var users = require('./lib/users'); var comments = require('./lib/comments'); -var config = require('./lib/config'); +var config = require('./lib/config') + , homeController = require('./controllers/home') + , adminController = require('./controllers/admin') + , adminFilter = require('./filters/admin'); var app = express.createServer(); @@ -70,136 +73,8 @@ bayeux = new faye.NodeAdapter({ timeout: 45 }); -app.get("/", function(req, res){ - // return posts list - - posts.getPosts(10, function (posts){ - res.render('posts/index', { - locals: { 'posts': posts } - }); - }); -}); - -app.get("/feed", function(req, res){ - // return posts in XML format - - posts.getPosts(10, function (postsResult){ - posts.generatePostsXML(postsResult, function(xmlString) { - return res.send(xmlString); - }); - }); -}); - -function adminLoginFilter(req, res, next) { - // verifies if user is an admin - - if(!req.session.username) { - return res.redirect("/admin/login"); - } - next(); -} - -app.get("/admin", adminLoginFilter, function(req, res){ - // return admin panel - - res.render('admin/panel', { - layout: false - }); -}); - - -app.get("/admin/login", function(req, res){ - // return admin login page - - if(req.session.username) { - return res.redirect("/admin") - } - - res.render('admin/login', { - layout: false - }); -}); - -app.post("/admin/authenticate", function(req, res){ - // verifies admin credentials - - if(!req.param('username') || !req.param('password')) { - res.redirect('/admin/login') - } - - users.verifyCredentials(req.param('username'), req.param('password'), function(isValidUser, userId){ - if(isValidUser) { - req.session.username = req.param('username'); - req.session.user_id = userId; - } - res.redirect("/admin"); - }); -}); - -app.get('/admin/posts', adminLoginFilter, function(req, res) { - // return the list of posts (as admin) - - posts.getPosts(0, function (posts){ - res.render('admin/posts/index', { - layout: false, - locals: { 'posts': posts } - }); - }); -}); -app.get('/admin/posts/new', adminLoginFilter, function(req, res) { - // return the formulary to create a new post - - res.render('admin/posts/new', { - layout: false - }); -}); - -app.get('/admin/posts/:id', adminLoginFilter, function(req, res) { - // return a post (to edit) - - posts.getPost(req.param('id'), function (post){ - res.render('admin/posts/edit', { - layout: false, - locals: { 'post': post } - }); - }); -}); - -app.post('/admin/posts/save', adminLoginFilter, function(req, res) { - // saves a post - - if(!req.param('title') || !req.param('body')) { - return res.redirect("/admin/posts/new"); - } - posts.createPost(req.param('title'), req.param('body'), req.session.user_id, function(postId) { - return res.redirect('/admin/posts/' + postId); - }); -}); - -app.put('/admin/posts/:id', adminLoginFilter, function(req, res) { - // updates a post - - if(!req.param('title') || !req.param('body')) { - return res.redirect("/admin/posts/new"); - } - posts.updatePost(req.param('id'), req.param('title'), req.param('body'), function() { - return res.redirect('/admin/posts/' + req.param('id')); - }); -}); - -app.get('/admin/posts/destroy/:id', adminLoginFilter, function(req, res) { - // destroys a post - - if(!req.param('id')) { - return res.redirect("/admin/posts/"); - } - posts.destroyPost(req.param('id'), function () { - return res.redirect('/admin/posts/') - }); -}); - -app.get('/admin/template', adminLoginFilter, function(req, res) { +app.get('/admin/template', adminFilter.verifyLogin, function(req, res) { // returns the template file editor config.getBlogConfigKeyValue('current_template', function(value) { @@ -227,7 +102,7 @@ app.get('/admin/template', adminLoginFilter, function(req, res) { }) }); -app.get('/admin/template/get_file_content', adminLoginFilter, function(req, res) { +app.get('/admin/template/get_file_content', adminFilter.verifyLogin, function(req, res) { // returns a template file content var fileToRead = "" @@ -250,7 +125,7 @@ app.get('/admin/template/get_file_content', adminLoginFilter, function(req, res) }); -app.put('/admin/template/set_file_content', adminLoginFilter, function(req, res) { +app.put('/admin/template/set_file_content', adminFilter.verifyLogin, function(req, res) { // sets a template file some content if(req.param('content') == '' || req.param('content') == undefined) { @@ -289,7 +164,7 @@ app.put('/admin/template/set_file_content', adminLoginFilter, function(req, res) }); -app.post('/admin/template/apply_template', adminLoginFilter, function(req, res) { +app.post('/admin/template/apply_template', adminFilter.verifyLogin, function(req, res) { // apply a template as the current template if(req.param('name') == '' || req.param('name') == undefined) { @@ -333,34 +208,22 @@ app.post('/admin/template/apply_template', adminLoginFilter, function(req, res) }); }); -app.get("/search", function(req, res){ - // performs a search for a post - - if(!req.param('keywords')) { - res.render('posts/search', { - locals: { 'posts': undefined } - }); - } - - posts.searchForPosts(req.param('keywords'), function(searchResults){ - res.render('posts/search', { - locals: { 'posts': searchResults } - }); - }); - -}); +//Admin Routes +app.get("/admin", adminFilter.verifyLogin, adminController.index); +app.get("/admin/login", adminController.login); +app.post("/admin/authenticate", adminController.authenticate); +app.get('/admin/posts', adminFilter.verifyLogin, adminController.posts); +app.get('/admin/posts/new', adminFilter.verifyLogin, adminController.newPost); +app.get('/admin/posts/:id', adminFilter.verifyLogin, adminController.showPost); +app.post('/admin/posts/save', adminFilter.verifyLogin, adminController.createPost); +app.put('/admin/posts/:id', adminFilter.verifyLogin, adminController.updatePost); +app.get('/admin/posts/destroy/:id', adminFilter.verifyLogin, adminController.destroyPost); -app.get("/:id", function(req, res){ - // return an specific post (by ID) - - posts.getPost(req.param('id'), function(post) { - comments.getCommentsOfPost(req.param('id'), function(comments){ - res.render('posts/show', { - locals: { 'post': post, 'comments': comments } - }); - }); - }); -}); +//Home routes +app.get("/", homeController.index); +app.get("/feed", homeController.feed); +app.get("/search", homeController.search); +app.get("/:id", homeController.show); app.post("/:id/comments/save", function(req, res){ // saves a comment (for a post) diff --git a/controllers/admin.js b/controllers/admin.js new file mode 100644 index 0000000..a630168 --- /dev/null +++ b/controllers/admin.js @@ -0,0 +1,103 @@ +var sys = require("sys") + , users = require('../lib/users') + , posts = require('../lib/posts'); + +exports.index = function(req, res){ + // return admin panel + + res.render('admin/panel', { + layout: false + }); +}; + +exports.login = function(req, res){ + // return admin login page + + if(req.session.username) { + return res.redirect("/admin"); + } + + res.render('admin/login', { + layout: false + }); +}; + +exports.authenticate = function(req, res){ + // verifies admin credentials + + if(!req.param('username') || !req.param('password')) { + res.redirect('/admin/login') + } + + users.verifyCredentials(req.param('username'), req.param('password'), function(isValidUser, userId){ + if(isValidUser) { + req.session.username = req.param('username'); + req.session.user_id = userId; + } + res.redirect("/admin"); + }); +}; + + +exports.posts = function(req, res) { + // return the list of posts (as admin) + + posts.getPosts(0, function (posts){ + res.render('admin/posts/index', { + layout: false, + locals: { 'posts': posts } + }); + }); +}; + +exports.newPost = function(req, res) { + // return the formulary to create a new post + + res.render('admin/posts/new', { + layout: false + }); +}; + +exports.showPost = function(req, res) { + // return a post (to edit) + + posts.getPost(req.param('id'), function (post){ + res.render('admin/posts/edit', { + layout: false, + locals: { 'post': post } + }); + }); +}; + +exports.createPost = function(req, res) { + // saves a post + + if(!req.param('title') || !req.param('body')) { + return res.redirect("/admin/posts/new"); + } + posts.createPost(req.param('title'), req.param('body'), req.session.user_id, function(postId) { + return res.redirect('/admin/posts/' + postId); + }); +}; + +exports.updatePost = function(req, res) { + // updates a post + + if(!req.param('title') || !req.param('body')) { + return res.redirect("/admin/posts/new"); + } + posts.updatePost(req.param('id'), req.param('title'), req.param('body'), function() { + return res.redirect('/admin/posts/' + req.param('id')); + }); +}; + +exports.destroyPost = function(req, res) { + // destroys a post + + if(!req.param('id')) { + return res.redirect("/admin/posts/"); + } + posts.destroyPost(req.param('id'), function () { + return res.redirect('/admin/posts/') + }); +}; diff --git a/controllers/home.js b/controllers/home.js new file mode 100644 index 0000000..f3d283b --- /dev/null +++ b/controllers/home.js @@ -0,0 +1,48 @@ +var posts = require('../lib/posts') + , comments = require('../lib/comments'); + + +exports.index = function(req, res){ + // return posts list + posts.getPosts(10, function (posts){ + res.render('posts/index', { + locals: { 'posts': posts } + }); + }); +}; + +exports.feed = function(req, res){ + // return posts in XML format + posts.getPosts(10, function (postsResult){ + posts.generatePostsXML(postsResult, function(xmlString) { + return res.send(xmlString); + }); + }); +}; + +exports.search = function(req, res){ + // performs a search for a post + + if(!req.param('keywords')) { + res.render('posts/search', { + locals: { 'posts': undefined } + }); + } + + posts.searchForPosts(req.param('keywords'), function(searchResults){ + res.render('posts/search', { + locals: { 'posts': searchResults } + }); + }); +}; + +exports.show = function(req, res){ + // return an specific post (by ID) + posts.getPost(req.param('id'), function(post) { + comments.getCommentsOfPost(req.param('id'), function(comments){ + res.render('posts/show', { + locals: { 'post': post, 'comments': comments } + }); + }); + }); +}; \ No newline at end of file diff --git a/filters/admin.js b/filters/admin.js new file mode 100644 index 0000000..ab78cf3 --- /dev/null +++ b/filters/admin.js @@ -0,0 +1,8 @@ +exports.verifyLogin = function (req, res, next) { + // verifies if user is an admin + + if(!req.session.username) { + return res.redirect("/admin/login"); + } + next(); +}; \ No newline at end of file