index.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. process.env.NODE_CONFIG_DIR = `${__dirname}/config`;
  3. const async = require('async');
  4. const fs = require('fs');
  5. const db = require('./logic/db');
  6. const app = require('./logic/app');
  7. const mail = require('./logic/mail');
  8. const api = require('./logic/api');
  9. const io = require('./logic/io');
  10. const stations = require('./logic/stations');
  11. const songs = require('./logic/songs');
  12. const playlists = require('./logic/playlists');
  13. const cache = require('./logic/cache');
  14. const notifications = require('./logic/notifications');
  15. const logger = require('./logic/logger');
  16. const config = require('config');
  17. process.on('uncaughtException', err => {
  18. //console.log(`ERROR: ${err.message}`);
  19. console.log(`ERROR: ${err.stack}`);
  20. });
  21. async.waterfall([
  22. // setup our Redis cache
  23. (next) => {
  24. cache.init(config.get('redis').url, () => {
  25. next();
  26. });
  27. },
  28. // setup our MongoDB database
  29. (next) => db.init(config.get("mongo").url, next),
  30. // setup the express server
  31. (next) => app.init(next),
  32. // setup the mail
  33. (next) => mail.init(next),
  34. // setup the socket.io server (all client / server communication is done over this)
  35. (next) => io.init(next),
  36. // setup the notifications
  37. (next) => notifications.init(config.get('redis').url, next),
  38. // setup the stations
  39. (next) => stations.init(next),
  40. // setup the songs
  41. (next) => songs.init(next),
  42. // setup the playlists
  43. (next) => playlists.init(next),
  44. // setup the API
  45. (next) => api.init(next),
  46. // setup the logger
  47. (next) => logger.init(next),
  48. // setup the frontend for local setups
  49. (next) => {
  50. if (!config.get("isDocker")) {
  51. const express = require('express');
  52. const app = express();
  53. app.listen(80);
  54. const rootDir = __dirname.substr(0, __dirname.lastIndexOf("backend")) + "frontend\\build\\";
  55. app.get("/*", (req, res) => {
  56. const path = req.path;
  57. fs.access(rootDir + path, function(err) {
  58. if (!err) {
  59. res.sendFile(rootDir + path);
  60. } else {
  61. res.sendFile(rootDir + "index.html");
  62. }
  63. });
  64. });
  65. }
  66. next();
  67. }
  68. ], (err) => {
  69. if (err && err !== true) {
  70. console.error('An error occurred while initializing the backend server');
  71. console.error(err);
  72. process.exit();
  73. } else {
  74. console.info('Backend server has been successfully started');
  75. }
  76. });