forked from programmingclub-knit/KNIT-Result-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (52 loc) · 1.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const axios = require("axios");
const express = require('express');
const app = express()
const {client, getResult_id, getResult_data} = require('./modules/results');
const async = require("async");
var port = process.env.PORT || 3000;
global.results = {}; // global variable for all results
function cache_check(roll,_callback){
console.log("callback"+roll);
client.exists(roll,(err, reply)=>{
if(reply == 0){
console.log("Not in cache");
getResult_id(roll,undefined,_callback);
}
else _callback();
})
}
app.get("/",async (req, res) => {
console.log(req.query.roll);
let roll = req.query.roll;
if(!roll){
return res.status(400).send({message:"Wrong Parameters"});
}
getResult_id(roll, res);
});
app.get("/cache",async (req, res) => {
console.log(req.query.roll);
let roll = req.query.roll;
if(roll == undefined){
return res.status(400).json({message:"Invalid Input"});
}
else{
cache_check(roll, function(){
client.hgetall(roll,(err,reply)=>{
if(reply == null || reply == undefined){
return res.status(200).json({message:"Not available"});
}
async.map(Object.keys(reply),function(key, cb){
client.hgetall(key,(err,rep)=>{
if(err) console.log(err);
cb(null, rep);
})
},(err, resp)=>{
res.json(resp);
})
});
})
}
});
app.listen(port, () => {
console.log(`Server Started at http://localhost:${port}`);
});