1
0
mirror of https://github.com/trambarhq/relaks-wordpress-example.git synced 2025-09-02 12:42:38 +02:00

Implemented cache status page (issue #39).

This commit is contained in:
Chung Leong
2019-01-29 22:02:21 +01:00
parent d24cc2cc1a
commit 3e60897e8c
2 changed files with 33 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
const Bluebird = require('bluebird');
const Moment = require('moment');
const FS = Bluebird.promisifyAll(require('fs'));
const OS = require('os');
const Express = require('express');
@@ -28,6 +29,7 @@ app.use(Compression())
app.use(SpiderDetector.middleware());
app.use(`/`, Express.static(`${__dirname}/www`));
app.get('/.mtime', handleTimestampRequest);
app.get('/.cache', handleCacheStatusRequest);
app.get('/json/*', handleJSONRequest);
app.get(`/*`, handlePageRequest);
app.purge(`/*`, handlePurgeRequest);
@@ -47,6 +49,34 @@ async function handleTimestampRequest(req, res, next) {
}
}
async function handleCacheStatusRequest(req, res, next) {
try {
res.set({ 'X-Accel-Expires': 0 });
res.type('html');
res.write(`<html><head><title>Nginx Cache</title></head><body>`);
res.write(`<table border="1" cellpadding="2">`);
res.write(`<thead><th>URL</th><th>Modified time</th><th>Size</th></thead>`);
res.write(`<tbody>`);
let entries = await NginxCache.read();
let total = 0;
for (let entry of _.orderBy(entries, 'mtime', 'desc')) {
let { url, mtime, size } = entry;
let date = Moment(mtime).format('LLLL');
let sizeKB = _.round(size / 1024, 2);
res.write(`<tr><td>${url}</td><td>${date}</td><td>${sizeKB}KB</td></tr>`)
total += size;
}
let totalMB = _.round(total / 1024 / 1024, 2);
res.write(`<tr><td colspan="2">Total</td><td>${totalMB}MB</td></tr>`)
res.write(`</tbody>`);
res.write(`</table>`);
res.write(`</body></html>`);
res.end();
} catch (err) {
next(err);
}
}
async function handleJSONRequest(req, res, next) {
try {
let path = `/wp-json/${req.url.substr(6)}`;