app.coffee 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. if error?.code is 'EBADCSRFTOKEN'
  20. logger.log err: error,url:req.url, method:req.method, user:req?.sesson?.user, "invalid csrf"
  21. res.sendStatus(403)
  22. return
  23. logger.error err: error, url:req.url, method:req.method, user:req?.sesson?.user, "error passed to top level next middlewear"
  24. res.statusCode = error.status or 500
  25. if res.statusCode == 500
  26. res.end("Oops, something went wrong with your request, sorry. If this continues, please contact us at #{Settings.adminEmail}")
  27. else
  28. res.end()
  29. if Settings.catchErrors
  30. process.removeAllListeners "uncaughtException"
  31. process.on "uncaughtException", (error) ->
  32. logger.error err: error, "uncaughtException"
  33. port = Settings.port or Settings.internal?.web?.port or 3000
  34. host = Settings.internal.web.host or "localhost"
  35. Server.server.listen port, host, ->
  36. logger.info "web starting up, listening on #{host}:#{port}"
  37. logger.info("#{require('http').globalAgent.maxSockets} sockets enabled")
  38. if argv.user
  39. process.setuid argv.user
  40. logger.info "Running as user: #{argv.user}"
  41. if argv.group
  42. process.setgid argv.group
  43. logger.info "Running as group: #{argv.group}"