api.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. const CoreClass = require("../core.js");
  2. const async = require("async");
  3. const crypto = require("crypto");
  4. class APIModule extends CoreClass {
  5. constructor() {
  6. super("api");
  7. }
  8. initialize() {
  9. return new Promise((resolve, reject) => {
  10. this.app = this.moduleManager.modules["app"];
  11. this.stations = this.moduleManager.modules["stations"];
  12. this.db = this.moduleManager.modules["db"];
  13. this.cache = this.moduleManager.modules["cache"];
  14. this.notifications = this.moduleManager.modules["notifications"];
  15. const actions = require("./actions");
  16. this.app.runJob("GET_APP", {})
  17. .then((response) => {
  18. response.app.get("/", (req, res) => {
  19. res.json({
  20. status: "success",
  21. message: "Coming Soon",
  22. });
  23. });
  24. response.app.get("/debug_station", async (req, res) => {
  25. const responseObject = {};
  26. const stationModel = await this.db.runJob(
  27. "GET_MODEL",
  28. {
  29. modelName: "station",
  30. }
  31. );
  32. async.waterfall([
  33. next => {
  34. stationModel.find({}, next);
  35. },
  36. (stations, next) => {
  37. responseObject.mongo = {
  38. stations
  39. };
  40. next();
  41. },
  42. next => {
  43. this.cache
  44. .runJob("HGETALL", { table: "stations" })
  45. .then(stations => next(null, stations))
  46. .catch(next);
  47. },
  48. (stations, next) => {
  49. responseObject.redis = {
  50. stations
  51. };
  52. next();
  53. },
  54. next => {
  55. responseObject.cryptoExamples = {};
  56. responseObject.mongo.stations.forEach(station => {
  57. const payloadName = `stations.nextSong?id=${station._id}`;
  58. responseObject.cryptoExamples[station._id] = crypto
  59. .createHash("md5")
  60. .update(`_notification:${payloadName}_`)
  61. .digest("hex")
  62. });
  63. next();
  64. },
  65. next => {
  66. this.notifications.pub.keys("*", next);
  67. },
  68. (redisKeys, next) => {
  69. responseObject.redis = {
  70. ...redisKeys,
  71. ttl: {}
  72. };
  73. async.eachLimit(redisKeys, 1, (redisKey, next) => {
  74. this.notifications.pub.ttl(redisKey, (err, ttl) => {
  75. responseObject.redis.ttl[redisKey] = ttl;
  76. next(err);
  77. })
  78. }, next);
  79. },
  80. next => {
  81. responseObject.debugLogs = this.moduleManager.debugLogs.stationIssue;
  82. next();
  83. },
  84. next => {
  85. responseObject.debugJobs = this.moduleManager.debugJobs;
  86. next();
  87. }
  88. ], (err, response) => {
  89. if (err) {
  90. console.log(err);
  91. return res.json({
  92. error: err,
  93. objectSoFar: responseObject
  94. });
  95. }
  96. res.json(responseObject);
  97. });
  98. });
  99. // Object.keys(actions).forEach(namespace => {
  100. // Object.keys(actions[namespace]).forEach(action => {
  101. // let name = `/${namespace}/${action}`;
  102. // response.app.get(name, (req, res) => {
  103. // actions[namespace][action](null, result => {
  104. // if (typeof cb === "function")
  105. // return res.json(result);
  106. // });
  107. // });
  108. // });
  109. // });
  110. resolve();
  111. })
  112. .catch((err) => {
  113. reject(err);
  114. });
  115. });
  116. }
  117. }
  118. module.exports = new APIModule();