app.js 3.0 KB

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