Website for visualizing a persons github network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.1 KiB

5 years ago
  1. const routes = require('express').Router();
  2. const got = require("got");
  3. const cache = require('memory-cache');
  4. const dotenv = require("dotenv").config();
  5. const GITHUB_API = "https://api.github.com";
  6. const authenticate = `client_id=${process.env.CLIENT_ID}&client_secret=${process.env.CLIENT_SECRET}`;
  7. function queryGitHubAPI(requestURL)
  8. {
  9. const apiData = cache.get(requestURL);
  10. return new Promise(function(reject, resolve)
  11. {
  12. if(apiData == null)
  13. {
  14. var queryURL;
  15. if(requestURL.includes("?page="))
  16. {
  17. queryURL = GITHUB_API + requestURL + "&" + authenticate;
  18. }
  19. else
  20. {
  21. queryURL = GITHUB_API + requestURL + "?" + authenticate;
  22. }
  23. console.log(queryURL);
  24. got(queryURL, { json: true }).then(response =>
  25. {
  26. resolve(response.body);
  27. cache.put(requestURL, response.body);
  28. }).catch(error =>
  29. {
  30. resolve(error);
  31. cache.put(requestURL, error);
  32. });
  33. }
  34. else
  35. {
  36. console.log("Fetched From Cahce");
  37. resolve(apiData);
  38. }
  39. })
  40. }
  41. routes.get('/*', (request, result) =>
  42. {
  43. var gitHubAPIURL = request.url;
  44. result.setHeader('Content-Type', 'application/json');
  45. queryGitHubAPI(gitHubAPIURL).then(function(data)
  46. {
  47. if(data.hasOwnProperty("id") || data[0].hasOwnProperty("id"))
  48. {
  49. result.write(JSON.stringify(data));
  50. }
  51. else
  52. {
  53. result.write("[]");
  54. }
  55. result.end();
  56. }).catch(function(error)
  57. {
  58. try
  59. {
  60. if(error.hasOwnProperty("id") || error[0].hasOwnProperty("id"))
  61. {
  62. result.write(JSON.stringify(error));
  63. }
  64. else
  65. {
  66. result.write("[]");
  67. }
  68. }
  69. catch(error) {
  70. result.write("[]");
  71. };
  72. result.end();
  73. });
  74. if(cache.size() > 50000)
  75. {
  76. cache.clear();
  77. }
  78. });
  79. module.exports = routes;