0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

🎨 use apiUrl helper for scheduling initialisation (#7400)

no issue
- extend apiUrl to return either an admin url for CORS or not
This commit is contained in:
Katharina Irrgang 2016-09-19 15:41:50 +02:00 committed by Hannah Wolfe
parent b5ef1da584
commit 265f058466
5 changed files with 35 additions and 14 deletions

View file

@ -233,7 +233,15 @@ function urlFor(context, data, absolute) {
return createUrl(urlPath, absolute, secure);
}
function apiUrl() {
/**
* CASE: generate api url for CORS
* - we delete the http protocol if your blog runs with http and https (configured by nginx)
* - in that case your config.js configures Ghost with http and no admin ssl force
* - the browser then reads the protocol dynamically
*/
function apiUrl(options) {
options = options || {cors: false};
// @TODO unify this with urlFor
var url;
@ -244,7 +252,11 @@ function apiUrl() {
} else if (ghostConfig.url.match(/^https:/)) {
url = ghostConfig.url;
} else {
url = ghostConfig.url.replace(/^.*?:\/\//g, '//');
if (options.cors === false) {
url = ghostConfig.url;
} else {
url = ghostConfig.url.replace(/^.*?:\/\//g, '//');
}
}
return url.replace(/\/$/, '') + apiPath + '/';

View file

@ -160,7 +160,7 @@ function init(options) {
// scheduling can trigger api requests, that's why we initialize the module after the ghost server creation
// scheduling module can create x schedulers with different adapters
return scheduling.init(_.extend(config.scheduling, {apiUrl: config.urlFor('api', null, true)}));
return scheduling.init(_.extend(config.scheduling, {apiUrl: config.apiUrl()}));
}).then(function () {
return ghostServer;
});

View file

@ -27,7 +27,7 @@ function serveSharedFile(file, type, maxAge) {
if (type === 'text/xsl' || type === 'text/plain' || type === 'application/javascript') {
buf = buf.toString().replace(blogRegex, config.url.replace(/\/$/, ''));
buf = buf.toString().replace(apiRegex, config.apiUrl());
buf = buf.toString().replace(apiRegex, config.apiUrl({cors: true}));
}
content = {
headers: {

View file

@ -609,12 +609,20 @@ describe('Config', function () {
config.apiUrl().should.eql('https://my-ghost-blog.com/ghost/api/v0.1/');
});
it('should return no protocol config.url if config.url is NOT https & forceAdminSSL/urlSSL is NOT set', function () {
it('CORS: should return no protocol config.url if config.url is NOT https & forceAdminSSL/urlSSL is NOT set', function () {
configUtils.set({
url: 'http://my-ghost-blog.com'
});
config.apiUrl().should.eql('//my-ghost-blog.com/ghost/api/v0.1/');
config.apiUrl({cors: true}).should.eql('//my-ghost-blog.com/ghost/api/v0.1/');
});
it('should return protocol config.url if config.url is NOT https & forceAdminSSL/urlSSL is NOT set', function () {
configUtils.set({
url: 'http://my-ghost-blog.com'
});
config.apiUrl().should.eql('http://my-ghost-blog.com/ghost/api/v0.1/');
});
});
});

View file

@ -6,6 +6,7 @@ var should = require('should'),
should.equal(true, true);
// @TODO: ghostUrl.init was obviously written for this test, get rid of it! (write a route test instead)
describe('Ghost Ajax Helper', function () {
beforeEach(function () {
configUtils.set({
@ -30,7 +31,7 @@ describe('Ghost Ajax Helper', function () {
ghostUrl.init({
clientId: '',
clientSecret: '',
url: configUtils.config.apiUrl()
url: configUtils.config.apiUrl({cors: true})
});
ghostUrl.url.api().should.equal('//testblog.com/ghost/api/v0.1/');
@ -40,7 +41,7 @@ describe('Ghost Ajax Helper', function () {
ghostUrl.init({
clientId: '',
clientSecret: '',
url: configUtils.config.apiUrl()
url: configUtils.config.apiUrl({cors: true})
});
ghostUrl.url.api('a/', '/b', '/c/').should.equal('//testblog.com/ghost/api/v0.1/a/b/c/');
@ -50,7 +51,7 @@ describe('Ghost Ajax Helper', function () {
ghostUrl.init({
clientId: 'ghost-frontend',
clientSecret: 'notasecret',
url: configUtils.config.apiUrl()
url: configUtils.config.apiUrl({cors: true})
});
ghostUrl.url.api().should.equal('//testblog.com/ghost/api/v0.1/?client_id=ghost-frontend&client_secret=notasecret');
@ -60,7 +61,7 @@ describe('Ghost Ajax Helper', function () {
ghostUrl.init({
clientId: 'ghost-frontend',
clientSecret: 'notasecret',
url: configUtils.config.apiUrl()
url: configUtils.config.apiUrl({cors: true})
});
var rendered = ghostUrl.url.api({a: 'string', b: 5, c: 'en coded'});
@ -98,7 +99,7 @@ describe('Ghost Ajax Helper', function () {
ghostUrl.init({
clientId: 'ghost-frontend',
clientSecret: 'notasecret',
url: configUtils.config.apiUrl()
url: configUtils.config.apiUrl({cors: true})
});
var rendered = ghostUrl.url.api('posts/', '/tags/', '/count', {include: 'tags,tests', page: 2});
@ -117,7 +118,7 @@ describe('Ghost Ajax Helper', function () {
ghostUrl.init({
clientId: 'ghost-frontend',
clientSecret: 'notasecret',
url: configUtils.config.apiUrl()
url: configUtils.config.apiUrl({cors: true})
});
var rendered = ghostUrl.url.api('posts/', '/tags/', '/count', {include: 'tags,tests', page: 2});
@ -136,7 +137,7 @@ describe('Ghost Ajax Helper', function () {
ghostUrl.init({
clientId: 'ghost-frontend',
clientSecret: 'notasecret',
url: configUtils.config.apiUrl()
url: configUtils.config.apiUrl({cors: true})
});
var rendered = ghostUrl.url.api('posts/', '/tags/', '/count', {include: 'tags,tests', page: 2});
@ -155,7 +156,7 @@ describe('Ghost Ajax Helper', function () {
ghostUrl.init({
clientId: 'ghost-frontend',
clientSecret: 'notasecret',
url: configUtils.config.apiUrl()
url: configUtils.config.apiUrl({cors: true})
});
var rendered = ghostUrl.url.api('posts', {limit: 3}),