2019-07-24 10:17:05 +02:00
|
|
|
'use strict';
|
2024-02-05 11:08:34 +01:00
|
|
|
const axios = require('axios');
|
2019-07-24 10:17:05 +02:00
|
|
|
let express = require('express');
|
|
|
|
let app = express();
|
2024-02-08 14:58:43 +01:00
|
|
|
const { LRUCache } = require('lru-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');
|
2024-04-03 09:20:58 +02:00
|
|
|
let compression = require("compression");
|
2024-02-08 14:58:43 +01:00
|
|
|
const PropertiesReader = require('properties-reader');
|
|
|
|
const properties = PropertiesReader('./properties.file');
|
|
|
|
const environment = properties.get("environment");
|
|
|
|
const preloadRequests = properties.get("preloadRequests").split(',');;
|
2024-07-15 10:37:16 +02:00
|
|
|
var accesslog = require('access-log');
|
2024-02-08 14:58:43 +01:00
|
|
|
|
2024-02-12 12:29:23 +01:00
|
|
|
const cacheMaxSize = 1000;
|
2020-03-11 13:51:10 +01:00
|
|
|
let cors = require('cors');
|
|
|
|
app.use(cors());
|
2024-04-03 09:20:58 +02:00
|
|
|
app.use(compression());
|
2024-02-08 14:58:43 +01:00
|
|
|
const lruCache = new LRUCache({ max: cacheMaxSize });
|
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) {
|
2024-04-03 09:20:58 +02:00
|
|
|
let key = req.query.url;
|
2020-05-14 18:48:40 +02:00
|
|
|
const url = new URL.parse(req.query.url);
|
2024-04-03 09:20:58 +02:00
|
|
|
// console.log(req.headers.origin, req.headers.referrer, req.headers.origin)
|
|
|
|
const cacheControlHeader = req.headers['cache-control'];
|
|
|
|
// Log the Cache-Control header
|
|
|
|
// console.log('Cache-Control header sent by client:', cacheControlHeader);
|
|
|
|
let forceReload = req.query.forceReload && req.query.forceReload == 'true'?true:false;
|
|
|
|
forceReload = forceReload || (cacheControlHeader && (cacheControlHeader.indexOf("no-cache") || cacheControlHeader.indexOf("no-store") || cacheControlHeader.indexOf("must-revalidate")))?true:false;
|
|
|
|
|
2020-04-15 15:53:37 +02:00
|
|
|
const target = url.host + '/' + url.pathname.split('/')[1];
|
|
|
|
const scheme = url.protocol.replace(':', '');
|
2024-04-03 09:20:58 +02:00
|
|
|
if (lruCache.has(key) && !forceReload) {
|
|
|
|
// console.log( key, "hit")
|
2020-04-15 15:53:37 +02:00
|
|
|
const end = histogram.startTimer({scheme: scheme, target: target, cache: 'hit'});
|
2024-02-08 14:58:43 +01:00
|
|
|
res.send(JSON.parse(lruCache.get(key)));
|
2020-04-15 15:53:37 +02:00
|
|
|
responses.inc({scheme: scheme, target: target, code: res.statusCode});
|
|
|
|
end();
|
|
|
|
} else {
|
2024-04-03 09:20:58 +02:00
|
|
|
// console.log( key, "miss", forceReload)
|
2024-02-08 14:58:43 +01:00
|
|
|
const end = histogram.startTimer({scheme: scheme, target: target, cache: 'miss'});
|
|
|
|
res.sendResponse = res.send;
|
|
|
|
res.send = (body) => {
|
2024-02-12 10:13:38 +01:00
|
|
|
if(isAllowedToBeCached(decodeURI(scheme), decodeURI(target))) {
|
2024-02-08 14:58:43 +01:00
|
|
|
let alreadyCached = lruCache.has(key);
|
|
|
|
entries.set(lruCache.size);
|
|
|
|
if (!alreadyCached) {
|
|
|
|
responses.inc({scheme: scheme, target: target, code: res.statusCode});
|
|
|
|
end();
|
|
|
|
}
|
|
|
|
if (res.statusCode === 200) {
|
|
|
|
lruCache.set(key, body);
|
|
|
|
entries.set(lruCache.size);
|
|
|
|
}
|
|
|
|
res.sendResponse(body);
|
|
|
|
}else{
|
|
|
|
res.statusCode = 405;
|
|
|
|
res.sendResponse(JSON.stringify( {code: 405, message: "Method not Allowed"}));
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2024-07-15 10:37:16 +02:00
|
|
|
accesslogCustomFormat(req, res);
|
2020-04-15 15:53:37 +02:00
|
|
|
next();
|
|
|
|
}
|
2020-03-11 13:51:10 +01:00
|
|
|
} else {
|
2024-07-15 10:37:16 +02:00
|
|
|
accesslogCustomFormat(req, res);
|
2020-03-11 13:51:10 +01:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
};
|
2020-03-11 13:53:14 +01:00
|
|
|
};
|
2024-02-08 14:58:43 +01:00
|
|
|
function isAllowedToBeCached(scheme, target){
|
|
|
|
if(environment != "development"){
|
2024-04-03 09:20:58 +02:00
|
|
|
return scheme.indexOf("https")!=-1 && ( target.indexOf(".openaire.eu/") !=-1 || target.indexOf("zenodo.org/api") !=-1 || target.indexOf("lab.idiap.ch/enermaps" != -1))
|
2024-02-08 14:58:43 +01:00
|
|
|
} else if(environment == "development"){
|
2024-04-03 09:20:58 +02:00
|
|
|
return target.indexOf(".openaire.eu/") !=-1 || target.indexOf(".di.uoa.gr") !=-1 || target.indexOf("zenodo.org/api") !=-1 || target.indexOf("dev-openaire.d4science.org") !=-1 || target.indexOf("lab.idiap.ch/enermaps") != -1
|
2024-02-08 14:58:43 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-26 15:33:35 +02:00
|
|
|
app.get('/clear', (req, res) => {
|
2024-02-08 14:58:43 +01:00
|
|
|
let c = lruCache.size;
|
2020-06-26 15:33:35 +02:00
|
|
|
const url = req.query.url;
|
2021-07-14 17:41:12 +02:00
|
|
|
let message = "";
|
|
|
|
if (url) {
|
2024-07-19 10:13:54 +02:00
|
|
|
let key = req.query.url;
|
2024-02-08 14:58:43 +01:00
|
|
|
lruCache.delete(key);
|
2021-07-14 17:41:12 +02:00
|
|
|
message = "Delete entry with key " + url;
|
2024-02-08 14:58:43 +01:00
|
|
|
entries.set(lruCache.size);
|
2021-07-14 17:41:12 +02:00
|
|
|
} else {
|
2020-06-26 15:33:35 +02:00
|
|
|
clearCache();
|
2024-02-08 14:58:43 +01:00
|
|
|
message = "Delete " + c + " entries. Now there are: " + lruCache.size
|
2021-07-14 17:41:12 +02:00
|
|
|
}
|
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");
|
2024-07-15 10:37:16 +02:00
|
|
|
accesslogCustomFormat(req, res);
|
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);
|
2024-07-15 10:37:16 +02:00
|
|
|
accesslogCustomFormat(req, res);
|
2020-03-11 13:51:10 +01:00
|
|
|
res.end(register.metrics());
|
|
|
|
});
|
2017-10-26 17:33:59 +02:00
|
|
|
|
2024-02-15 11:13:04 +01:00
|
|
|
app.get('/info', (req, res) => {
|
2024-07-15 10:37:16 +02:00
|
|
|
accesslogCustomFormat(req, res);
|
2024-02-15 11:13:04 +01:00
|
|
|
res.status(200).send(getResponse(200, {size:lruCache.size, keys: Array.from(lruCache.keys())}));
|
|
|
|
});
|
|
|
|
|
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-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();
|
2024-02-05 11:08:34 +01:00
|
|
|
initCache();
|
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!");
|
2024-02-08 14:58:43 +01:00
|
|
|
lruCache.clear();
|
|
|
|
entries.set(lruCache.size);
|
2024-02-05 11:08:34 +01:00
|
|
|
initCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function initCache() {
|
|
|
|
try {
|
2024-02-08 14:58:43 +01:00
|
|
|
const requests = await axios.get(properties.get('utilsService') + '/grouped-requests');
|
|
|
|
const additionalDataPromises = requests.data.map((url) => axios.get('http://localhost:'+properties.get('port') + '/get?url=' + properties.get('utilsService') + url));
|
|
|
|
const additionalDataResponses = await Promise.all(additionalDataPromises);
|
|
|
|
console.log("Cache initialized group queries!")
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error fetching data: Cache initialize failed', error);
|
|
|
|
}
|
|
|
|
try{
|
|
|
|
const additionalDataPromisesPreloadRequests = preloadRequests.map((url) => axios.get('http://localhost:'+properties.get('port') + '/get?url=' + url));
|
|
|
|
const additionalDataResponsesPreloadRequests = await Promise.all(additionalDataPromisesPreloadRequests);
|
|
|
|
console.log("Cache initialized preload requests!")
|
2024-02-05 11:08:34 +01:00
|
|
|
} catch (error) {
|
2024-02-08 14:58:43 +01:00
|
|
|
console.error('Error fetching data: Cache initialize failed', error);
|
2024-02-05 11:08:34 +01:00
|
|
|
}
|
2020-06-26 15:33:35 +02:00
|
|
|
}
|
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);
|
|
|
|
}
|
2024-07-15 10:37:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
function accesslogCustomFormat(req, res){
|
|
|
|
accesslog(req, res,
|
|
|
|
/*{
|
|
|
|
userID: function (req) { return req.user; },
|
|
|
|
format : 'url=":url" method=":method" statusCode=":statusCode" delta=":delta" ip=":ip"'
|
|
|
|
}*/);
|
|
|
|
// url="/clear" method="GET" statusCode="200" delta="2" ip="::ffff:195.134.66.178"
|
|
|
|
|
|
|
|
// ::ffff:195.134.66.178 - - [15/Jul/2024:11:24:35 +0300] "GET /clear HTTP/1.1" 200 59 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/113.0"
|
|
|
|
|
|
|
|
}
|