Set caching headers on asm API requests

This commit is contained in:
Matt Godbolt
2017-04-04 08:03:23 -05:00
parent ca75d5a7ea
commit 0d38dfa2fd
4 changed files with 23 additions and 8 deletions

17
app.js
View File

@@ -24,24 +24,22 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// load external and internal libraries (will load more internal binaries later)
// Initialise options and properties. Don't load any handlers here; they
// may need an initialised properties library.
var nopt = require('nopt'),
os = require('os'),
props = require('./lib/properties'),
CompileHandler = require('./lib/compile-handler').CompileHandler,
express = require('express'),
child_process = require('child_process'),
path = require('path'),
fs = require('fs-extra'),
http = require('http'),
https = require('https'),
url = require('url'),
utils = require('./lib/utils'),
Promise = require('promise'),
aws = require('./lib/aws'),
_ = require('underscore-node'),
logger = require('./lib/logger').logger,
asm_doc_api = require('./lib/asm-docs-api');
utils = require('./lib/utils'),
express = require('express'),
logger = require('./lib/logger').logger;
// Parse arguments from command line 'node ./app.js args...'
var opts = nopt({
@@ -89,6 +87,11 @@ if (opts.propDebug) props.setDebug(true);
// *All* files in config dir are parsed
props.initialize(rootDir + '/config', propHierarchy);
// Now load up our libraries.
var CompileHandler = require('./lib/compile-handler').CompileHandler,
aws = require('./lib/aws'),
asm_doc_api = require('./lib/asm-docs-api');
// Instantiate a function to access records concerning "compiler-explorer"
// in hidden object props.properties
var gccProps = props.propsFor("compiler-explorer");

View File

@@ -0,0 +1 @@
staticMaxAgeSecs=3600

View File

@@ -0,0 +1 @@
staticMaxAgeSecs=10

View File

@@ -24,8 +24,12 @@
// POSSIBILITY OF SUCH DAMAGE.
var asm_doc = require('./asm-docs');
var props = require('./properties');
function docHandler(req, res) {
var asmProps = props.propsFor("asm-docs");
var staticMaxAgeSecs = asmProps('staticMaxAgeSecs', 10);
function docHandler(req, res, next) {
var info = asm_doc.getAsmOpcode(req.params.opcode);
if (!info) {
// If the opcode ends with an AT&T suffix, try removing that and giving it another go.
@@ -36,6 +40,12 @@ function docHandler(req, res) {
info = asm_doc.getAsmOpcode(suffixRemoved[1]);
}
}
if (staticMaxAgeSecs) {
res.setHeader('Cache-Control', 'public, max-age=' + staticMaxAgeSecs);
}
if (!info) {
return res.status(404).send("Unknown opcode");
}
if (req.accepts(['text', 'json']) == 'json') {
res.set('Content-Type', 'application/json');
res.end(JSON.stringify(info));