app.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. const SiteAdminHandler = require('./app/src/infrastructure/SiteAdminHandler')
  18. logger.initialize(process.env.METRICS_APP_NAME || 'web')
  19. logger.logger.serializers.user =
  20. require('./app/src/infrastructure/LoggerSerializers').user
  21. logger.logger.serializers.docs =
  22. require('./app/src/infrastructure/LoggerSerializers').docs
  23. logger.logger.serializers.files =
  24. require('./app/src/infrastructure/LoggerSerializers').files
  25. logger.logger.serializers.project =
  26. require('./app/src/infrastructure/LoggerSerializers').project
  27. if ((Settings.sentry != null ? Settings.sentry.dsn : undefined) != null) {
  28. logger.initializeErrorReporting(Settings.sentry.dsn)
  29. }
  30. const http = require('http')
  31. const https = require('https')
  32. http.globalAgent.maxSockets = Settings.limits.httpGlobalAgentMaxSockets
  33. https.globalAgent.maxSockets = Settings.limits.httpsGlobalAgentMaxSockets
  34. metrics.memory.monitor(logger)
  35. const Server = require('./app/src/infrastructure/Server')
  36. const QueueWorkers = require('./app/src/infrastructure/QueueWorkers')
  37. const mongodb = require('./app/src/infrastructure/mongodb')
  38. const mongoose = require('./app/src/infrastructure/Mongoose')
  39. const {
  40. triggerGracefulShutdown,
  41. } = require('./app/src/infrastructure/GracefulShutdown')
  42. if (Settings.catchErrors) {
  43. process.removeAllListeners('uncaughtException')
  44. process.on('uncaughtException', error =>
  45. logger.error({ err: error }, 'uncaughtException')
  46. )
  47. }
  48. const port = Settings.port || Settings.internal.web.port || 3000
  49. const host = Settings.internal.web.host || 'localhost'
  50. if (!module.parent) {
  51. // Called directly
  52. // We want to make sure that we provided a password through the environment.
  53. if (!process.env.WEB_API_USER || !process.env.WEB_API_PASSWORD) {
  54. throw new Error('No API user and password provided')
  55. }
  56. PlansLocator.ensurePlansAreSetupCorrectly()
  57. Promise.all([mongodb.waitForDb(), mongoose.connectionPromise])
  58. .then(() => {
  59. Server.server.listen(port, host, function () {
  60. logger.debug(`web starting up, listening on ${host}:${port}`)
  61. logger.debug(
  62. `${require('http').globalAgent.maxSockets} sockets enabled`
  63. )
  64. // wait until the process is ready before monitoring the event loop
  65. metrics.event_loop.monitor(logger)
  66. })
  67. QueueWorkers.start()
  68. })
  69. .catch(err => {
  70. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  71. process.exit(1)
  72. })
  73. }
  74. // monitor site maintenance file
  75. SiteAdminHandler.initialise()
  76. // handle SIGTERM for graceful shutdown in kubernetes
  77. process.on('SIGTERM', function (signal) {
  78. triggerGracefulShutdown(Server.server, signal)
  79. })
  80. module.exports = Server.server