app.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* eslint-disable
  2. max-len,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const metrics = require('@overleaf/metrics')
  13. metrics.initialize(process.env.METRICS_APP_NAME || 'web')
  14. const Settings = require('@overleaf/settings')
  15. const logger = require('@overleaf/logger')
  16. const PlansLocator = require('./app/src/Features/Subscription/PlansLocator')
  17. logger.initialize(process.env.METRICS_APP_NAME || 'web')
  18. logger.logger.serializers.user =
  19. require('./app/src/infrastructure/LoggerSerializers').user
  20. logger.logger.serializers.docs =
  21. require('./app/src/infrastructure/LoggerSerializers').docs
  22. logger.logger.serializers.files =
  23. require('./app/src/infrastructure/LoggerSerializers').files
  24. logger.logger.serializers.project =
  25. require('./app/src/infrastructure/LoggerSerializers').project
  26. if ((Settings.sentry != null ? Settings.sentry.dsn : undefined) != null) {
  27. logger.initializeErrorReporting(Settings.sentry.dsn)
  28. }
  29. const http = require('http')
  30. const https = require('https')
  31. http.globalAgent.maxSockets = Settings.limits.httpGlobalAgentMaxSockets
  32. https.globalAgent.maxSockets = Settings.limits.httpsGlobalAgentMaxSockets
  33. metrics.memory.monitor(logger)
  34. const Server = require('./app/src/infrastructure/Server')
  35. const QueueWorkers = require('./app/src/infrastructure/QueueWorkers')
  36. const mongodb = require('./app/src/infrastructure/mongodb')
  37. const mongoose = require('./app/src/infrastructure/Mongoose')
  38. if (Settings.catchErrors) {
  39. process.removeAllListeners('uncaughtException')
  40. process.on('uncaughtException', error =>
  41. logger.error({ err: error }, 'uncaughtException')
  42. )
  43. }
  44. const port = Settings.port || Settings.internal.web.port || 3000
  45. const host = Settings.internal.web.host || 'localhost'
  46. if (!module.parent) {
  47. // Called directly
  48. // We want to make sure that we provided a password through the environment.
  49. if (!process.env.WEB_API_USER || !process.env.WEB_API_PASSWORD) {
  50. throw new Error('No API user and password provided')
  51. }
  52. PlansLocator.ensurePlansAreSetupCorrectly()
  53. Promise.all([mongodb.waitForDb(), mongoose.connectionPromise])
  54. .then(() => {
  55. Server.server.listen(port, host, function () {
  56. logger.debug(`web starting up, listening on ${host}:${port}`)
  57. logger.debug(
  58. `${require('http').globalAgent.maxSockets} sockets enabled`
  59. )
  60. // wait until the process is ready before monitoring the event loop
  61. metrics.event_loop.monitor(logger)
  62. })
  63. QueueWorkers.start()
  64. })
  65. .catch(err => {
  66. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  67. process.exit(1)
  68. })
  69. }
  70. // handle SIGTERM for graceful shutdown in kubernetes
  71. process.on('SIGTERM', function (signal) {
  72. logger.warn({ signal }, 'received signal, shutting down')
  73. Settings.shuttingDown = true
  74. })
  75. module.exports = Server.server