app.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 mongodb = require('./app/src/infrastructure/mongodb')
  32. const mongoose = require('./app/src/infrastructure/Mongoose')
  33. if (Settings.catchErrors) {
  34. process.removeAllListeners('uncaughtException')
  35. process.on('uncaughtException', error =>
  36. logger.error({ err: error }, 'uncaughtException')
  37. )
  38. }
  39. const port = Settings.port || Settings.internal.web.port || 3000
  40. const host = Settings.internal.web.host || 'localhost'
  41. if (!module.parent) {
  42. // Called directly
  43. // We want to make sure that we provided a password through the environment.
  44. if (!process.env.WEB_API_USER || !process.env.WEB_API_PASSWORD) {
  45. throw new Error('No API user and password provided')
  46. }
  47. PlansLocator.ensurePlansAreSetupCorrectly()
  48. Promise.all([mongodb.waitForDb(), mongoose.connectionPromise])
  49. .then(() => {
  50. Server.server.listen(port, host, function () {
  51. logger.info(`web starting up, listening on ${host}:${port}`)
  52. logger.info(`${require('http').globalAgent.maxSockets} sockets enabled`)
  53. // wait until the process is ready before monitoring the event loop
  54. metrics.event_loop.monitor(logger)
  55. })
  56. })
  57. .catch(err => {
  58. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  59. process.exit(1)
  60. })
  61. }
  62. // handle SIGTERM for graceful shutdown in kubernetes
  63. process.on('SIGTERM', function (signal) {
  64. logger.warn({ signal: signal }, 'received signal, shutting down')
  65. Settings.shuttingDown = true
  66. })
  67. module.exports = Server.server