Skip to content

Commit

Permalink
Merge pull request #59 from smashwilson/context-object
Browse files Browse the repository at this point in the history
Deglobal
  • Loading branch information
smashwilson committed Jul 31, 2015
2 parents 35e9ffc + 03f7e0f commit 74a5e14
Show file tree
Hide file tree
Showing 19 changed files with 294 additions and 330 deletions.
38 changes: 38 additions & 0 deletions src/helpers/context.js
@@ -0,0 +1,38 @@
var
config = require('../config'),
TemplateService = require('../services/template');

function Context(req, resp) {
this.request = req;
this.response = resp;
}

Context.prototype.host = function () {
return config.presented_url_domain() || this.request.get('Host');
};

Context.prototype.protocol = function () {
return config.presented_url_proto() || this._request.protocol;
};

Context.prototype.send = function (body) {
this.response.send(body);
};

Context.prototype.handleError = function (err) {
var code = err.statusCode || 500;
if (err.statusCode && err.statusCode.toString() === "404") {
code = 404;
}

TemplateService.render(this, code.toString(), {}, function (err, responseBody) {
if (err) {
console.error("I couldn't render an error template. I'm freaking out!", err);
responseBody = "Er, I was going to render an error template, but I couldn't.";
}

this.response.status(code).send(responseBody);
}.bind(this));
};

module.exports = Context;
44 changes: 0 additions & 44 deletions src/helpers/http-error.js

This file was deleted.

19 changes: 0 additions & 19 deletions src/helpers/request.js

This file was deleted.

41 changes: 0 additions & 41 deletions src/helpers/response.js

This file was deleted.

101 changes: 58 additions & 43 deletions src/routers/content.js
@@ -1,37 +1,31 @@
// Handler to assemble a specific piece of static content.

var
fs = require('fs'),
path = require('path'),
request = require('request'),
url = require('url'),
urljoin = require('url-join'),
async = require('async'),
handlebars = require('handlebars'),
_ = require('lodash'),
config = require('../config'),
logger = require('../server/logging').logger,
TemplateService = require('../services/template'),
TemplateRoutingService = require('../services/template-routing'),
ContentService = require('../services/content'),
ContentRoutingService = require('../services/content/routing'),
ContentFilterService = require('../services/content/filter'),
UrlService = require('../services/url'),
HttpErrorHelper = require('../helpers/http-error');

var handleError = function (error) {
logger.error(error);

if(error.statusCode && error.statusCode.toString() === '404') {
return HttpErrorHelper.emit(error.statusCode.toString(), error);
}

return HttpErrorHelper.emit('500', error);
};
fs = require('fs'),
path = require('path'),
request = require('request'),
url = require('url'),
urljoin = require('url-join'),
async = require('async'),
handlebars = require('handlebars'),
_ = require('lodash'),
config = require('../config'),
logger = require('../server/logging').logger,
Context = require('../helpers/context'),
TemplateService = require('../services/template'),
TemplateRoutingService = require('../services/template-routing'),
ContentService = require('../services/content'),
ContentRoutingService = require('../services/content/routing'),
ContentFilterService = require('../services/content/filter'),
UrlService = require('../services/url');

// Register content filters.

