app.coffee 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. Settings = require('settings-sharelatex')
  2. logger = require 'logger-sharelatex'
  3. logger.initialize("web-sharelatex")
  4. logger.logger.serializers.user = require("./app/js/infrastructure/LoggerSerializers").user
  5. logger.logger.serializers.project = require("./app/js/infrastructure/LoggerSerializers").project
  6. if Settings.sentry?.dsn?
  7. logger.initializeErrorReporting(Settings.sentry.dsn)
  8. metrics = require("metrics-sharelatex")
  9. metrics.initialize("web")
  10. metrics.memory.monitor(logger)
  11. Server = require("./app/js/infrastructure/Server")
  12. Errors = require "./app/js/errors"
  13. argv = require("optimist")
  14. .options("user", {alias : "u", description : "Run the server with permissions of the specified user"})
  15. .options("group", {alias : "g", description : "Run the server with permissions of the specified group"})
  16. .usage("Usage: $0")
  17. .argv
  18. Server.app.use (error, req, res, next) ->
  19. logger.error err: error, url:req.url, method:req.method, user:req?.sesson?.user, "error passed to top level next middlewear"
  20. res.statusCode = error.status or 500
  21. if res.statusCode == 500
  22. res.end("Oops, something went wrong with your request, sorry. If this continues, please contact us at #{Settings.adminEmail}")
  23. else
  24. res.end()
  25. if Settings.catchErrors
  26. process.removeAllListeners "uncaughtException"
  27. process.on "uncaughtException", (error) ->
  28. logger.error err: error, "uncaughtException"
  29. port = Settings.port or Settings.internal?.web?.port or 3000
  30. host = Settings.internal.web.host or "localhost"
  31. Server.server.listen port, host, ->
  32. logger.info "web starting up, listening on #{host}:#{port}"
  33. logger.info("#{require('http').globalAgent.maxSockets} sockets enabled")
  34. if argv.user
  35. process.setuid argv.user
  36. logger.info "Running as user: #{argv.user}"
  37. if argv.group
  38. process.setgid argv.group
  39. logger.info "Running as group: #{argv.group}"