app.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. * DS103: Rewrite code to no longer use __guard__
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const metrics = require('metrics-sharelatex')
  14. metrics.initialize(process.env['METRICS_APP_NAME'] || 'web')
  15. const Settings = require('settings-sharelatex')
  16. const logger = require('logger-sharelatex')
  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. metrics.memory.monitor(logger)
  26. const Server = require('./app/src/infrastructure/Server')
  27. const { argv } = require('optimist')
  28. .options('user', {
  29. alias: 'u',
  30. description: 'Run the server with permissions of the specified user'
  31. })
  32. .options('group', {
  33. alias: 'g',
  34. description: 'Run the server with permissions of the specified group'
  35. })
  36. .usage('Usage: $0')
  37. if (Settings.catchErrors) {
  38. process.removeAllListeners('uncaughtException')
  39. process.on('uncaughtException', error =>
  40. logger.error({ err: error }, 'uncaughtException')
  41. )
  42. }
  43. const port =
  44. Settings.port ||
  45. __guard__(
  46. Settings.internal != null ? Settings.internal.web : undefined,
  47. x => x.port
  48. ) ||
  49. 3000
  50. const host = Settings.internal.web.host || 'localhost'
  51. if (!module.parent) {
  52. // Called directly
  53. Server.server.listen(port, host, function() {
  54. logger.info(`web starting up, listening on ${host}:${port}`)
  55. logger.info(`${require('http').globalAgent.maxSockets} sockets enabled`)
  56. if (argv.user) {
  57. process.setuid(argv.user)
  58. logger.info(`Running as user: ${argv.user}`)
  59. }
  60. if (argv.group) {
  61. process.setgid(argv.group)
  62. return logger.info(`Running as group: ${argv.group}`)
  63. }
  64. })
  65. }
  66. module.exports = Server.server
  67. function __guard__(value, transform) {
  68. return typeof value !== 'undefined' && value !== null
  69. ? transform(value)
  70. : undefined
  71. }