mirror of
https://github.com/compiler-explorer/compiler-explorer.git
synced 2026-12-02 12:57:32 -06:00
Cache headers for API requests. Default to 5m caching. Closes #1770
This commit is contained in:
@@ -5,6 +5,7 @@ binaryExecTimeoutMs=2000
|
||||
defaultSource=builtin
|
||||
cacheConfig=InMemory(50)
|
||||
executableCacheConfig=InMemory(50)
|
||||
apiMaxAgeSecs=600
|
||||
maxConcurrentCompiles=4
|
||||
staticMaxAgeSecs=1
|
||||
maxUploadSize=16mb
|
||||
|
||||
+23
-32
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user