2019-07-24 10:17:05 +02:00
|
|
|
'use strict';
|
2017-10-26 17:33:59 +02:00
|
|
|
|
2019-07-24 10:17:05 +02:00
|
|
|
let express = require('express');
|
|
|
|
let app = express();
|
|
|
|
let mcache = require('memory-cache');
|
2017-10-26 17:33:59 +02:00
|
|
|
const request = require('superagent');
|
2020-03-11 13:51:10 +01:00
|
|
|
const prom = require('prom-client');
|
2020-05-14 18:48:40 +02:00
|
|
|
const URL = require('url');
|
2022-10-03 11:22:13 +02:00
|
|
|
var PropertiesReader = require('properties-reader');
|
|
|
|
var properties = PropertiesReader('./properties.file');
|
2020-06-26 15:33:35 +02:00
|
|
|
const expireShort = 2 * 60 * 1000; //2mins
|
|
|
|
const expireLong = 24 * 60 * 60 * 1000; //24 hours
|
2021-07-14 17:41:12 +02:00
|
|
|
const cacheMaxSize = 500;
|
2020-10-20 16:53:26 +02:00
|
|
|
const longCachingRequests = ["/communityFull", "/full", "/pagehelpcontent",
|
2021-07-14 17:41:12 +02:00
|
|
|
"/provision/mvc/vocabularies/", "/pages?page_route=",
|
|
|
|
"/allmetrics", "/countryusagestats/", "/openaire/info",
|
|
|
|
"/api/communities/", "/openaire/contexts/"];
|
2020-03-11 13:51:10 +01:00
|
|
|
let cors = require('cors');
|
|
|
|
app.use(cors());
|
2017-10-27 13:59:57 +02:00
|
|
|
|
2020-03-11 13:51:10 +01:00
|
|
|
const register = new prom.Registry();
|
2020-04-10 16:29:46 +02:00
|
|
|
prom.collectDefaultMetrics({register: register});
|
2017-10-27 13:59:57 +02:00
|
|
|
|
2020-03-11 13:51:10 +01:00
|
|
|
const responses = new prom.Counter({
|
|
|
|
name: 'cache_http_responses_total',
|
|
|
|
help: 'A counter for cache response codes for every API request.',
|
2020-04-15 15:53:37 +02:00
|
|
|
labelNames: ['scheme', 'target', 'code'],
|
2020-04-10 16:29:46 +02:00
|
|
|
registers: [register]
|
|
|
|
});
|
|
|
|
|
2020-04-15 15:53:37 +02:00
|
|
|
const entries = new prom.Gauge({
|
2020-04-10 16:29:46 +02:00
|
|
|
name: 'cache_used_entries',
|
|
|
|
help: 'A counter to count cache entries',
|
2020-03-11 13:51:10 +01:00
|
|
|
registers: [register]
|
|
|
|
});
|
2017-10-27 13:59:57 +02:00
|
|
|
|
2020-03-11 13:51:10 +01:00
|
|
|
const histogram = new prom.Histogram({
|
|
|
|
name: 'cache_http_request_duration_seconds',
|
2020-04-10 16:29:46 +02:00
|
|
|
help: 'A Histogram for cache. Providing information about a cache request and load latency in seconds.',
|
2020-04-15 15:53:37 +02:00
|
|
|
labelNames: ['scheme', 'target', 'cache'],
|
2020-03-11 13:51:10 +01:00
|
|
|
registers: [register],
|
|
|
|
buckets: [0.1, 0.2, 0.5, 1, 2]
|
|
|
|
});
|
2017-10-26 17:33:59 +02:00
|
|
|
|
2020-06-26 15:33:35 +02:00
|
|
|
let cache = () => {
|
2020-03-11 13:51:10 +01:00
|
|
|
return (req, res, next) => {
|
2021-07-14 17:41:12 +02:00
|
|
|
if (req.query.url) {
|
|
|
|
if (mcache.memsize() > cacheMaxSize) {
|
|
|
|
console.log("Max cache size reached!" + cacheMaxSize);
|
2020-06-26 15:33:35 +02:00
|
|
|
clearCache();
|
|
|
|
}
|
2020-04-15 15:53:37 +02:00
|
|
|
let key = '__express__' + req.query.url;
|
2020-06-26 15:33:35 +02:00
|
|
|
let longCache = checkForLongCachedRequests(req.query.url);
|
2020-04-15 15:53:37 +02:00
|
|
|
let cachedBody = mcache.get(key);
|
2020-05-14 18:48:40 +02:00
|
|
|
const url = new URL.parse(req.query.url);
|
2020-04-15 15:53:37 +02:00
|
|
|
const target = url.host + '/' + url.pathname.split('/')[1];
|
|
|
|
const scheme = url.protocol.replace(':', '');
|
|
|
|
if (cachedBody) {
|
|
|
|
const end = histogram.startTimer({scheme: scheme, target: target, cache: 'hit'});
|
|
|
|
res.send(JSON.parse(cachedBody));
|
|
|
|
responses.inc({scheme: scheme, target: target, code: res.statusCode});
|
|
|
|
end();
|
|
|
|
} else {
|
|
|
|
const end = histogram.startTimer({scheme: scheme, target: target, cache: 'miss'});
|
|
|
|
res.sendResponse = res.send;
|
|
|
|
res.send = (body) => {
|
|
|
|
let alreadyCached = !!mcache.get(key);
|
2020-07-29 16:09:45 +02:00
|
|
|
entries.set(mcache.memsize());
|
2021-07-14 17:41:12 +02:00
|
|
|
if (!alreadyCached) {
|
2020-04-15 15:53:37 +02:00
|
|
|
responses.inc({scheme: scheme, target: target, code: res.statusCode});
|
|
|
|
end();
|
|
|
|
}
|
|
|
|
if (res.statusCode === 200) {
|
2020-06-26 15:33:35 +02:00
|
|
|
// console.log("Expire in " +(longCache?expireLong:expireShort) + " " +req.query.url);
|
2021-07-14 17:41:12 +02:00
|
|
|
mcache.put(key, body, longCache ? expireLong : expireShort, () => {
|
2020-07-29 16:09:45 +02:00
|
|
|
entries.set(mcache.memsize());
|
2020-04-15 15:53:37 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
res.sendResponse(body);
|
|
|
|
};
|
|
|
|
next();
|
|
|
|
}
|
2020-03-11 13:51:10 +01:00
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
};
|
2020-03-11 13:53:14 +01:00
|
|
|
};
|
2017-10-26 17:33:59 +02:00
|
|
|
|
2020-06-26 15:33:35 +02:00
|
|
|
app.get('/clear', (req, res) => {
|
|
|
|
let c = mcache.size();
|
|
|
|
const url = req.query.url;
|
2021-07-14 17:41:12 +02:00
|
|
|
let message = "";
|
|
|
|
if (url) {
|
2020-06-26 15:33:35 +02:00
|
|
|
let key = '__express__' + req.query.url;
|
|
|
|
mcache.del(key);
|
2021-07-14 17:41:12 +02:00
|
|
|
message = "Delete entry with key " + url;
|
2020-06-26 15:33:35 +02:00
|
|
|
entries.set(mcache.size());
|
2021-07-14 17:41:12 +02:00
|
|
|
} else {
|
2020-06-26 15:33:35 +02:00
|
|
|
clearCache();
|
2021-07-14 17:41:12 +02:00
|
|
|
message = "Delete " + c + " entries. Now there are: " + mcache.size()
|
|
|
|
}
|
2020-06-26 15:33:35 +02:00
|
|
|
res.header("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length");
|
|
|
|
res.header("Access-Control-Allow-Methods", "GET, OPTIONS");
|
|
|
|
res.header("Access-Control-Allow-Methods", "GET, OPTIONS");
|
|
|
|
res.header("Content-Type", "application/json");
|
2021-07-14 17:41:12 +02:00
|
|
|
res.status(200).send(getResponse(200, message));
|
2017-10-26 17:33:59 +02:00
|
|
|
|
2020-06-26 15:33:35 +02:00
|
|
|
});
|
2020-03-11 13:51:10 +01:00
|
|
|
app.get('/metrics', (req, res) => {
|
|
|
|
res.set('Content-Type', register.contentType);
|
|
|
|
res.end(register.metrics());
|
|
|
|
});
|
2017-10-26 17:33:59 +02:00
|
|
|
|
2020-06-26 15:33:35 +02:00
|
|
|
app.get('/get', cache(), cors(), (req, res) => {
|
2020-03-11 13:51:10 +01:00
|
|
|
setTimeout(() => {
|
|
|
|
const url = (req.query) ? req.query.url : null;
|
|
|
|
if (!url) {
|
|
|
|
res.status(404).send(getResponse(404, "Not Found ")) //not found
|
|
|
|
} else {
|
2020-04-15 15:53:37 +02:00
|
|
|
request.get(url, function (err, response) {
|
2020-03-11 13:51:10 +01:00
|
|
|
// res.header("Access-Control-Allow-Origin", "http://localhost:3000");
|
|
|
|
res.header("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length");
|
|
|
|
res.header("Access-Control-Allow-Methods", "GET, OPTIONS");
|
|
|
|
res.header("Access-Control-Allow-Methods", "GET, OPTIONS");
|
|
|
|
res.header("Content-Type", "application/json");
|
|
|
|
if (!response && err) {
|
|
|
|
res.status(500).send(getResponse(500, "An error occurred for " + url))
|
|
|
|
} else {
|
|
|
|
res.status(response.status).send(response.body);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-10-26 17:33:59 +02:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2020-06-26 15:33:35 +02:00
|
|
|
|
2017-10-26 17:33:59 +02:00
|
|
|
app.use((req, res) => {
|
2020-03-11 13:51:10 +01:00
|
|
|
res.status(404).send(getResponse(404, "Not Found")); //not found
|
2019-07-24 10:17:05 +02:00
|
|
|
});
|
2017-10-26 17:33:59 +02:00
|
|
|
|
2022-10-03 11:22:13 +02:00
|
|
|
const server = app.listen(properties.get('port'), function () {
|
|
|
|
console.log(`Example app listening on port`, server.address().port)
|
2020-06-26 16:44:28 +02:00
|
|
|
//run the timer
|
|
|
|
resetAtMidnight();
|
2019-07-24 10:17:05 +02:00
|
|
|
});
|
2018-07-13 13:56:03 +02:00
|
|
|
|
2020-03-11 13:51:10 +01:00
|
|
|
function getResponse(code, message) {
|
|
|
|
var response = {};
|
|
|
|
response["code"] = code;
|
|
|
|
response["message"] = message;
|
|
|
|
return response;
|
2018-07-13 13:56:03 +02:00
|
|
|
}
|
2021-07-14 17:41:12 +02:00
|
|
|
|
|
|
|
function clearCache() {
|
2020-06-26 16:44:28 +02:00
|
|
|
console.log("cache is cleared!");
|
2020-06-26 15:33:35 +02:00
|
|
|
mcache.clear();
|
|
|
|
entries.set(mcache.size());
|
|
|
|
}
|
2021-07-14 17:41:12 +02:00
|
|
|
|
|
|
|
function checkForLongCachedRequests(url) {
|
|
|
|
let long = false;
|
2020-06-26 15:33:35 +02:00
|
|
|
longCachingRequests.forEach(partUrl => {
|
2021-07-14 17:41:12 +02:00
|
|
|
if (url.indexOf(partUrl) !== -1) {
|
2020-06-26 15:33:35 +02:00
|
|
|
long = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return long;
|
|
|
|
}
|
2021-07-14 17:41:12 +02:00
|
|
|
|
2020-06-26 15:33:35 +02:00
|
|
|
function resetAtMidnight() {
|
2020-06-26 16:44:28 +02:00
|
|
|
console.log("Run Reset timer");
|
2020-06-26 15:33:35 +02:00
|
|
|
var now = new Date();
|
|
|
|
var night = new Date(
|
|
|
|
now.getFullYear(),
|
|
|
|
now.getMonth(),
|
2021-07-14 17:41:12 +02:00
|
|
|
now.getDate() + 1, // the next day, ...
|
|
|
|
0, 0, 0 // ...at 00:00:00 hours
|
2020-06-26 15:33:35 +02:00
|
|
|
);
|
|
|
|
var msToMidnight = night.getTime() - now.getTime();
|
2021-07-14 17:41:12 +02:00
|
|
|
setTimeout(function () {
|
2020-06-26 15:33:35 +02:00
|
|
|
clearCache(); // <-- This is the function being called at midnight.
|
|
|
|
resetAtMidnight(); // Then, reset again next midnight.
|
|
|
|
}, msToMidnight);
|
|
|
|
}
|