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.

99 lines
2.2 KiB

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