mirror of
https://github.com/trambarhq/relaks-wordpress-example.git
synced 2025-09-02 20:52:33 +02:00
Implemented cache status page (issue #39).
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
const Bluebird = require('bluebird');
|
const Bluebird = require('bluebird');
|
||||||
|
const Moment = require('moment');
|
||||||
const FS = Bluebird.promisifyAll(require('fs'));
|
const FS = Bluebird.promisifyAll(require('fs'));
|
||||||
const OS = require('os');
|
const OS = require('os');
|
||||||
const Express = require('express');
|
const Express = require('express');
|
||||||
@@ -28,6 +29,7 @@ app.use(Compression())
|
|||||||
app.use(SpiderDetector.middleware());
|
app.use(SpiderDetector.middleware());
|
||||||
app.use(`/`, Express.static(`${__dirname}/www`));
|
app.use(`/`, Express.static(`${__dirname}/www`));
|
||||||
app.get('/.mtime', handleTimestampRequest);
|
app.get('/.mtime', handleTimestampRequest);
|
||||||
|
app.get('/.cache', handleCacheStatusRequest);
|
||||||
app.get('/json/*', handleJSONRequest);
|
app.get('/json/*', handleJSONRequest);
|
||||||
app.get(`/*`, handlePageRequest);
|
app.get(`/*`, handlePageRequest);
|
||||||
app.purge(`/*`, handlePurgeRequest);
|
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) {
|
async function handleJSONRequest(req, res, next) {
|
||||||
try {
|
try {
|
||||||
let path = `/wp-json/${req.url.substr(6)}`;
|
let path = `/wp-json/${req.url.substr(6)}`;
|
||||||
|
@@ -58,11 +58,11 @@ let cacheEntryCache = {};
|
|||||||
async function loadCacheEntry(md5) {
|
async function loadCacheEntry(md5) {
|
||||||
try {
|
try {
|
||||||
let path = `${NGINX_CACHE}/${md5}`;
|
let path = `${NGINX_CACHE}/${md5}`;
|
||||||
let { mtime } = await FS.statAsync(path);
|
let { mtime, size } = await FS.statAsync(path);
|
||||||
let entry = cacheEntryCache[md5];
|
let entry = cacheEntryCache[md5];
|
||||||
if (!entry || entry.mtime !== mtime) {
|
if (!entry || entry.mtime !== mtime) {
|
||||||
let url = await loadCacheEntryKey(path);
|
let url = await loadCacheEntryKey(path);
|
||||||
entry = cacheEntryCache[md5] = { url, mtime, md5 };
|
entry = cacheEntryCache[md5] = { url, md5, mtime, size };
|
||||||
}
|
}
|
||||||
return entry;
|
return entry;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -98,4 +98,5 @@ async function removeCacheEntry(entry) {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
purge,
|
purge,
|
||||||
|
read: loadCacheEntries,
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user