app.js 3.9 KB

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