2020-11-04 13:55:47 +03:00
|
|
|
const Promise = require('bluebird');
|
2019-06-20 14:19:22 +03:00
|
|
|
const moment = require('moment-timezone');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
2021-06-28 14:09:02 +03:00
|
|
|
const urlService = require('../../../frontend/services/url');
|
2019-06-20 14:19:22 +03:00
|
|
|
|
2021-06-28 14:09:02 +03:00
|
|
|
const debug = require('@tryghost/debug')('services:route-settings');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-06-27 16:10:35 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../shared/config');
|
2021-06-27 15:38:48 +03:00
|
|
|
const bridge = require('../../../bridge');
|
2021-09-23 19:17:46 +03:00
|
|
|
const ensureSettingsFile = require('./ensure-settings');
|
2021-09-27 13:29:54 +03:00
|
|
|
const SettingsLoader = require('./loader');
|
2019-06-20 14:19:22 +03:00
|
|
|
|
2021-06-27 16:10:35 +03:00
|
|
|
const messages = {
|
|
|
|
loadError: 'Could not load {filename} file.'
|
|
|
|
};
|
|
|
|
|
2019-06-20 14:34:22 +03:00
|
|
|
/**
|
|
|
|
* The `routes.yaml` file offers a way to configure your Ghost blog. It's currently a setting feature
|
|
|
|
* we have added. That's why the `routes.yaml` file is treated as a "setting" right now.
|
|
|
|
* If we want to add single permissions for this file (e.g. upload/download routes.yaml), we can add later.
|
|
|
|
*
|
|
|
|
* How does it work?
|
|
|
|
*
|
|
|
|
* - we first reset all url generators (each url generator belongs to one express router)
|
|
|
|
* - we don't destroy the resources, we only release them (this avoids reloading all resources from the db again)
|
|
|
|
* - then we reload the whole site app, which will reset all routers and re-create the url generators
|
|
|
|
*/
|
2021-06-27 15:38:48 +03:00
|
|
|
|
2021-06-27 16:10:35 +03:00
|
|
|
const filename = 'routes';
|
|
|
|
const ext = 'yaml';
|
2019-06-20 14:19:22 +03:00
|
|
|
|
2021-06-27 16:10:35 +03:00
|
|
|
const getSettingsFolder = async () => {
|
|
|
|
return config.getContentPath('settings');
|
|
|
|
};
|
2019-06-20 14:19:22 +03:00
|
|
|
|
2021-06-27 16:10:35 +03:00
|
|
|
const getSettingsFilePath = async () => {
|
|
|
|
const settingsFolder = await getSettingsFolder();
|
|
|
|
return path.join(settingsFolder, `${filename}.${ext}`);
|
2019-06-20 14:19:22 +03:00
|
|
|
};
|
|
|
|
|
2021-06-27 16:10:35 +03:00
|
|
|
const getBackupFilePath = async () => {
|
|
|
|
const settingsFolder = await getSettingsFolder();
|
|
|
|
return path.join(settingsFolder, `${filename}-${moment().format('YYYY-MM-DD-HH-mm-ss')}.${ext}`);
|
|
|
|
};
|
2019-06-20 14:23:58 +03:00
|
|
|
|
2021-06-27 16:10:35 +03:00
|
|
|
const createBackupFile = async (settingsPath, backupPath) => {
|
|
|
|
return await fs.copy(settingsPath, backupPath);
|
|
|
|
};
|
|
|
|
|
|
|
|
const restoreBackupFile = async (settingsPath, backupPath) => {
|
|
|
|
return await fs.copy(backupPath, settingsPath);
|
|
|
|
};
|
|
|
|
|
|
|
|
const saveFile = async (filePath, settingsPath) => {
|
|
|
|
return await fs.copy(filePath, settingsPath);
|
|
|
|
};
|
|
|
|
|
|
|
|
const readFile = (settingsFilePath) => {
|
|
|
|
return fs.readFile(settingsFilePath, 'utf-8')
|
2019-06-20 14:23:58 +03:00
|
|
|
.catch((err) => {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
|
|
|
|
2020-05-22 21:22:20 +03:00
|
|
|
if (errors.utils.isIgnitionError(err)) {
|
2019-06-20 14:23:58 +03:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.NotFoundError({
|
2019-06-20 14:23:58 +03:00
|
|
|
err: err
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-06-27 16:10:35 +03:00
|
|
|
const setFromFilePath = async (filePath) => {
|
|
|
|
const settingsPath = await getSettingsFilePath();
|
|
|
|
const backupPath = await getBackupFilePath();
|
|
|
|
|
|
|
|
await createBackupFile(settingsPath, backupPath);
|
|
|
|
await saveFile(filePath, settingsPath);
|
|
|
|
|
|
|
|
urlService.resetGenerators({releaseResourcesOnly: true});
|
|
|
|
|
|
|
|
const bringBackValidRoutes = async () => {
|
|
|
|
urlService.resetGenerators({releaseResourcesOnly: true});
|
|
|
|
|
|
|
|
await restoreBackupFile(settingsPath, backupPath);
|
|
|
|
|
|
|
|
return bridge.reloadFrontend();
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
bridge.reloadFrontend();
|
|
|
|
} catch (err) {
|
|
|
|
return bringBackValidRoutes()
|
|
|
|
.finally(() => {
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO: how can we get rid of this from here?
|
|
|
|
let tries = 0;
|
|
|
|
|
|
|
|
function isBlogRunning() {
|
|
|
|
debug('waiting for blog running');
|
|
|
|
return Promise.delay(1000)
|
|
|
|
.then(() => {
|
|
|
|
debug('waited for blog running');
|
|
|
|
if (!urlService.hasFinished()) {
|
|
|
|
if (tries > 5) {
|
|
|
|
throw new errors.InternalServerError({
|
|
|
|
message: tpl(messages.loadError, {filename: `${filename}.${ext}`})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
tries = tries + 1;
|
|
|
|
return isBlogRunning();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return isBlogRunning()
|
|
|
|
.catch((err) => {
|
|
|
|
return bringBackValidRoutes()
|
|
|
|
.finally(() => {
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const get = async () => {
|
|
|
|
const settingsFilePath = await getSettingsFilePath();
|
|
|
|
|
|
|
|
return readFile(settingsFilePath);
|
|
|
|
};
|
|
|
|
|
2021-09-23 19:17:46 +03:00
|
|
|
const init = function () {
|
|
|
|
debug('init routes settings service');
|
|
|
|
|
|
|
|
// Make sure that supported settings files are available
|
|
|
|
// inside of the `content/setting` directory
|
|
|
|
return ensureSettingsFile('routes.yaml');
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.init = init;
|
2021-09-27 13:29:54 +03:00
|
|
|
/**
|
|
|
|
* Fetches routes YAML settings synchronously.
|
|
|
|
* will return a JSON Object like this:
|
|
|
|
* {routes: {}, collections: {}, resources: {}}
|
|
|
|
* @returns {Object} routes.yaml in JSON format
|
|
|
|
*/
|
|
|
|
module.exports.loadRouteSettingsSync = () => {
|
2021-09-27 13:48:48 +03:00
|
|
|
return SettingsLoader.loadSettingsSync();
|
2021-09-27 13:29:54 +03:00
|
|
|
};
|
|
|
|
|
2021-09-23 19:31:13 +03:00
|
|
|
module.exports.api = {
|
|
|
|
setFromFilePath: setFromFilePath,
|
|
|
|
get: get
|
|
|
|
};
|