app.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * decaffeinate suggestions:
  3. * DS102: Remove unnecessary code created because of implicit returns
  4. * DS103: Rewrite code to no longer use __guard__
  5. * DS207: Consider shorter variations of null checks
  6. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  7. */
  8. const logger = require('logger-sharelatex')
  9. const settings = require('@overleaf/settings')
  10. const mongodb = require('./app/js/mongodb')
  11. const Server = require('./app/js/server')
  12. if (!module.parent) {
  13. // Called directly
  14. const port =
  15. __guard__(
  16. settings.internal != null ? settings.internal.chat : undefined,
  17. x => x.port
  18. ) || 3010
  19. const host =
  20. __guard__(
  21. settings.internal != null ? settings.internal.chat : undefined,
  22. x1 => x1.host
  23. ) || 'localhost'
  24. mongodb
  25. .waitForDb()
  26. .then(() => {
  27. Server.server.listen(port, host, function (err) {
  28. if (err) {
  29. logger.fatal({ err }, `Cannot bind to ${host}:${port}. Exiting.`)
  30. process.exit(1)
  31. }
  32. return logger.info(`Chat starting up, listening on ${host}:${port}`)
  33. })
  34. })
  35. .catch(err => {
  36. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  37. process.exit(1)
  38. })
  39. }
  40. module.exports = Server.server
  41. function __guard__(value, transform) {
  42. return typeof value !== 'undefined' && value !== null
  43. ? transform(value)
  44. : undefined
  45. }