Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deglobal #59

Merged
merged 23 commits into from Jul 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
15f74b9
A bunch of whitespace consistency tweaks.
smashwilson Jul 31, 2015
9ad28cf
Introduce a Context object.
smashwilson Jul 31, 2015
f4e6de5
Construct a Context for each content request.
smashwilson Jul 31, 2015
28be645
Use Context in ContentRoutingService.
smashwilson Jul 31, 2015
a0d7e74
Use Context in TemplateRoutingService.
smashwilson Jul 31, 2015
635023d
Use Contexts in the TemplateService.
smashwilson Jul 31, 2015
2573a54
Use Context in PathService.
smashwilson Jul 31, 2015
73ee4a2
Remove ResponseHelper from rewrite handler.
smashwilson Jul 31, 2015
344108b
:hocho: RequestHelper
smashwilson Jul 31, 2015
32ab813
:hocho: ResponseHelper.
smashwilson Jul 31, 2015
4f2a6c4
Eliminate the HttpErrorHelper and its uses.
smashwilson Jul 31, 2015
b280092
Nunjucks environments are per-domain.
smashwilson Jul 31, 2015
a4bb808
Create routes for proxies for all sites.
smashwilson Jul 31, 2015
2754077
ContentFilter only accepts one argument.
smashwilson Jul 31, 2015
746b146
Pass the Context where it's supposed to go.
smashwilson Jul 31, 2015
63ce228
Whitespace
smashwilson Jul 31, 2015
ac79ebb
getContentId does need that optional arg.
smashwilson Jul 31, 2015
08f7ed3
Template location and rendering.
smashwilson Jul 31, 2015
90ed29f
Get the first test passing. Phew.
smashwilson Jul 31, 2015
6c1453e
Pass status codes through.
smashwilson Jul 31, 2015
3dd755c
Clear cached templates to avoid confusion.
smashwilson Jul 31, 2015
a16f611
I know JavaScript really
smashwilson Jul 31, 2015
03f7e0f
Properly mock the control repo.
smashwilson Jul 31, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a leftover reference to the _request property

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good catch.

};

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;
};