Cache service:
-change console.debug to log -mv resetAtMidnight inside app.listen git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-portal/trunk@59002 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
parent
a9652670b3
commit
cac44d718e
|
@ -41,7 +41,7 @@ let cache = () => {
|
||||||
return (req, res, next) => {
|
return (req, res, next) => {
|
||||||
if(req.query.url) {
|
if(req.query.url) {
|
||||||
if(mcache.size() > cacheMaxSize){
|
if(mcache.size() > cacheMaxSize){
|
||||||
console.debug("Max cache size reached!"+cacheMaxSize);
|
console.log("Max cache size reached!"+cacheMaxSize);
|
||||||
clearCache();
|
clearCache();
|
||||||
}
|
}
|
||||||
let key = '__express__' + req.query.url;
|
let key = '__express__' + req.query.url;
|
||||||
|
@ -129,33 +129,14 @@ app.get('/get', cache(), cors(), (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
app.get('/get', cache(30), cors(), (req, res) => {
|
|
||||||
setTimeout(() => {
|
|
||||||
const url = (req.query) ? req.query.url : null;
|
|
||||||
if (!url) {
|
|
||||||
res.status(404).send(getResponse(404, "Not Found ")) //not found
|
|
||||||
} else {
|
|
||||||
request.get(url, function (err, response) {
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
});
|
|
||||||
app.use((req, res) => {
|
app.use((req, res) => {
|
||||||
res.status(404).send(getResponse(404, "Not Found")); //not found
|
res.status(404).send(getResponse(404, "Not Found")); //not found
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen((process.env.PORT) ? process.env.PORT : 3000, function () {
|
app.listen((process.env.PORT) ? process.env.PORT : 3000, function () {
|
||||||
console.log(`Example app listening on port ${process.env.PORT}!`)
|
console.log(`Example app listening on port ${(process.env.PORT) ? process.env.PORT : 3000}!`)
|
||||||
|
//run the timer
|
||||||
|
resetAtMidnight();
|
||||||
});
|
});
|
||||||
|
|
||||||
function getResponse(code, message) {
|
function getResponse(code, message) {
|
||||||
|
@ -166,7 +147,7 @@ function getResponse(code, message) {
|
||||||
|
|
||||||
}
|
}
|
||||||
function clearCache(){
|
function clearCache(){
|
||||||
console.debug("cache is cleared!");
|
console.log("cache is cleared!");
|
||||||
mcache.clear();
|
mcache.clear();
|
||||||
entries.set(mcache.size());
|
entries.set(mcache.size());
|
||||||
}
|
}
|
||||||
|
@ -179,10 +160,8 @@ function checkForLongCachedRequests(url){
|
||||||
});
|
});
|
||||||
return long;
|
return long;
|
||||||
}
|
}
|
||||||
//run the timer
|
|
||||||
resetAtMidnight();
|
|
||||||
function resetAtMidnight() {
|
function resetAtMidnight() {
|
||||||
console.debug("Run Reset timer");
|
console.log("Run Reset timer");
|
||||||
var now = new Date();
|
var now = new Date();
|
||||||
var night = new Date(
|
var night = new Date(
|
||||||
now.getFullYear(),
|
now.getFullYear(),
|
||||||
|
|
Loading…
Reference in New Issue