groupRoute: function() { var blog = this.blog; var router = _.bind(function(req, res) { var group = req.params.group; if (group == "drafts") { blog.checkPassword(req.cookies.token||'', _.bind(function(err, passed) { if (passed) { res.doNotCache = true; this._renderPosts('posts', req, res, _.bind(function(cb) { blog.getPostsByGroup(group, true, cb); }, this)); } else { this._renderError(401, "Not authorized", req, res); } }, this)); } else { this._renderPosts('posts', req, res, _.bind(function(cb) { blog.getPostsByGroup(group, true, cb); }, this)); } }, this); if (blog.cache) { return [cacheware(blog.cache), router]; } else { return router; } },
apiRoute: function() { var apiRoute = this.api.route(); if (this.cache) { return [cacheware(this.cache), apiRoute]; } else { return apiRoute; } },
rssRoute: function(numberOfPosts) { var rssRoute = syndicate.route(this, numberOfPosts); if (this.cache) { return [cacheware(this.cache), rssRoute]; } else { return rssRoute; } },
contentRoute: function() { var blog = this.blog; var contentRoute = _.bind(function(req, res) { res.sendSafely(_.bind(function(cb) { var urlPath = req.params[0].split('/'); if (urlPath.length > 1 && urlPath[0] == imagesDirName) { var imageFileName = urlPath[1]; // var imageSize = urlPath[2]; // var imagePath = path.join(blog.contentPaths[0].path, imagesDirName, // imageFileName); var imageSize = null;//urlPath[2]; var imagePath = path.join(blog.contentPaths[0].path, imagesDirName, urlPath.slice(1).join('/')); if (!imageSize) { cb(0, {path: imagePath}); } else { var temp = require('temp'); var tempPath = temp.path({suffix: path.extname(imagePath)}); var options = { srcPath: imagePath, dstPath: tempPath }; var m = /^\s*(\d*)x(\d*)\s*$/.exec(imageSize); if (m) { if (m[1]) { options.width = parseInt(m[1]); } if (m[2]) { options.height = parseInt(m[2]); } } if (options.width && options.height) { options.height += '\!'; } var magick = require('imagemagick'); magick.resize(options, function(err) { if (err) { console.error(err); cb({error: 500, body: 'Error processing image'}); } else { cb(0, {path: tempPath}); } }); } } else { cb({error: 404, body: 'Not found'}); } }, blog)); }, blog); if (blog.cache) { return [cacheware(blog.cache), contentRoute]; } else { return contentRoute; } },
apiRoute: function() { var blog = this.blog; var apiRoute = blog.api.route(); if (blog.cache) { return [cacheware(blog.cache), apiRoute]; } else { return apiRoute; } },
rssRoute: function(numberOfPosts) { var blog = this.blog; var rssRoute = syndicate.route(blog, numberOfPosts); if (blog.cache) { return [cacheware(blog.cache), rssRoute]; } else { return rssRoute; } },
staticRoute: function() { function handler(req, res, next) { var filePath; if (!req.appjsMatch) { filePath = 'index.html'; } else { filePath = req.appjsParts.slice(1).join('/'); } filePath = path.join(this.repoPath, filePath); fs.stat(filePath, abind(function(err, stat) { var dependencies = [{path: filePath, mtime: stat.mtime.getTime()}]; fs.readFile(filePath, abind(function(err, source) { send.call(this, {path: filePath, body: source, error: err}, !!err); }, next, this)); }, next, this)); function send(result, isError) { var mimeType = result.mimeType || (result.path ? mime.lookup(result.path) || defaultMimeType : htmlMimeType); if (isTextMimeType(mimeType)) { mimeType += '; charset=UTF-8' } res.header('Content-Type', mimeType); if (!isError) { res.header('ETag', this.appVersion); // if (result.permanent || isCDN(req)) { // res.header('Cache-Control', 'public, max-age=31536000'); // } else { res.header('Cache-Control', 'public, max-age=0'); // } } res.send(result.body, result.error || 200); } } if (this.cache) { this.cache.on('unmonitor', _.bind(function(URL) { this.invalidate(URL); }, this)); } var cacheMiddleware = this.cache ? cacheware(this.cache) : noware; return [prepware(this), cacheMiddleware, _.bind(handler, this)]; },
archiveRoute: function() { var blog = this.blog; var router = _.bind(function(req, res) { this._renderPosts('archive', req, res, _.bind(function(cb) { blog.getAllPosts(true, cb); }, this)); }, this); if (blog.cache) { return [cacheware(blog.cache), router]; } else { return router; } },
homeRoute: function() { var blog = this.blog; var router = _.bind(function(req, res) { this._renderPosts('posts', req, res, _.bind(function(cb) { blog.getPostsByPage(0, blog.postsPerPage || 1, true, cb); }, this)); }, this); if (blog.cache) { return [cacheware(blog.cache), router]; } else { return router; } },
postRoute: function() { var blog = this.blog; var router = _.bind(function(req, res) { var year = req.params.year; var month = req.params.month; var day = req.params.day; var slug = req.params.slug; this._renderPosts('posts', req, res, _.bind(function(cb) { blog.getPost(slug, year, month, day, true, cb); }, this)); }, this); if (blog.cache) { return [cacheware(blog.cache), router]; } else { return router; } },
route: function() { function handler(req, res, next) { var urlOptions = readOptions(req.query, this); if (isCrawler(req)) { urlOptions.content = 'inline'; } urlOptions.userAgent = req.header('user-agent'); // Note that the URL matching is done in the prepware middleware function this._loadURL(req.urlParsed, req.appjsParts, req.appjsMatch, this.renderer, urlOptions, function(err, result) { if (err) { dandy.logException(err); send(err, true); } else { send(result); } }); function send(result, isError) { if (result.dependencies) { res.dependencies = _.pluck(result.dependencies, 'path'); } var mimeType = result.mimeType || (result.path ? mime.lookup(result.path) || defaultMimeType : htmlMimeType); if (isTextMimeType(mimeType)) { mimeType += '; charset=UTF-8' } res.header('Content-Type', mimeType); if (!isError) { var latestTime = findLatestMtime(result.dependencies || []); if (latestTime) { res.header('ETag', latestTime); } // if (result.permanent || isCDN(req)) { // res.header('Cache-Control', 'public, max-age=31536000'); // } else { res.header('Cache-Control', 'public, max-age=0'); // } } res.send(result.body, result.error || 200); } } if (this.cache) { this.cache.on('unmonitor', _.bind(function(URL) { this.invalidate(URL); }, this)); } if (this.socket) { this._listenToSocket(); } var cacheMiddleware = this.cache ? cacheware(this.cache) : noware; return [prepware(this), uaware(this), cacheMiddleware, _.bind(handler, this)]; },