Skip to content

Commit

Permalink
Merge pull request TryGhost#1764 from ErisDS/path-cleanup
Browse files Browse the repository at this point in the history
Path, url and subdir cleanup & test
  • Loading branch information
ErisDS committed Dec 28, 2013
2 parents 32f6575 + 755ad93 commit 25f3df2
Show file tree
Hide file tree
Showing 24 changed files with 180 additions and 98 deletions.
4 changes: 2 additions & 2 deletions core/client/assets/lib/uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
.attr({'src': '', "width": 'auto', "height": 'auto'});

$progress.animate({"opacity": 0}, 250, function () {
$dropzone.find('span.media').after('<img class="fileupload-loading" src="' + Ghost.paths.ghostRoot + '/ghost/img/loadingcat.gif" />');
$dropzone.find('span.media').after('<img class="fileupload-loading" src="' + Ghost.paths.subdir + '/ghost/img/loadingcat.gif" />');
if (!settings.editor) {$progress.find('.fileupload-loading').css({"top": "56px"}); }
});
$dropzone.trigger("uploadsuccess", [result]);
Expand All @@ -62,7 +62,7 @@
var self = this;

$dropzone.find('.js-fileupload').fileupload().fileupload("option", {
url: Ghost.paths.ghostRoot + '/ghost/upload/',
url: Ghost.paths.subdir + '/ghost/upload/',
headers: {
'X-CSRF-Token': $("meta[name='csrf-param']").attr('content')
},
Expand Down
7 changes: 5 additions & 2 deletions core/client/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*globals Handlebars, moment
*/
/*globals Handlebars, moment, Ghost */
(function () {
'use strict';
Handlebars.registerHelper('date', function (context, options) {
Expand Down Expand Up @@ -29,4 +28,8 @@
}
return date;
});

Handlebars.registerHelper('url', function () {
return Ghost.paths.subdir;
});
}());
8 changes: 4 additions & 4 deletions core/client/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

function ghostPaths() {
var path = window.location.pathname,
root = path.substr(0, path.search('/ghost/'));
subdir = path.substr(0, path.search('/ghost/'));

return {
ghostRoot: root,
apiRoot: root + '/ghost/api/v0.1'
subdir: subdir,
apiRoot: subdir + '/ghost/api/v0.1'
};
}

Expand Down Expand Up @@ -61,7 +61,7 @@
Backbone.history.start({
pushState: true,
hashChange: false,
root: Ghost.paths.ghostRoot + '/ghost'
root: Ghost.paths.subdir + '/ghost'
});
};

Expand Down
2 changes: 1 addition & 1 deletion core/client/tpl/login.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
</div>
<button class="button-save" type="submit">Log in</button>
<section class="meta">
<a class="forgotten-password" href="/ghost/forgotten/">Forgotten password?</a>{{! &bull; <a href="/ghost/signup/">Register new user</a>}}
<a class="forgotten-password" href="{{url}}/ghost/forgotten/">Forgotten password?</a>
</section>
</form>
2 changes: 1 addition & 1 deletion core/client/tpl/preview.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<div class="no-posts-box">
<div class="no-posts">
<h3>You Haven't Written Any Posts Yet!</h3>
<form action="/ghost/editor/"><button class="button-add large" title="New Post">Write a new Post</button></form>
<form action="{{url}}/ghost/editor/"><button class="button-add large" title="New Post">Write a new Post</button></form>
</div>
</div>
{{/unless}}
2 changes: 1 addition & 1 deletion core/client/views/blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
e.preventDefault();
// for now this will disable "open in new tab", but when we have a Router implemented
// it can go back to being a normal link to '#/ghost/editor/X'
window.location = Ghost.paths.ghostRoot + '/ghost/editor/' + this.model.get('id') + '/';
window.location = Ghost.paths.subdir + '/ghost/editor/' + this.model.get('id') + '/';
},

