index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. 'use strict';
  2. process.env.NODE_CONFIG_DIR = `${__dirname}/config`;
  3. const async = require('async');
  4. const fs = require('fs');
  5. const Discord = require("discord.js");
  6. const client = new Discord.Client();
  7. const db = require('./logic/db');
  8. const app = require('./logic/app');
  9. const mail = require('./logic/mail');
  10. const api = require('./logic/api');
  11. const io = require('./logic/io');
  12. const stations = require('./logic/stations');
  13. const songs = require('./logic/songs');
  14. const playlists = require('./logic/playlists');
  15. const cache = require('./logic/cache');
  16. const notifications = require('./logic/notifications');
  17. const logger = require('./logic/logger');
  18. const tasks = require('./logic/tasks');
  19. const config = require('config');
  20. let currentComponent;
  21. process.on('uncaughtException', err => {
  22. //console.log(`ERROR: ${err.message}`);
  23. console.log(`ERROR: ${err.stack}`);
  24. });
  25. client.on('ready', () => {
  26. discordClientCBS.forEach((cb) => {
  27. cb();
  28. });
  29. console.log(`DISCORD Logged in as ${client.user.username}!`);
  30. //this.logToDiscord("Lost connection to MongoDB. Crashing server.", "Database error", true, [{name: 'Error', value: 'MONGOERR: ERRCNNCT 127.0.0.1 FAILED TO CONNECT', inline: false}], (err) => {console.log(err);});
  31. });
  32. client.login(config.get('apis.discord.token'));
  33. let discordClientCBS = [];
  34. const getDiscordClient = (cb) => {
  35. if (client.status === 0) return cb();
  36. else discordClientCBS.push(cb);
  37. };
  38. const logToDiscord = (message, color, type, critical, extraFields, cb = ()=>{}) => {
  39. getDiscordClient(() => {
  40. let richEmbed = new Discord.RichEmbed();
  41. richEmbed.setAuthor("Musare Logger", "https://musare.com/favicon-194x194.png", "https://musare.com");
  42. richEmbed.setColor(color);
  43. richEmbed.setDescription(message);
  44. //richEmbed.setFooter("Footer", "https://musare.com/favicon-194x194.png");
  45. //richEmbed.setImage("https://musare.com/favicon-194x194.png");
  46. //richEmbed.setThumbnail("https://musare.com/favicon-194x194.png");
  47. richEmbed.setTimestamp(new Date());
  48. richEmbed.setTitle("MUSARE ALERT");
  49. richEmbed.setURL("https://musare.com");
  50. richEmbed.addField("Type:", type, true);
  51. richEmbed.addField("Critical:", (critical) ? 'True' : 'False', true);
  52. extraFields.forEach((extraField) => {
  53. richEmbed.addField(extraField.name, extraField.value, extraField.inline);
  54. });
  55. /*client.channels.get(config.get('apis.discord.loggingChannel')).sendEmbed(richEmbed).then(() => {
  56. cb();
  57. }).then((reason) => {
  58. cb(reason);
  59. });*/
  60. });
  61. };
  62. async.waterfall([
  63. // setup our Redis cache
  64. (next) => {
  65. currentComponent = 'Cache';
  66. cache.init(config.get('redis').url, config.get('redis').password, () => {
  67. next();
  68. });
  69. },
  70. // setup our MongoDB database
  71. (next) => {
  72. currentComponent = 'DB';
  73. db.init(config.get("mongo").url, next);
  74. },
  75. // setup the express server
  76. (next) => {
  77. currentComponent = 'App';
  78. app.init(next);
  79. },
  80. // setup the mail
  81. (next) => {
  82. currentComponent = 'Mail';
  83. mail.init(next);
  84. },
  85. // setup the socket.io server (all client / server communication is done over this)
  86. (next) => {
  87. currentComponent = 'IO';
  88. io.init(next);
  89. },
  90. // setup the notifications
  91. (next) => {
  92. currentComponent = 'Notifications';
  93. notifications.init(config.get('redis').url, config.get('redis').password, next);
  94. },
  95. // setup the stations
  96. (next) => {
  97. currentComponent = 'Stations';
  98. stations.init(next)
  99. },
  100. // setup the songs
  101. (next) => {
  102. currentComponent = 'Songs';
  103. songs.init(next)
  104. },
  105. // setup the playlists
  106. (next) => {
  107. currentComponent = 'Playlists';
  108. playlists.init(next)
  109. },
  110. // setup the API
  111. (next) => {
  112. currentComponent = 'API';
  113. api.init(next)
  114. },
  115. // setup the logger
  116. (next) => {
  117. currentComponent = 'Logger';
  118. logger.init(next)
  119. },
  120. // setup the tasks system
  121. (next) => {
  122. currentComponent = 'Tasks';
  123. tasks.init(next)
  124. },
  125. // setup the frontend for local setups
  126. (next) => {
  127. currentComponent = 'Windows';
  128. if (!config.get("isDocker")) {
  129. const express = require('express');
  130. const app = express();
  131. app.listen(80);
  132. const rootDir = __dirname.substr(0, __dirname.lastIndexOf("backend")) + "frontend\\build\\";
  133. app.get("/*", (req, res) => {
  134. const path = req.path;
  135. fs.access(rootDir + path, function(err) {
  136. if (!err) {
  137. res.sendFile(rootDir + path);
  138. } else {
  139. res.sendFile(rootDir + "index.html");
  140. }
  141. });
  142. });
  143. }
  144. next();
  145. }
  146. ], (err) => {
  147. if (err && err !== true) {
  148. logToDiscord("An error occurred while initializing the backend server.", "#FF0000", "Startup error", true, [{name: "Error:", value: err, inline: false}, {name: "Component:", value: currentComponent, inline: true}])
  149. console.error('An error occurred while initializing the backend server');
  150. console.error(err);
  151. process.exit();
  152. } else {
  153. logToDiscord("The backend server started successfully.", "#00AA00", "Startup", false);
  154. console.info('Backend server has been successfully started');
  155. }
  156. });