From 4cf57924912ac27d29ba8e59cdc3ae4cc31ecd95 Mon Sep 17 00:00:00 2001 From: Matt Godbolt Date: Sun, 12 Jan 2020 11:18:39 -0600 Subject: [PATCH] Cache headers for API requests. Default to 5m caching. Closes #1770 --- .../compiler-explorer.defaults.properties | 1 + lib/handlers/api.js | 55 ++++++++----------- 2 files changed, 24 insertions(+), 32 deletions(-) diff --git a/etc/config/compiler-explorer.defaults.properties b/etc/config/compiler-explorer.defaults.properties index 7f3873f91..8dea2fa99 100644 --- a/etc/config/compiler-explorer.defaults.properties +++ b/etc/config/compiler-explorer.defaults.properties @@ -5,6 +5,7 @@ binaryExecTimeoutMs=2000 defaultSource=builtin cacheConfig=InMemory(50) executableCacheConfig=InMemory(50) +apiMaxAgeSecs=600 maxConcurrentCompiles=4 staticMaxAgeSecs=1 maxUploadSize=16mb diff --git a/lib/handlers/api.js b/lib/handlers/api.js index db6472361..a9bc7b176 100644 --- a/lib/handlers/api.js +++ b/lib/handlers/api.js @@ -38,9 +38,15 @@ class ApiHandler { this.options = null; this.storageHandler = storageHandler; this.handle = express.Router(); + const cacheHeader = `public, max-age=${ceProps("apiMaxAgeSecs", 24 * 60 * 60)}`; this.handle.use((req, res, next) => { - res.header("Access-Control-Allow-Origin", "*"); - res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + res.header( + { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept", + "Cache-Control": cacheHeader + } + ); next(); }); this.handle.get('/compilers', this.handleCompilers.bind(this)); @@ -78,11 +84,9 @@ class ApiHandler { const normalizer = new clientStateNormalizer(); normalizer.fromGoldenLayout(config); - res.set('Content-Type', 'application/json'); - res.end(JSON.stringify(normalizer.normalized)); + res.send(normalizer.normalized); } else { - res.set('Content-Type', 'application/json'); - res.end(JSON.stringify(config)); + res.send(config); } }) .catch(err => { @@ -104,17 +108,18 @@ class ApiHandler { return newLangObj; }); + this.outputList(availableLanguages, 'Id', req, res); + } + + outputList(list, title, req, res) { if (req.accepts(['text', 'json']) === 'json') { - res.set('Content-Type', 'application/json'); - res.end(JSON.stringify(availableLanguages)); - } else { - res.set('Content-Type', 'text/plain'); - const title = 'Id'; - const maxLength = _.max(_.pluck(_.pluck(availableLanguages, 'id').concat([title]), 'length')); - res.write(utils.padRight(title, maxLength) + ' | Name\n'); - res.end(availableLanguages.map(lang => utils.padRight(lang.id, maxLength) + ' | ' + lang.name) - .join("\n")); + res.send(list); + return; } + const maxLength = _.max(_.pluck(_.pluck(list, 'id').concat([title]), 'length')); + res.set('Content-Type', 'text/plain'); + res.send(utils.padRight(title, maxLength) + ' | Name\n' + + list.map(lang => utils.padRight(lang.id, maxLength) + ' | ' + lang.name).join("\n")); } getLibrariesAsArray(languageId) { @@ -142,9 +147,7 @@ class ApiHandler { handleLangLibraries(req, res, next) { if (this.options) { if (req.params.language) { - res.set('Content-Type', 'application/json'); - const libsForLanguageArr = this.getLibrariesAsArray(req.params.language); - res.end(JSON.stringify(libsForLanguageArr)); + res.send(this.getLibrariesAsArray(req.params.language)); } else { next({ statusCode: 404, @@ -161,8 +164,7 @@ class ApiHandler { handleAllLibraries(req, res, next) { if (this.options) { - res.set('Content-Type', 'application/json'); - res.end(JSON.stringify(this.options.options.libs)); + res.send(this.options.options.libs); } else { next({ statusCode: 500, @@ -177,18 +179,7 @@ class ApiHandler { filteredCompilers = this.compilers.filter((val) => val.lang === req.params.language); } - if (req.accepts(['text', 'json']) === 'json') { - res.set('Content-Type', 'application/json'); - res.end(JSON.stringify(filteredCompilers)); - } else { - res.set('Content-Type', 'text/plain'); - const title = 'Compiler Name'; - const maxLength = _.max(_.pluck(_.pluck(filteredCompilers, 'id').concat([title]), 'length')); - res.write(utils.padRight(title, maxLength) + ' | Description\n'); - res.end( - filteredCompilers.map(compiler => utils.padRight(compiler.id, maxLength) + ' | ' + compiler.name) - .join("\n")); - } + this.outputList(filteredCompilers, 'Compiler Name', req, res); } setCompilers(compilers) {