app.coffee 1.7 KB

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