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:
parent
b5ef1da584
commit
265f058466
5 changed files with 35 additions and 14 deletions
|
@ -233,7 +233,15 @@ function urlFor(context, data, absolute) {
|
||||||
return createUrl(urlPath, absolute, secure);
|
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
|
// @TODO unify this with urlFor
|
||||||
var url;
|
var url;
|
||||||
|
|
||||||
|
@ -244,7 +252,11 @@ function apiUrl() {
|
||||||
} else if (ghostConfig.url.match(/^https:/)) {
|
} else if (ghostConfig.url.match(/^https:/)) {
|
||||||
url = ghostConfig.url;
|
url = ghostConfig.url;
|
||||||
} else {
|
} else {
|
||||||
url = ghostConfig.url.replace(/^.*?:\/\//g, '//');
|
if (options.cors === false) {
|
||||||
|
url = ghostConfig.url;
|
||||||
|
} else {
|
||||||
|
url = ghostConfig.url.replace(/^.*?:\/\//g, '//');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return url.replace(/\/$/, '') + apiPath + '/';
|
return url.replace(/\/$/, '') + apiPath + '/';
|
||||||
|
|
|
@ -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 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
|
// 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 () {
|
}).then(function () {
|
||||||
return ghostServer;
|
return ghostServer;
|
||||||
});
|
});
|
||||||
|
|
|
@ -27,7 +27,7 @@ function serveSharedFile(file, type, maxAge) {
|
||||||
|
|
||||||
if (type === 'text/xsl' || type === 'text/plain' || type === 'application/javascript') {
|
if (type === 'text/xsl' || type === 'text/plain' || type === 'application/javascript') {
|
||||||
buf = buf.toString().replace(blogRegex, config.url.replace(/\/$/, ''));
|
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 = {
|
content = {
|
||||||
headers: {
|
headers: {
|
||||||
|
|
|
@ -609,12 +609,20 @@ describe('Config', function () {
|
||||||
config.apiUrl().should.eql('https://my-ghost-blog.com/ghost/api/v0.1/');
|
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({
|
configUtils.set({
|
||||||
url: 'http://my-ghost-blog.com'
|
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/');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -6,6 +6,7 @@ var should = require('should'),
|
||||||
|
|
||||||
should.equal(true, true);
|
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 () {
|
describe('Ghost Ajax Helper', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
configUtils.set({
|
configUtils.set({
|
||||||
|
@ -30,7 +31,7 @@ describe('Ghost Ajax Helper', function () {
|
||||||
ghostUrl.init({
|
ghostUrl.init({
|
||||||
clientId: '',
|
clientId: '',
|
||||||
clientSecret: '',
|
clientSecret: '',
|
||||||
url: configUtils.config.apiUrl()
|
url: configUtils.config.apiUrl({cors: true})
|
||||||
});
|
});
|
||||||
|
|
||||||
ghostUrl.url.api().should.equal('//testblog.com/ghost/api/v0.1/');
|
ghostUrl.url.api().should.equal('//testblog.com/ghost/api/v0.1/');
|
||||||
|
@ -40,7 +41,7 @@ describe('Ghost Ajax Helper', function () {
|
||||||
ghostUrl.init({
|
ghostUrl.init({
|
||||||
clientId: '',
|
clientId: '',
|
||||||
clientSecret: '',
|
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/');
|
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({
|
ghostUrl.init({
|
||||||
clientId: 'ghost-frontend',
|
clientId: 'ghost-frontend',
|
||||||
clientSecret: 'notasecret',
|
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');
|
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({
|
ghostUrl.init({
|
||||||
clientId: 'ghost-frontend',
|
clientId: 'ghost-frontend',
|
||||||
clientSecret: 'notasecret',
|
clientSecret: 'notasecret',
|
||||||
url: configUtils.config.apiUrl()
|
url: configUtils.config.apiUrl({cors: true})
|
||||||
});
|
});
|
||||||
|
|
||||||
var rendered = ghostUrl.url.api({a: 'string', b: 5, c: 'en coded'});
|
var rendered = ghostUrl.url.api({a: 'string', b: 5, c: 'en coded'});
|
||||||
|
@ -98,7 +99,7 @@ describe('Ghost Ajax Helper', function () {
|
||||||
ghostUrl.init({
|
ghostUrl.init({
|
||||||
clientId: 'ghost-frontend',
|
clientId: 'ghost-frontend',
|
||||||
clientSecret: 'notasecret',
|
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});
|
var rendered = ghostUrl.url.api('posts/', '/tags/', '/count', {include: 'tags,tests', page: 2});
|
||||||
|
@ -117,7 +118,7 @@ describe('Ghost Ajax Helper', function () {
|
||||||
ghostUrl.init({
|
ghostUrl.init({
|
||||||
clientId: 'ghost-frontend',
|
clientId: 'ghost-frontend',
|
||||||
clientSecret: 'notasecret',
|
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});
|
var rendered = ghostUrl.url.api('posts/', '/tags/', '/count', {include: 'tags,tests', page: 2});
|
||||||
|
@ -136,7 +137,7 @@ describe('Ghost Ajax Helper', function () {
|
||||||
ghostUrl.init({
|
ghostUrl.init({
|
||||||
clientId: 'ghost-frontend',
|
clientId: 'ghost-frontend',
|
||||||
clientSecret: 'notasecret',
|
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});
|
var rendered = ghostUrl.url.api('posts/', '/tags/', '/count', {include: 'tags,tests', page: 2});
|
||||||
|
@ -155,7 +156,7 @@ describe('Ghost Ajax Helper', function () {
|
||||||
ghostUrl.init({
|
ghostUrl.init({
|
||||||
clientId: 'ghost-frontend',
|
clientId: 'ghost-frontend',
|
||||||
clientSecret: 'notasecret',
|
clientSecret: 'notasecret',
|
||||||
url: configUtils.config.apiUrl()
|
url: configUtils.config.apiUrl({cors: true})
|
||||||
});
|
});
|
||||||
|
|
||||||
var rendered = ghostUrl.url.api('posts', {limit: 3}),
|
var rendered = ghostUrl.url.api('posts', {limit: 3}),
|
||||||
|
|
Loading…
Add table
Reference in a new issue