app.js 4.3 KB

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