toggleFeatured: function (e) {
Expand Down
8 changes: 4 additions & 4 deletions core/client/views/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
Ghost.Validate.handleErrors();
} else {
$.ajax({
url: Ghost.paths.ghostRoot + '/ghost/signin/',
url: Ghost.paths.subdir + '/ghost/signin/',
type: 'POST',
headers: {
'X-CSRF-Token': $("meta[name='csrf-param']").attr('content')
Expand Down Expand Up @@ -100,7 +100,7 @@
Ghost.Validate.handleErrors();
} else {
$.ajax({
url: Ghost.paths.ghostRoot + '/ghost/signup/',
url: Ghost.paths.subdir + '/ghost/signup/',
type: 'POST',
headers: {
'X-CSRF-Token': $("meta[name='csrf-param']").attr('content')
Expand Down Expand Up @@ -157,7 +157,7 @@
Ghost.Validate.handleErrors();
} else {
$.ajax({
url: Ghost.paths.ghostRoot + '/ghost/forgotten/',
url: Ghost.paths.subdir + '/ghost/forgotten/',
type: 'POST',
headers: {
'X-CSRF-Token': $("meta[name='csrf-param']").attr('content')
Expand Down Expand Up @@ -224,7 +224,7 @@
this.$('input, button').prop('disabled', true);

$.ajax({
url: Ghost.paths.ghostRoot + '/ghost/reset/' + this.token + '/',
url: Ghost.paths.subdir + '/ghost/reset/' + this.token + '/',
type: 'POST',
headers: {
'X-CSRF-Token': $("meta[name='csrf-param']").attr('content')
Expand Down
2 changes: 1 addition & 1 deletion core/client/views/post-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
}).then(function () {
// Redirect to content screen if deleting post from editor.
if (window.location.pathname.indexOf('editor') > -1) {
window.location = Ghost.paths.ghostRoot + '/ghost/content/';
window.location = Ghost.paths.subdir + '/ghost/content/';
}
Ghost.notifications.addItem({
type: 'success',
Expand Down
2 changes: 1 addition & 1 deletion core/client/views/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@
} else {

$.ajax({
url: '/ghost/changepw/',
url: Ghost.paths.subdir + '/ghost/changepw/',
type: 'POST',
headers: {
'X-CSRF-Token': $("meta[name='csrf-param']").attr('content')
Expand Down
4 changes: 2 additions & 2 deletions core/server/api/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var dataExport = require('../data/export'),
_ = require('underscore'),
schema = require('../data/schema'),
config = require('../config'),
debugPath = config.paths().webroot + '/ghost/debug/',
debugPath = config.paths().subdir + '/ghost/debug/',

db;

Expand Down Expand Up @@ -142,7 +142,7 @@ db = {
res.set({
"X-Cache-Invalidate": "/*"
});
res.redirect(config.paths().webroot + '/ghost/signin/');
res.redirect(config.paths().subdir + '/ghost/signin/');
});
}).otherwise(function importFailure(error) {
return apiNotifications.browse().then(function (notifications) {
Expand Down
2 changes: 1 addition & 1 deletion core/server/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ requestHandler = function (apiMethod) {
// If permalinks have changed, find old post route
if (req.body.permalinks && req.body.permalinks !== permalinks) {
for (i = 0; i < req.app.routes.get.length; i += 1) {
if (req.app.routes.get[i].path === config.paths().webroot + permalinks) {
if (req.app.routes.get[i].path === config.paths().subdir + permalinks) {
postRouteIndex = i;
break;
}
Expand Down
11 changes: 6 additions & 5 deletions core/server/config/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ function writeConfigFile() {
}

function validateConfigEnvironment() {
var envVal = process.env.NODE_ENV || 'undefined',
var envVal = process.env.NODE_ENV || undefined,
rejectMessage = 'Unable to load config',
hasHostAndPort,
hasSocket,
config,
Expand All @@ -65,20 +66,20 @@ function validateConfigEnvironment() {
if (!config) {
errors.logError(new Error('Cannot find the configuration for the current NODE_ENV'), "NODE_ENV=" + envVal,
'Ensure your config.js has a section for the current NODE_ENV value and is formatted properly.');
return when.reject();
return when.reject(rejectMessage);
}

// Check that our url is valid
parsedUrl = url.parse(config.url || 'invalid', false, true);
if (!parsedUrl.host) {
errors.logError(new Error('Your site url in config.js is invalid.'), config.url, 'Please make sure this is a valid url before restarting');
return when.reject();
return when.reject(rejectMessage);
}

// Check that we have database values
if (!config.database) {
errors.logError(new Error('Your database configuration in config.js is invalid.'), JSON.stringify(config.database), 'Please make sure this is a valid Bookshelf database configuration');
return when.reject();
return when.reject(rejectMessage);
}

hasHostAndPort = config.server && !!config.server.host && !!config.server.port;
Expand All @@ -87,7 +88,7 @@ function validateConfigEnvironment() {
// Check for valid server host and port values
if (!config.server || !(hasHostAndPort || hasSocket)) {
errors.logError(new Error('Your server values (socket, or host and port) in config.js are invalid.'), JSON.stringify(config.server), 'Please provide them before restarting.');
return when.reject();
return when.reject(rejectMessage);
}

return when.resolve(config);
Expand Down
3 changes: 1 addition & 2 deletions core/server/config/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ var path = require('path'),
function paths() {
return {
'appRoot': appRoot,
'path': localPath,
'webroot': localPath === '/' ? '' : localPath,
'subdir': localPath === '/' ? '' : localPath,
'config': path.join(appRoot, 'config.js'),
'configExample': path.join(appRoot, 'config.example.js'),
'contentPath': contentPath,
Expand Down
6 changes: 3 additions & 3 deletions core/server/config/theme.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Holds all theme configuration information
// that as mostly used by templates and handlebar helpers.

var when = require('when'),
var when = require('when'),

// Variables
themeConfig = {};
Expand All @@ -23,8 +23,8 @@ function update(settings, configUrl) {
settings.read('logo'),
settings.read('cover')
]).then(function (globals) {

themeConfig.url = configUrl;
// normalise the URL by removing any trailing slash
themeConfig.url = configUrl.replace(/\/$/, '');
themeConfig.title = globals[0].value;
themeConfig.description = globals[1].value;
themeConfig.logo = globals[2] ? globals[2].value : '';
Expand Down
12 changes: 6 additions & 6 deletions core/server/controllers/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ adminControllers = {
req.session.regenerate(function (err) {
if (!err) {
req.session.user = user.id;
var redirect = config.paths().webroot + '/ghost/';
var redirect = config.paths().subdir + '/ghost/';
if (req.body.redirect) {
redirect += decodeURIComponent(req.body.redirect);
}
Expand Down Expand Up @@ -137,7 +137,7 @@ adminControllers = {
if (req.session.user === undefined) {
req.session.user = user.id;
}
res.json(200, {redirect: config.paths().webroot + '/ghost/'});
res.json(200, {redirect: config.paths().subdir + '/ghost/'});
}
});
});
Expand Down Expand Up @@ -179,7 +179,7 @@ adminControllers = {
};

return api.notifications.add(notification).then(function () {
res.json(200, {redirect: config.paths().webroot + '/ghost/signin/'});
res.json(200, {redirect: config.paths().subdir + '/ghost/signin/'});
});

}, function failure(error) {
Expand Down Expand Up @@ -215,7 +215,7 @@ adminControllers = {
errors.logError(err, 'admin.js', "Please check the provided token for validity and expiration.");

return api.notifications.add(notification).then(function () {
res.redirect(config.paths().webroot + '/ghost/forgotten');
res.redirect(config.paths().subdir + '/ghost/forgotten');
});
});
},
Expand All @@ -233,7 +233,7 @@ adminControllers = {
};

return api.notifications.add(notification).then(function () {
res.json(200, {redirect: config.paths().webroot + '/ghost/signin/'});
res.json(200, {redirect: config.paths().subdir + '/ghost/signin/'});
});
}).otherwise(function (err) {
// TODO: Better error message if we can tell whether the passwords didn't match or something
Expand All @@ -251,7 +251,7 @@ adminControllers = {
};

return api.notifications.add(notification).then(function () {
res.redirect(config.paths().webroot + '/ghost/signin/');
res.redirect(config.paths().subdir + '/ghost/signin/');
});
},
'index': function (req, res) {
Expand Down
12 changes: 6 additions & 6 deletions core/server/controllers/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ frontendControllers = {

// Redirect '/page/1/' to '/' for all teh good SEO
if (pageParam === 1 && req.route.path === '/page/:page/') {
return res.redirect(config.paths().webroot + '/');
return res.redirect(config.paths().subdir + '/');
}

// No negative posts per page, must be number
Expand All @@ -54,7 +54,7 @@ frontendControllers = {

// If page is greater than number of pages we have, redirect to last page
if (pageParam > maxPage) {
return res.redirect(maxPage === 1 ? config.paths().webroot + '/' : (config.paths().webroot + '/page/' + maxPage + '/'));
return res.redirect(maxPage === 1 ? config.paths().subdir + '/' : (config.paths().subdir + '/page/' + maxPage + '/'));
}

// Render the page of posts
Expand Down Expand Up @@ -116,11 +116,11 @@ frontendControllers = {

// No negative pages
if (isNaN(pageParam) || pageParam < 1) {
return res.redirect(config.paths().webroot + '/rss/');
return res.redirect(config.paths().subdir + '/rss/');
}

if (pageParam === 1 && req.route.path === config.paths().webroot + '/rss/:page/') {
return res.redirect(config.paths().webroot + '/rss/');
if (pageParam === 1 && req.route.path === config.paths().subdir + '/rss/:page/') {
return res.redirect(config.paths().subdir + '/rss/');
}

api.posts.browse({page: pageParam}).then(function (page) {
Expand All @@ -134,7 +134,7 @@ frontendControllers = {

// If page is greater than number of pages we have, redirect to last page
if (pageParam > maxPage) {
return res.redirect(config.paths().webroot + '/rss/' + maxPage + '/');
return res.redirect(config.paths().subdir + '/rss/' + maxPage + '/');
}

filters.doFilter('prePostsRender', page.posts).then(function (posts) {
Expand Down
Loading

0 comments on commit 25f3df2

Please sign in to comment.