diff --git a/core/server/config/overrides.json b/core/server/config/overrides.json index 3f9f332990..ef33232cdf 100644 --- a/core/server/config/overrides.json +++ b/core/server/config/overrides.json @@ -8,6 +8,7 @@ "defaultViews": "core/server/views/", "defaultSettings": "core/frontend/services/settings/", "internalAppPath": "core/frontend/apps/", + "internalAdaptersPath": "core/server/adapters/", "internalStoragePath": "core/server/adapters/storage/", "internalSchedulingPath": "core/server/adapters/scheduling/", "migrationPath": "core/server/data/migrations", diff --git a/core/server/config/utils.js b/core/server/config/utils.js index 8961776413..86ca553dac 100644 --- a/core/server/config/utils.js +++ b/core/server/config/utils.js @@ -53,6 +53,8 @@ exports.getContentPath = function getContentPath(type) { return path.join(this.get('paths:contentPath'), 'images/'); case 'themes': return path.join(this.get('paths:contentPath'), 'themes/'); + case 'adapters': + return path.join(this.get('paths:contentPath'), 'adapters/'); case 'storage': return path.join(this.get('paths:contentPath'), 'adapters', 'storage/'); case 'scheduling': diff --git a/core/server/services/adapter-manager/config.js b/core/server/services/adapter-manager/config.js new file mode 100644 index 0000000000..6c29815adc --- /dev/null +++ b/core/server/services/adapter-manager/config.js @@ -0,0 +1,28 @@ +/** + * { + * [adapterType]: { + * active: [adapterName], + * [adapterName]: {} + * } + * } + */ +module.exports = function getAdapterServiceConfig(config) { + const adapterServiceConfig = config.get('adapters'); + + if (!adapterServiceConfig.storage) { + adapterServiceConfig.storage = config.get('storage'); + } + + if (!adapterServiceConfig.scheduling) { + const schedulingConfig = config.get('scheduling'); + const activeSchedulingAdapter = schedulingConfig.active; + adapterServiceConfig.scheduling = { + active: activeSchedulingAdapter, + [activeSchedulingAdapter]: { + schedulerUrl: schedulingConfig.schedulerUrl + } + }; + } + + return adapterServiceConfig; +}; diff --git a/core/server/services/adapter-manager/index.js b/core/server/services/adapter-manager/index.js new file mode 100644 index 0000000000..a75ed99e76 --- /dev/null +++ b/core/server/services/adapter-manager/index.js @@ -0,0 +1,27 @@ +const AdapterManager = require('@tryghost/adapter-manager'); +const getAdapterServiceConfig = require('./config'); +const config = require('../../config'); + +const adapterManager = new AdapterManager({ + loadAdapterFromPath: require, + pathsToAdapters: [ + '', // A blank path will cause us to check node_modules for the adapter + config.getContentPath('adapters'), + config.get('paths').internalAdaptersPath + ] +}); + +adapterManager.registerAdapter('storage', require('ghost-storage-base')); +adapterManager.registerAdapter('scheduling', require('../../adapters/scheduling/SchedulingBase')); + +module.exports = { + getAdapter(adapterType) { + const adapterServiceConfig = getAdapterServiceConfig(config); + + const adapterSettings = adapterServiceConfig[adapterType]; + const activeAdapter = adapterSettings.active; + const activeAdapterConfig = adapterSettings[activeAdapter]; + + return adapterManager.getAdapter(adapterType, activeAdapter, activeAdapterConfig); + } +}; diff --git a/package.json b/package.json index 1befcf20d9..b6f24a3fb1 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "dependencies": { "@nexes/nql": "0.3.0", "@sentry/node": "5.15.4", + "@tryghost/adapter-manager": "^0.1.0", "@tryghost/errors": "0.1.1", "@tryghost/helpers": "1.1.25", "@tryghost/image-transform": "0.2.0", diff --git a/yarn.lock b/yarn.lock index a65e47acf6..eb39af7ab3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -301,6 +301,13 @@ dependencies: defer-to-connect "^2.0.0" +"@tryghost/adapter-manager@^0.1.0": + version "0.1.0" + resolved "https://registry.yarnpkg.com/@tryghost/adapter-manager/-/adapter-manager-0.1.0.tgz#b95178b1d2f89de8b6a243ee312de61237c6a973" + integrity sha512-Z5Pnhd/b/EBsl6mCJdwmdVEESWnurgD/GAVma0g8GMuUECg0NCynyVJtvszKFny9484OagWzrpTUi7F5l4jp3Q== + dependencies: + "@tryghost/errors" "^0.1.1" + "@tryghost/errors@0.1.1", "@tryghost/errors@^0.1.1": version "0.1.1" resolved "https://registry.yarnpkg.com/@tryghost/errors/-/errors-0.1.1.tgz#c140f8558fc54cf69ac7e7ea719a189c763fea04"