app.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 HttpController = require('./app/js/HttpController')
  42. const express = require('express')
  43. const bodyParser = require('body-parser')
  44. const app = express()
  45. app.use(bodyParser.json())
  46. app.use(Metrics.http.monitor(logger))
  47. Metrics.injectMetricsRoute(app)
  48. app.post('/project/:project_id/doc/:doc_id/flush', HttpController.flushDoc)
  49. app.get('/project/:project_id/doc/:doc_id/diff', HttpController.getDiff)
  50. app.get('/project/:project_id/doc/:doc_id/check', HttpController.checkDoc)
  51. app.get('/project/:project_id/updates', HttpController.getUpdates)
  52. app.post('/project/:project_id/flush', HttpController.flushProject)
  53. app.post(
  54. '/project/:project_id/doc/:doc_id/version/:version/restore',
  55. HttpController.restore
  56. )
  57. app.post('/project/:project_id/doc/:doc_id/push', HttpController.pushDocHistory)
  58. app.post('/project/:project_id/doc/:doc_id/pull', HttpController.pullDocHistory)
  59. app.post('/flush/all', HttpController.flushAll)
  60. app.post('/check/dangling', HttpController.checkDanglingUpdates)
  61. let packWorker = null // use a single packing worker
  62. app.post('/pack', function (req, res, next) {
  63. if (packWorker != null) {
  64. return res.send('pack already running')
  65. } else {
  66. logger.log('running pack')
  67. packWorker = childProcess.fork(
  68. Path.join(__dirname, '/app/js/PackWorker.js'),
  69. [
  70. req.query.limit || 1000,
  71. req.query.delay || 1000,
  72. req.query.timeout || 30 * 60 * 1000
  73. ]
  74. )
  75. packWorker.on('exit', function (code, signal) {
  76. logger.log({ code, signal }, 'history auto pack exited')
  77. return (packWorker = null)
  78. })
  79. return res.send('pack started')
  80. }
  81. })
  82. app.get('/status', (req, res, next) => res.send('track-changes is alive'))
  83. app.get('/oops', function (req, res, next) {
  84. throw new Error('dummy test error')
  85. })
  86. app.get('/check_lock', HttpController.checkLock)
  87. app.get('/health_check', HttpController.healthCheck)
  88. app.use(function (error, req, res, next) {
  89. logger.error({ err: error, req }, 'an internal error occured')
  90. return res.sendStatus(500)
  91. })
  92. const port =
  93. __guard__(
  94. Settings.internal != null ? Settings.internal.trackchanges : undefined,
  95. (x) => x.port
  96. ) || 3015
  97. const host =
  98. __guard__(
  99. Settings.internal != null ? Settings.internal.trackchanges : undefined,
  100. (x1) => x1.host
  101. ) || 'localhost'
  102. if (!module.parent) {
  103. // Called directly
  104. app.listen(port, host, function (error) {
  105. if (error != null) {
  106. return logger.error(
  107. { err: error },
  108. 'could not start track-changes server'
  109. )
  110. } else {
  111. return logger.info(
  112. `trackchanges starting up, listening on ${host}:${port}`
  113. )
  114. }
  115. })
  116. }
  117. module.exports = app
  118. function __guard__(value, transform) {
  119. return typeof value !== 'undefined' && value !== null
  120. ? transform(value)
  121. : undefined
  122. }