paradigm-core
Version:
Paradigm Core Modules
185 lines (141 loc) • 4.01 kB
JavaScript
const {
RootController,
ApplicationsPlugin: {ApplicationModel},
Cache: {cache}
} = require('structure-core')
const Purge = require('../purge')
/**
* PurgeController Class
*
* @public
* @class PurgeController
*/
class PurgeController extends RootController {
/**
* PurgeController constructor
*
* @public
* @constructor
* @param {Object} options - Options
*/
constructor(options = {}) {
super(Object.assign({}, {
name: 'purge'
}, options))
}
/**
* Recursively invalidate all pages of a an application on the CDN, update
* the lastPurgedAt for all documents of the application, and clear the
* middleware cache for said application
*
* @public
* @param {Object} req - Express req
* @param {Object} res - Express res
*/
async purgeAll(req, res) {
try {
const organizationId = req.headers.organizationid
const applicationId = req.headers.applicationid
const applicationModel = new ApplicationModel({
logger: req.logger,
organizationId
})
const application = await applicationModel.getById(applicationId)
const hostname = req.body.hostname || application.host
const protocol = application.protocol
const purge = new Purge({
organizationId: organizationId,
applicationId: application.id,
logger: req.logger,
hostname,
protocol
})
await purge.purgeAll()
await purge.clearCache()
} catch(e) {
this.logger.error('Error purging all', {err: e})
throw e
}
}
/**
* Recursively invalidate all urls of a an application on the CDN
*
* @public
* @param {Object} req - Express req
* @param {Object} res - Express res
*/
async purgeCdnAll(req, res) {
try {
const organizationId = req.headers.organizationid
const applicationId = req.headers.applicationid
const applicationModel = new ApplicationModel({
logger: req.logger,
organizationId
})
const application = await applicationModel.getById(applicationId)
const hostname = req.body.hostname || application.host
const protocol = application.protocol
const purge = new Purge({
organizationId: organizationId,
applicationId: application.id,
logger: req.logger,
hostname,
protocol
})
await purge.purgeAll()
} catch(e) {
this.logger.error('Error purging all Cdn', {err: e})
throw e
}
}
/**
* Invalidate a url of an application on the CDN, optionally recursively
*
* @public
* @param {Object} req - Express req
* @param {Object} res - Express res
*/
async purgeCdnUrl(req, res) {
try {
const organizationId = req.headers.organizationid
const applicationId = req.headers.applicationid
const applicationModel = new ApplicationModel({
logger: req.logger,
organizationId
})
const application = await applicationModel.getById(applicationId)
const hostname = req.body.hostname || application.host
const protocol = application.protocol
const url = req.body.url
const recursive = req.body.recursive
const purge = new Purge({
organizationId: organizationId,
applicationId: application.id,
logger: req.logger,
hostname,
protocol
})
await purge.purgeUrl(url, recursive)
} catch(e) {
this.logger.error('Error purging all Cdn', {err: e})
throw e
}
}
/**
* Clear the middleware cache for an application
*
* @public
* @param {Object} req - Express req
* @param {Object} res - Express res
*/
async clearCache(req, res) {
try {
const applicationId = req.headers.applicationid
await cache.invalidateApplication(applicationId)
} catch(e) {
this.logger.error('Error purging cache', {err: e})
throw e
}
}
}
module.exports = PurgeController