app.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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('logger-sharelatex')
  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.post('/project/:project_id/flush', HttpController.flushProject)
  55. app.post(
  56. '/project/:project_id/doc/:doc_id/version/:version/restore',
  57. HttpController.restore
  58. )
  59. app.post('/project/:project_id/doc/:doc_id/push', HttpController.pushDocHistory)
  60. app.post('/project/:project_id/doc/:doc_id/pull', HttpController.pullDocHistory)
  61. app.post('/flush/all', HttpController.flushAll)
  62. app.post('/check/dangling', HttpController.checkDanglingUpdates)
  63. let packWorker = null // use a single packing worker
  64. app.post('/pack', function (req, res, next) {
  65. if (packWorker != null) {
  66. return res.send('pack already running')
  67. } else {
  68. logger.log('running pack')
  69. packWorker = childProcess.fork(
  70. Path.join(__dirname, '/app/js/PackWorker.js'),
  71. [
  72. req.query.limit || 1000,
  73. req.query.delay || 1000,
  74. req.query.timeout || 30 * 60 * 1000,
  75. ]
  76. )
  77. packWorker.on('exit', function (code, signal) {
  78. logger.log({ code, signal }, 'history auto pack exited')
  79. return (packWorker = null)
  80. })
  81. return res.send('pack started')
  82. }
  83. })
  84. app.get('/status', (req, res, next) => res.send('track-changes is alive'))
  85. app.get('/oops', function (req, res, next) {
  86. throw new Error('dummy test error')
  87. })
  88. app.get('/check_lock', HttpController.checkLock)
  89. app.get('/health_check', HttpController.healthCheck)
  90. app.use(function (error, req, res, next) {
  91. logger.error({ err: error, req }, 'an internal error occured')
  92. return res.sendStatus(500)
  93. })
  94. const port =
  95. __guard__(
  96. Settings.internal != null ? Settings.internal.trackchanges : undefined,
  97. x => x.port
  98. ) || 3015
  99. const host =
  100. __guard__(
  101. Settings.internal != null ? Settings.internal.trackchanges : undefined,
  102. x1 => x1.host
  103. ) || 'localhost'
  104. if (!module.parent) {
  105. // Called directly
  106. mongodb
  107. .waitForDb()
  108. .then(() => {
  109. app.listen(port, host, function (error) {
  110. if (error != null) {
  111. return logger.error(
  112. { err: error },
  113. 'could not start track-changes server'
  114. )
  115. } else {
  116. return logger.info(
  117. `trackchanges starting up, listening on ${host}:${port}`
  118. )
  119. }
  120. })
  121. })
  122. .catch(err => {
  123. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  124. process.exit(1)
  125. })
  126. }
  127. module.exports = app
  128. function __guard__(value, transform) {
  129. return typeof value !== 'undefined' && value !== null
  130. ? transform(value)
  131. : undefined
  132. }