index.js 2.3 KB

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