0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Parameterized url cache storage path

refs https://github.com/TryGhost/Toolbox/issues/116

- This should allow testing in a bit easier manner and would place the file into a more suitable directory
- ideally we'd put an alfa flag bahind this new "cached routes" feature to have less consequences to deal with if we have to back out
This commit is contained in:
Naz 2021-11-11 16:48:36 +04:00 committed by naz
parent 36bc05447c
commit 4582fd48b5
2 changed files with 26 additions and 6 deletions

View file

@ -18,9 +18,14 @@ const events = require('../../lib/common/events');
* It will tell you if the url generation is in progress or not.
*/
class UrlService {
constructor() {
/**
*
* @param {Object} options
* @param {String} [options.urlCachePath] - path to store cached URLs at
*/
constructor({urlCachePath} = {}) {
this.utils = urlUtils;
this.urlCachePath = urlCachePath;
this.finished = false;
this.urlGenerators = [];
@ -308,15 +313,23 @@ class UrlService {
}
async persistUrls() {
return fs.writeFileSync('./urls.json', JSON.stringify(this.urls.urls, null, 4));
if (!this.urlCachePath) {
return null;
}
return fs.writeFileSync(this.urlCachePath, JSON.stringify(this.urls.urls, null, 4));
}
async fetchUrls() {
if (!this.urlCachePath) {
return null;
}
let urlsCacheExists = false;
let urls;
try {
await fs.stat('./urls.json');
await fs.stat(this.urlCachePath);
urlsCacheExists = true;
} catch (e) {
urlsCacheExists = false;
@ -324,7 +337,7 @@ class UrlService {
if (urlsCacheExists) {
try {
const urlsFile = await fs.readFile('./urls.json', 'utf8');
const urlsFile = await fs.readFile(this.urlCachePath, 'utf8');
urls = JSON.parse(urlsFile);
} catch (e) {
//noop as we'd start a long boot process if there are any errors in the file

View file

@ -1,5 +1,12 @@
const path = require('path');
const config = require('../../../shared/config');
const UrlService = require('./UrlService');
const urlService = new UrlService();
// NOTE: instead of a path we could give UrlService a "data-resolver" of some sort
// so it doesn't have to contain the logic to read data at all. This would be
// a possible improvement in the future
const urlCachePath = path.join(config.getContentPath('data'), 'urls.json');
const urlService = new UrlService({urlCachePath});
// Singleton
module.exports = urlService;