From 1fa900e61541c310b73fcd32c28e6e449632792e Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 17 Jul 2013 22:35:16 -0400 Subject: [PATCH] added sitemap.xml to routes, closes #96 --- src/sitemap.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ src/webserver.js | 29 ++++++------------- 2 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 src/sitemap.js diff --git a/src/sitemap.js b/src/sitemap.js new file mode 100644 index 0000000000..44378a215d --- /dev/null +++ b/src/sitemap.js @@ -0,0 +1,73 @@ +var path = require('path'), + async = require('async'), + sm = require('sitemap'), + url = require('url'), + categories = require('./categories'), + topics = require('./topics'), + sitemap = { + getStaticUrls: function(callback) { + var relative_path = global.nconf.get('relative_path'); + + callback(null, [ + { url: relative_path, changefreq: 'weekly', priority: '0.6' }, + { url: path.join(relative_path, 'recent'), changefreq: 'daily', priority: '0.4' }, + { url: path.join(relative_path, 'users'), changefreq: 'daily', priority: '0.4' } + ]); + }, + getDynamicUrls: function(callback) { + var relative_path = global.nconf.get('relative_path'), + returnUrls = []; + + async.parallel([ + function(next) { + var categoryUrls = []; + categories.getAllCategories(function(data) { + data.categories.forEach(function(category) { + categoryUrls.push({ + url: path.join(relative_path, 'category', category.slug), + changefreq: 'weekly', + priority: '0.4' + }); + }); + + next(null, categoryUrls); + }, 0); + }, + function(next) { + var topicUrls = []; + topics.getAllTopics(null, null, function(topics) { + topics.forEach(function(topic) { + topicUrls.push({ + url: path.join(relative_path, 'topic', topic.slug), + changefreq: 'daily', + priority: '0.6' + }); + }); + + next(null, topicUrls); + }); + } + ], function(err, data) { + if (!err) { + returnUrls = returnUrls.concat(data[0]).concat(data[1]); + } + + callback(null, returnUrls); + }); + }, + render: function(callback) { + async.parallel([sitemap.getStaticUrls, sitemap.getDynamicUrls], function(err, urls) { + var urls = urls[0].concat(urls[1]), + map = sm.createSitemap({ + hostname: global.nconf.get('base_url') + (global.nconf.get('use_port') ? ':' + global.nconf.get('port') : '') + '/', + cacheTime: 600000, + urls: urls + }), + xml = map.toXML(function(xml) { + callback(xml); + }); + }); + } + } + +module.exports = sitemap; \ No newline at end of file diff --git a/src/webserver.js b/src/webserver.js index d173da5d06..513d1f7453 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -229,6 +229,14 @@ var express = require('express'), res.send(app.build_header(res) + '' + templates['footer']); }); + app.get('/sitemap.xml', function(req, res) { + var sitemap = require('./sitemap.js'); + + sitemap.render(function(xml) { + res.type('xml').set('Content-Length', xml.length).send(xml); + }); + }); + app.get('/api/:method', api_method); app.get('/api/:method/:id', api_method); // ok fine MUST ADD RECURSION style. I'll look for a better fix in future but unblocking baris for this: @@ -262,22 +270,6 @@ var express = require('express'), }); }); - app.get('/test', function(req, res) { - - /*user.get_userslugs_by_uids([1,2], function(data) { - res.send(data); - });*/ - var gravatar= require('gravatar'); - var img = gravatar.url('', {}, https=false); - res.send(img); - // 'http://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e' - - /* categories.getCategoryById(1,1, function(data) { - res.send(data); - },1);*/ - - }); - }); // These functions are called via ajax once the initial page is loaded to populate templates with data @@ -407,11 +399,6 @@ var express = require('express'), break; } } - - - - - }(WebServer)); server.listen(nconf.get('port'));