app.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * decaffeinate suggestions:
  3. * DS102: Remove unnecessary code created because of implicit returns
  4. * DS103: Rewrite code to no longer use __guard__
  5. * DS207: Consider shorter variations of null checks
  6. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  7. */
  8. const Metrics = require('@overleaf/metrics')
  9. Metrics.initialize('track-changes')
  10. const Settings = require('@overleaf/settings')
  11. const logger = require('@overleaf/logger')
  12. const TrackChangesLogger = logger.initialize('track-changes').logger
  13. if ((Settings.sentry != null ? Settings.sentry.dsn : undefined) != null) {
  14. logger.initializeErrorReporting(Settings.sentry.dsn)
  15. }
  16. // log updates as truncated strings
  17. const truncateFn = updates =>
  18. JSON.parse(
  19. JSON.stringify(updates, function (key, value) {
  20. let len
  21. if (typeof value === 'string' && (len = value.length) > 80) {
  22. return (
  23. value.substr(0, 32) +
  24. `...(message of length ${len} truncated)...` +
  25. value.substr(-32)
  26. )
  27. } else {
  28. return value
  29. }
  30. })
  31. )
  32. TrackChangesLogger.addSerializers({
  33. rawUpdate: truncateFn,
  34. rawUpdates: truncateFn,
  35. newUpdates: truncateFn,
  36. lastUpdate: truncateFn,
  37. })
  38. const Path = require('path')
  39. Metrics.memory.monitor(logger)
  40. const childProcess = require('child_process')
  41. const mongodb = require('./app/js/mongodb')
  42. const HttpController = require('./app/js/HttpController')
  43. const express = require('express')
  44. const bodyParser = require('body-parser')
  45. const app = express()
  46. app.use(bodyParser.json())
  47. app.use(Metrics.http.monitor(logger))
  48. Metrics.injectMetricsRoute(app)
  49. app.post('/project/:project_id/doc/:doc_id/flush', HttpController.flushDoc)
  50. app.get('/project/:project_id/doc/:doc_id/diff', HttpController.getDiff)
  51. app.get('/project/:project_id/doc/:doc_id/check', HttpController.checkDoc)
  52. app.get('/project/:project_id/updates', HttpController.getUpdates)
  53. app.get('/project/:project_id/export', HttpController.exportProject)
  54. app.get('/project/:project_id/zip', HttpController.zipProject)
  55. app.post('/project/:project_id/flush', HttpController.flushProject)
  56. app.post(
  57. '/project/:project_id/doc/:doc_id/version/:version/restore',
  58. HttpController.restore
  59. )
  60. app.post('/project/:project_id/doc/:doc_id/push', HttpController.pushDocHistory)
  61. app.post('/project/:project_id/doc/:doc_id/pull', HttpController.pullDocHistory)
  62. app.post('/flush/all', HttpController.flushAll)
  63. app.post('/check/dangling', HttpController.checkDanglingUpdates)
  64. let packWorker = null // use a single packing worker
  65. app.post('/pack', function (req, res, next) {
  66. if (packWorker != null) {
  67. return res.send('pack already running')
  68. } else {
  69. logger.log('running pack')
  70. packWorker = childProcess.fork(
  71. Path.join(__dirname, '/app/js/PackWorker.js'),
  72. [
  73. req.query.limit || 1000,
  74. req.query.delay || 1000,
  75. req.query.timeout || 30 * 60 * 1000,
  76. ]
  77. )
  78. packWorker.on('exit', function (code, signal) {
  79. logger.log({ code, signal }, 'history auto pack exited')
  80. return (packWorker = null)
  81. })
  82. return res.send('pack started')
  83. }
  84. })
  85. app.get('/status', (req, res, next) => res.send('track-changes is alive'))
  86. app.get('/oops', function (req, res, next) {
  87. throw new Error('dummy test error')
  88. })
  89. app.get('/check_lock', HttpController.checkLock)
  90. app.get('/health_check', HttpController.healthCheck)
  91. app.use(function (error, req, res, next) {
  92. logger.error({ err: error, req }, 'an internal error occured')
  93. return res.sendStatus(500)
  94. })
  95. const port =
  96. __guard__(
  97. Settings.internal != null ? Settings.internal.trackchanges : undefined,
  98. x => x.port
  99. ) || 3015
  100. const host =
  101. __guard__(
  102. Settings.internal != null ? Settings.internal.trackchanges : undefined,
  103. x1 => x1.host
  104. ) || 'localhost'
  105. if (!module.parent) {
  106. // Called directly
  107. mongodb
  108. .waitForDb()
  109. .then(() => {
  110. app.listen(port, host, function (error) {
  111. if (error != null) {
  112. return logger.error(
  113. { err: error },
  114. 'could not start track-changes server'
  115. )
  116. } else {
  117. return logger.info(
  118. `trackchanges starting up, listening on ${host}:${port}`
  119. )
  120. }
  121. })
  122. })
  123. .catch(err => {
  124. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  125. process.exit(1)
  126. })
  127. }
  128. module.exports = app
  129. function __guard__(value, transform) {
  130. return typeof value !== 'undefined' && value !== null
  131. ? transform(value)
  132. : undefined
  133. }