2017-10-26 17:33:59 +02:00
|
|
|
const redis = require('redis');
|
|
|
|
const express = require('express');
|
|
|
|
const request = require('superagent');
|
|
|
|
const PORT = process.env.PORT;
|
|
|
|
const REDIS_PORT = process.env.REDIS_PORT;
|
2017-10-27 13:59:57 +02:00
|
|
|
const REDIS_HOST = process.env.REDIS_HOST;
|
2017-10-26 17:33:59 +02:00
|
|
|
|
|
|
|
const app = express();
|
2017-10-27 13:59:57 +02:00
|
|
|
const client = redis.createClient(REDIS_PORT, REDIS_HOST);
|
2017-10-26 17:33:59 +02:00
|
|
|
|
2017-10-27 13:59:57 +02:00
|
|
|
// function respond(org, numberOfRepos) {
|
|
|
|
// return `Organization "${org}" has ${numberOfRepos} public repositories.`;
|
|
|
|
// }
|
2017-10-26 17:33:59 +02:00
|
|
|
|
|
|
|
function cache(req, res, next) {
|
|
|
|
const url = req.query.url;
|
|
|
|
client.get(url, function (err, data) {
|
|
|
|
if (data != null) {
|
2017-10-27 13:59:57 +02:00
|
|
|
res.send(data);
|
2017-10-26 17:33:59 +02:00
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// function getNumberOfRepos(req, res, next) {
|
|
|
|
// const org = req.query.org;
|
|
|
|
// request.get(`https://api.github.com/orgs/${org}/repos`, function (err, response) {
|
|
|
|
// if (err) {
|
|
|
|
// throw err;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// var repoNumber = 0;
|
|
|
|
// if (response && response.body) {
|
|
|
|
// repoNumber = response.body.length;
|
|
|
|
// }
|
|
|
|
// client.setex(org, 5, repoNumber);
|
|
|
|
// res.send(respond(org, repoNumber));
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// app.get('/repos', cache, getNumberOfRepos);
|
|
|
|
app.get('/get',cache, (req, res) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
const url = (req.query)?req.query.url:null;
|
|
|
|
if (!url){
|
|
|
|
res.status(404).send('Not Found'); //not found
|
|
|
|
|
|
|
|
}else{
|
|
|
|
request.get(url, function (err, response) {
|
|
|
|
if (err) throw err;
|
|
|
|
|
|
|
|
// response.body contains an array of public repositories
|
|
|
|
// var repoNumber = response.body.length;
|
2017-10-27 13:59:57 +02:00
|
|
|
res.send(response.body);
|
2017-10-26 17:33:59 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
app.listen(PORT, function () {
|
|
|
|
console.log('app listening on port', PORT);
|
|
|
|
});
|