app.coffee 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. metrics = require("metrics-sharelatex")
  7. metrics.initialize("web")
  8. Server = require("./app/js/infrastructure/Server")
  9. Errors = require "./app/js/errors"
  10. argv = require("optimist")
  11. .options("user", {alias : "u", description : "Run the server with permissions of the specified user"})
  12. .options("group", {alias : "g", description : "Run the server with permissions of the specified group"})
  13. .usage("Usage: $0")
  14. .argv
  15. Server.app.use (error, req, res, next) ->
  16. logger.error err: error, url:req.url, method:req.method, user:req?.sesson?.user, "error passed to top level next middlewear"
  17. res.statusCode = error.status or 500
  18. if res.statusCode == 500
  19. res.end("Oops, something went wrong with your request, sorry. If this continues, please contact us at #{Settings.adminEmail}")
  20. else
  21. res.end()
  22. if Settings.catchErrors
  23. # fairy cleans then exits on an uncaughtError, but we don't want
  24. # to exit so it doesn't need to do this.
  25. require "fairy"
  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}"