app.coffee 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. CompileController = require "./app/js/CompileController"
  2. Settings = require "settings-sharelatex"
  3. logger = require "logger-sharelatex"
  4. logger.initialize("clsi")
  5. if Settings.sentry?.dsn?
  6. logger.initializeErrorReporting(Settings.sentry.dsn)
  7. smokeTest = require "smoke-test-sharelatex"
  8. ContentTypeMapper = require "./app/js/ContentTypeMapper"
  9. Path = require "path"
  10. fs = require "fs"
  11. Metrics = require "metrics-sharelatex"
  12. Metrics.initialize("clsi")
  13. Metrics.open_sockets.monitor(logger)
  14. Metrics.memory.monitor(logger)
  15. ProjectPersistenceManager = require "./app/js/ProjectPersistenceManager"
  16. OutputCacheManager = require "./app/js/OutputCacheManager"
  17. require("./app/js/db").sync()
  18. express = require "express"
  19. bodyParser = require "body-parser"
  20. app = express()
  21. app.use Metrics.http.monitor(logger)
  22. # Compile requests can take longer than the default two
  23. # minutes (including file download time), so bump up the
  24. # timeout a bit.
  25. TIMEOUT = 6 * 60 * 1000
  26. app.use (req, res, next) ->
  27. req.setTimeout TIMEOUT
  28. res.setTimeout TIMEOUT
  29. next()
  30. app.post "/project/:project_id/compile", bodyParser.json(limit: "5mb"), CompileController.compile
  31. app.delete "/project/:project_id", CompileController.clearCache
  32. app.get "/project/:project_id/sync/code", CompileController.syncFromCode
  33. app.get "/project/:project_id/sync/pdf", CompileController.syncFromPdf
  34. app.get "/project/:project_id/wordcount", CompileController.wordcount
  35. ForbidSymlinks = require "./app/js/StaticServerForbidSymlinks"
  36. # create a static server which does not allow access to any symlinks
  37. # avoids possible mismatch of root directory between middleware check
  38. # and serving the files
  39. staticServer = ForbidSymlinks express.static, Settings.path.compilesDir, setHeaders: (res, path, stat) ->
  40. if Path.basename(path) == "output.pdf"
  41. # Calculate an etag in the same way as nginx
  42. # https://github.com/tj/send/issues/65
  43. etag = (path, stat) ->
  44. '"' + Math.ceil(+stat.mtime / 1000).toString(16) +
  45. '-' + Number(stat.size).toString(16) + '"'
  46. res.set("Etag", etag(path, stat))
  47. res.set("Content-Type", ContentTypeMapper.map(path))
  48. app.get "/project/:project_id/output/*", (req, res, next) ->
  49. if req.query?.build? && req.query.build.match(OutputCacheManager.BUILD_REGEX)
  50. # for specific build get the path from the OutputCacheManager (e.g. .clsi/buildId)
  51. req.url = "/#{req.params.project_id}/" + OutputCacheManager.path(req.query.build, "/#{req.params[0]}")
  52. else
  53. req.url = "/#{req.params.project_id}/#{req.params[0]}"
  54. staticServer(req, res, next)
  55. app.get "/oops", (req, res, next) ->
  56. logger.error {err: "hello"}, "test error"
  57. res.send "error\n"
  58. app.get "/status", (req, res, next) ->
  59. res.send "CLSI is alive\n"
  60. resCacher =
  61. contentType:(@setContentType)->
  62. send:(@code, @body)->
  63. #default the server to be down
  64. code:500
  65. body:{}
  66. setContentType:"application/json"
  67. if Settings.smokeTest
  68. do runSmokeTest = ->
  69. logger.log("running smoke tests")
  70. smokeTest.run(require.resolve(__dirname + "/test/smoke/js/SmokeTests.js"))({}, resCacher)
  71. setTimeout(runSmokeTest, 20 * 1000)
  72. app.get "/health_check", (req, res)->
  73. res.contentType(resCacher?.setContentType)
  74. res.status(resCacher?.code).send(resCacher?.body)
  75. profiler = require "v8-profiler"
  76. app.get "/profile", (req, res) ->
  77. time = parseInt(req.query.time || "1000")
  78. profiler.startProfiling("test")
  79. setTimeout () ->
  80. profile = profiler.stopProfiling("test")
  81. res.json(profile)
  82. , time
  83. app.get "/heapdump", (req, res)->
  84. require('heapdump').writeSnapshot '/tmp/' + Date.now() + '.clsi.heapsnapshot', (err, filename)->
  85. res.send filename
  86. app.use (error, req, res, next) ->
  87. logger.error err: error, "server error"
  88. res.sendStatus(error?.statusCode || 500)
  89. app.listen port = (Settings.internal?.clsi?.port or 3013), host = (Settings.internal?.clsi?.host or "localhost"), (error) ->
  90. logger.info "CLSI starting up, listening on #{host}:#{port}"
  91. setInterval () ->
  92. ProjectPersistenceManager.clearExpiredProjects()
  93. , tenMinutes = 10 * 60 * 1000