ContentFilterService.add(function (content, next) {
ContentFilterService.add(function (input, next) {
var
context = input.context,
content = input.content;

// Match nunjucks-like "{{ to('') }}" directives that are used to defer rendering of presented URLs
// until presenter-time.
var urlDirectiveRx = /\{\{\s*to\('([^']+)'\)\s*\}\}/g;
Expand All @@ -41,32 +35,38 @@ ContentFilterService.add(function (content, next) {
content.envelope.body = content.envelope.body.replace(
urlDirectiveRx,
function (match, contentID) {
return ContentRoutingService.getPresentedUrl(contentID);
return ContentRoutingService.getPresentedUrl(context, contentID);
}
);
}

return next();
});

ContentFilterService.add(function (content, next) {
ContentFilterService.add(function (input, next) {
var
context = input.context,
content = input.content;

// Locate the URLs for the content IDs of any next and previous links included in the
// document.
if (content.next && content.next.contentID && ! content.next.url) {
content.next.url = ContentRoutingService.getPresentedUrl(content.next.contentID);
content.next.url = ContentRoutingService.getPresentedUrl(context, content.next.contentID);
}

if (content.previous && content.previous.contentID && ! content.previous.url) {
content.previous.url = ContentRoutingService.getPresentedUrl(content.previous.contentID);
content.previous.url = ContentRoutingService.getPresentedUrl(context, content.previous.contentID);
}

return next();
});

module.exports = function (req, res) {
var contentId = ContentRoutingService.getContentId();
var prefix = ContentRoutingService.getContentPrefix();
var tocId = ContentRoutingService.getContentId(
var context = new Context(req, res);

var contentId = ContentRoutingService.getContentId(context);
var prefix = ContentRoutingService.getContentPrefix(context);
var tocId = ContentRoutingService.getContentId(context,
UrlService.getSitePath(prefix + '_toc')
);

Expand All @@ -76,7 +76,7 @@ module.exports = function (req, res) {
},
toc: function (callback) {
ContentService.get(tocId, {ignoreErrors: true}, function (err, toc) {
if(!toc) {
if (!toc) {
return callback(null, null);
}

Expand All @@ -91,21 +91,36 @@ module.exports = function (req, res) {
});
},
}, function (err, output) {
if(err) {
return handleError(err);
if (err) {
return context.handleError(err);
}
if(output.toc) {

if (output.toc) {
output.content.globals = {
toc: output.toc
};
}

ContentFilterService.filter(output.content, function (error, filteredContent) {
if(error) {
return HttpErrorHelper.emit('500');
var input = {
context: context,
content: output.content
};

ContentFilterService.filter(input, function (err, filterResult) {
if (err) {
return context.handleError(err);
}

TemplateService.render(TemplateRoutingService.getRoute(), filteredContent);
var route = TemplateRoutingService.getRoute(context);
var filteredContent = filterResult.content;

TemplateService.render(context, route, filteredContent, function (err, renderedContent) {
if (err) {
return context.handleError(err);
}

context.send(renderedContent);
});
});
});
};
4 changes: 1 addition & 3 deletions src/routers/index.js
Expand Up @@ -3,12 +3,10 @@
var version = require('./version');
var content = require('./content');
var crash = require('./crash');
var RequestHelper = require('../helpers/request');
var ResponseHelper = require('../helpers/response');
var config = require('../config');

exports.install = function (app) {
if(config.presenter_diagnostics()) {
if (config.presenter_diagnostics()) {
app.get('/crash', crash);
}

Expand Down
39 changes: 15 additions & 24 deletions src/server/index.js
Expand Up @@ -3,34 +3,25 @@
*/

var
express = require('express'),
logging = require('./logging'),
proxies = require('./proxies'),
rewrites = require('./rewrites'),
routes = require('../routers'),
path = require('path'),
pathService = require('../services/path'),
RequestHelper = require('../helpers/request'),
ResponseHelper = require('../helpers/response');
express = require('express'),
logging = require('./logging'),
proxies = require('./proxies'),
rewrites = require('./rewrites'),
routes = require('../routers'),
path = require('path'),
pathService = require('../services/path');

exports.create = function () {
var app = express();
var app = express();

var staticPath = path.resolve(pathService.getControlRepoPath(), 'assets');
app.use('/assets', express.static(staticPath));
var staticPath = path.resolve(pathService.getControlRepoPath(), 'assets');
app.use('/assets', express.static(staticPath));

app.use(function (req, res, next) {
RequestHelper.request = req;
ResponseHelper.response = res;
app.use(logging.requestLogger());

next();
});
proxies(app);
rewrites(app);
routes.install(app);

app.use(logging.requestLogger());

proxies(app);
rewrites(app);
routes.install(app);

return app;
return app;
};

0 comments on commit 74a5e14

Please sign in to comment.