Skip to content

Commit

Permalink
🎨 source out url utils from ConfigManager (#7347)
Browse files Browse the repository at this point in the history
refs #6982
  • Loading branch information
kirrg001 authored and ErisDS committed Sep 20, 2016
1 parent dc1ef35 commit c8119ee
Show file tree
Hide file tree
Showing 39 changed files with 233 additions and 245 deletions.
3 changes: 2 additions & 1 deletion core/server/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
var _ = require('lodash'),
Promise = require('bluebird'),
config = require('../config'),
utils = require('../utils'),
configuration = require('./configuration'),
db = require('./db'),
mail = require('./mail'),
Expand Down Expand Up @@ -117,7 +118,7 @@ cacheInvalidationHeader = function cacheInvalidationHeader(req, result) {
* @return {String} Resolves to header string
*/
locationHeader = function locationHeader(req, result) {
var apiRoot = config.urlFor('api'),
var apiRoot = utils.url.urlFor('api'),
location,
newObject;

Expand Down
8 changes: 4 additions & 4 deletions core/server/apps/private-blogging/lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ privateBlogging = {
if (isVerified) {
return next();
} else {
url = config.urlFor({relativeUrl: privateRoute});
url = utils.url.urlFor({relativeUrl: privateRoute});
url += req.url === '/' ? '' : '?r=' + encodeURIComponent(req.url);
return res.redirect(url);
}
Expand All @@ -92,7 +92,7 @@ privateBlogging = {
// This is here so a call to /private/ after a session is verified will redirect to home;
isPrivateSessionAuth: function isPrivateSessionAuth(req, res, next) {
if (!res.isPrivateBlog) {
return res.redirect(config.urlFor('home', true));
return res.redirect(utils.url.urlFor('home', true));
}

var hash = req.session.token || '',
Expand All @@ -101,7 +101,7 @@ privateBlogging = {
return verifySessionHash(salt, hash).then(function then(isVerified) {
if (isVerified) {
// redirect to home if user is already authenticated
return res.redirect(config.urlFor('home', true));
return res.redirect(utils.url.urlFor('home', true));
} else {
return next();
}
Expand All @@ -127,7 +127,7 @@ privateBlogging = {
req.session.token = hasher.digest('hex');
req.session.salt = salt;

return res.redirect(config.urlFor({relativeUrl: decodeURIComponent(forward)}));
return res.redirect(utils.url.urlFor({relativeUrl: decodeURIComponent(forward)}));
} else {
res.error = {
message: i18n.t('errors.middleware.privateblogging.wrongPassword')
Expand Down
13 changes: 0 additions & 13 deletions core/server/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var path = require('path'),

validator = require('validator'),
errors = require('../errors'),
configUrl = require('./url'),
packageInfo = require('../../../package.json'),
i18n = require('../i18n'),
appRoot = path.resolve(__dirname, '../../../'),
Expand All @@ -26,13 +25,6 @@ function ConfigManager(config) {
*/
this._config = {};

// Allow other modules to be externally accessible.
this.urlJoin = configUrl.urlJoin;
this.urlFor = configUrl.urlFor;
this.urlPathForPost = configUrl.urlPathForPost;
this.apiUrl = configUrl.apiUrl;
this.getBaseUrl = configUrl.getBaseUrl;

// If we're given an initial config object then we can set it.
if (config && _.isObject(config)) {
this.set(config);
Expand Down Expand Up @@ -264,11 +256,6 @@ ConfigManager.prototype.set = function (config) {
}
});

// Also pass config object to
// configUrl object to maintain
// clean dependency tree
configUrl.setConfig(this._config);

// For now we're going to copy the current state of this._config
// so it's directly accessible on the instance.
// @TODO: perhaps not do this? Put access of the config object behind
Expand Down
3 changes: 2 additions & 1 deletion core/server/controllers/frontend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

var api = require('../../api'),
config = require('../../config'),
utils = require('../../utils'),
filters = require('../../filters'),
templates = require('./templates'),
handleError = require('./error'),
Expand Down Expand Up @@ -48,7 +49,7 @@ frontendControllers = {
}

if (post.status === 'published') {
return res.redirect(301, config.urlFor('post', {post: post}));
return res.redirect(301, utils.url.urlFor('post', {post: post}));
}

setRequestIsSecure(req, post);
Expand Down
4 changes: 2 additions & 2 deletions core/server/data/meta/amp_url.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var config = require('../../config'),
var utils = require('../../utils'),
getUrl = require('./url'),
_ = require('lodash');

function getAmplUrl(data) {
var context = data.context ? data.context : null;

if (_.includes(context, 'post') && !_.includes(context, 'amp')) {
return config.urlJoin(config.getBaseUrl(false),
return utils.url.urlJoin(utils.url.getBaseUrl(false),
getUrl(data, false)) + 'amp/';
}
return null;
Expand Down
4 changes: 2 additions & 2 deletions core/server/data/meta/author_image.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var config = require('../../config'),
var utils = require('../../utils'),
getContextObject = require('./context_object.js'),
_ = require('lodash');

Expand All @@ -7,7 +7,7 @@ function getAuthorImage(data, absolute) {
contextObject = getContextObject(data, context);

if ((_.includes(context, 'post') || _.includes(context, 'page')) && contextObject.author && contextObject.author.image) {
return config.urlFor('image', {image: contextObject.author.image}, absolute);
return utils.url.urlFor('image', {image: contextObject.author.image}, absolute);
}
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions core/server/data/meta/author_url.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var config = require('../../config');
var utils = require('../../utils');

function getAuthorUrl(data, absolute) {
var context = data.context ? data.context[0] : null;

context = context === 'amp' ? 'post' : context;

if (data.author) {
return config.urlFor('author', {author: data.author}, absolute);
return utils.url.urlFor('author', {author: data.author}, absolute);
}
if (data[context] && data[context].author) {
return config.urlFor('author', {author: data[context].author}, absolute);
return utils.url.urlFor('author', {author: data[context].author}, absolute);
}
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions core/server/data/meta/canonical_url.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var config = require('../../config'),
var utils = require('../../utils'),
getUrl = require('./url');

function getCanonicalUrl(data) {
var url = config.urlJoin(config.getBaseUrl(false),
var url = utils.url.urlJoin(utils.url.getBaseUrl(false),
getUrl(data, false));

if (url.indexOf('/amp/')) {
Expand Down
6 changes: 3 additions & 3 deletions core/server/data/meta/cover_image.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var config = require('../../config'),
var utils = require('../../utils'),
getContextObject = require('./context_object.js'),
_ = require('lodash');

Expand All @@ -8,11 +8,11 @@ function getCoverImage(data) {

if (_.includes(context, 'home') || _.includes(context, 'author')) {
if (contextObject.cover) {
return config.urlFor('image', {image: contextObject.cover}, true);
return utils.url.urlFor('image', {image: contextObject.cover}, true);
}
} else {
if (contextObject.image) {
return config.urlFor('image', {image: contextObject.image}, true);
return utils.url.urlFor('image', {image: contextObject.image}, true);
}
}
return null;
Expand Down
3 changes: 2 additions & 1 deletion core/server/data/meta/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var _ = require('lodash'),
Promise = require('bluebird'),
config = require('../../config'),
utils = require('../../utils'),
getUrl = require('./url'),
getImageDimensions = require('./image-dimensions'),
getCanonicalUrl = require('./canonical_url'),
Expand Down Expand Up @@ -50,7 +51,7 @@ function getMetaData(data, root) {

metaData.blog.logo = {};
metaData.blog.logo.url = config.theme.logo ?
config.urlFor('image', {image: config.theme.logo}, true) : config.urlFor({relativeUrl: '/ghost/img/ghosticon.jpg'}, {}, true);
utils.url.urlFor('image', {image: config.theme.logo}, true) : utils.url.urlFor({relativeUrl: '/ghost/img/ghosticon.jpg'}, {}, true);

// TODO: cleanup these if statements
if (data.post && data.post.html) {
Expand Down
5 changes: 3 additions & 2 deletions core/server/data/meta/paginated_url.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var _ = require('lodash'),
config = require('../../config');
config = require('../../config'),
utils = require('../../utils');

function getPaginatedUrl(page, data, absolute) {
// If we don't have enough information, return null right away
Expand Down Expand Up @@ -29,7 +30,7 @@ function getPaginatedUrl(page, data, absolute) {
// baseUrl can be undefined, if there was nothing preceding the pagePath (e.g. first page of the index channel)
newRelativeUrl = baseUrl ? baseUrl + newRelativeUrl : newRelativeUrl;

return config.urlFor({relativeUrl: newRelativeUrl, secure: data.secure}, absolute);
return utils.url.urlFor({relativeUrl: newRelativeUrl, secure: data.secure}, absolute);
}

module.exports = getPaginatedUrl;
4 changes: 2 additions & 2 deletions core/server/data/meta/rss_url.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var config = require('../../config');
var utils = require('../../utils');

function getRssUrl(data, absolute) {
return config.urlFor('rss', {secure: data.secure}, absolute);
return utils.url.urlFor('rss', {secure: data.secure}, absolute);
}

module.exports = getRssUrl;
12 changes: 6 additions & 6 deletions core/server/data/meta/url.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var schema = require('../schema').checks,
config = require('../../config');
utils = require('../../utils');

// This cleans the url from any `/amp` postfixes, so we'll never
// output a url with `/amp` in the end, except for the needed `amphtml`
Expand All @@ -13,23 +13,23 @@ function sanitizeAmpUrl(url) {

function getUrl(data, absolute) {
if (schema.isPost(data)) {
return config.urlFor('post', {post: data, secure: data.secure}, absolute);
return utils.url.urlFor('post', {post: data, secure: data.secure}, absolute);
}

if (schema.isTag(data)) {
return config.urlFor('tag', {tag: data, secure: data.secure}, absolute);
return utils.url.urlFor('tag', {tag: data, secure: data.secure}, absolute);
}

if (schema.isUser(data)) {
return config.urlFor('author', {author: data, secure: data.secure}, absolute);
return utils.url.urlFor('author', {author: data, secure: data.secure}, absolute);
}

if (schema.isNav(data)) {
return config.urlFor('nav', {nav: data, secure: data.secure}, absolute);
return utils.url.urlFor('nav', {nav: data, secure: data.secure}, absolute);
}

// sanitize any trailing `/amp` in the url
return sanitizeAmpUrl(config.urlFor(data, {}, absolute));
return sanitizeAmpUrl(utils.url.urlFor(data, {}, absolute));
}

module.exports = getUrl;
6 changes: 3 additions & 3 deletions core/server/data/slack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var https = require('https'),
errors = require('../../errors'),
url = require('url'),
Promise = require('bluebird'),
config = require('../../config'),
utils = require('../../utils'),
events = require('../../events'),
api = require('../../api/settings'),
i18n = require('../../i18n'),
Expand Down Expand Up @@ -47,7 +47,7 @@ function ping(post) {

// If this is a post, we want to send the link of the post
if (schema.isPost(post)) {
message = config.urlFor('post', {post: post}, true);
message = utils.url.urlFor('post', {post: post}, true);
} else {
message = post.message;
}
Expand All @@ -72,7 +72,7 @@ function ping(post) {
slackData = {
text: message,
unfurl_links: true,
icon_url: config.urlFor({relativeUrl: '/ghost/img/ghosticon.jpg'}, {}, true),
icon_url: utils.url.urlFor({relativeUrl: '/ghost/img/ghosticon.jpg'}, {}, true),
username: 'Ghost'
};

Expand Down
9 changes: 5 additions & 4 deletions core/server/data/xml/rss/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var crypto = require('crypto'),
downsize = require('downsize'),
RSS = require('rss'),
config = require('../../../config'),
utils = require('../../../utils'),
errors = require('../../../errors'),
filters = require('../../../filters'),
processUrls = require('../../../utils/make-absolute-urls'),
Expand Down Expand Up @@ -106,7 +107,7 @@ generateFeed = function generateFeed(data) {
});

data.results.posts.forEach(function forEach(post) {
var itemUrl = config.urlFor('post', {post: post, secure: data.secure}, true),
var itemUrl = utils.url.urlFor('post', {post: post, secure: data.secure}, true),
htmlContent = processUrls(post.html, data.siteUrl, itemUrl),
item = {
title: post.title,
Expand All @@ -121,7 +122,7 @@ generateFeed = function generateFeed(data) {
imageUrl;

if (post.image) {
imageUrl = config.urlFor('image', {image: post.image, secure: data.secure}, true);
imageUrl = utils.url.urlFor('image', {image: post.image, secure: data.secure}, true);

// Add a media content tag
item.custom_elements.push({
Expand Down Expand Up @@ -176,8 +177,8 @@ generate = function generate(req, res, next) {
}

data.version = res.locals.safeVersion;
data.siteUrl = config.urlFor('home', {secure: req.secure}, true);
data.feedUrl = config.urlFor({relativeUrl: baseUrl, secure: req.secure}, true);
data.siteUrl = utils.url.urlFor('home', {secure: req.secure}, true);
data.feedUrl = utils.url.urlFor({relativeUrl: baseUrl, secure: req.secure}, true);
data.secure = req.secure;

return getFeedXml(req.originalUrl, data).then(function then(feedXml) {
Expand Down
10 changes: 5 additions & 5 deletions core/server/data/xml/sitemap/base-generator.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var _ = require('lodash'),
xml = require('xml'),
moment = require('moment'),
config = require('../../../config'),
utils = require('../../../utils'),
events = require('../../../events'),
utils = require('./utils'),
localUtils = require('./utils'),
Promise = require('bluebird'),
path = require('path'),
CHANGE_FREQ = 'weekly',
Expand Down Expand Up @@ -92,7 +92,7 @@ _.extend(BaseSiteMapGenerator.prototype, {
};

// Return the xml
return utils.getDeclarations() + xml(data);
return localUtils.getDeclarations() + xml(data);
},

updateXmlFromNodes: function (urlElements) {
Expand Down Expand Up @@ -133,11 +133,11 @@ _.extend(BaseSiteMapGenerator.prototype, {
},

getUrlForDatum: function () {
return config.urlFor('home', true);
return utils.url.urlFor('home', true);
},

getUrlForImage: function (image) {
return config.urlFor('image', {image: image}, true);
return utils.url.urlFor('image', {image: image}, true);
},

getPriorityForDatum: function () {
Expand Down
8 changes: 4 additions & 4 deletions core/server/data/xml/sitemap/index-generator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var _ = require('lodash'),
xml = require('xml'),
moment = require('moment'),
config = require('../../../config'),
utils = require('./utils'),
utils = require('../../../utils'),
localUtils = require('./utils'),
RESOURCES,
XMLNS_DECLS;

Expand All @@ -28,14 +28,14 @@ _.extend(SiteMapIndexGenerator.prototype, {
};

// Return the xml
return utils.getDeclarations() + xml(data);
return localUtils.getDeclarations() + xml(data);
},

generateSiteMapUrlElements: function () {
var self = this;

return _.map(RESOURCES, function (resourceType) {
var url = config.urlFor({
var url = utils.url.urlFor({
relativeUrl: '/sitemap-' + resourceType + '.xml'
}, true),
lastModified = self[resourceType].lastModified;
Expand Down
Loading

0 comments on commit c8119ee

Please sign in to comment.