app.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 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 = child_process.fork(__dirname + '/app/js/PackWorker.js', [
  68. req.query.limit || 1000,
  69. req.query.delay || 1000,
  70. req.query.timeout || 30 * 60 * 1000
  71. ])
  72. packWorker.on('exit', function(code, signal) {
  73. logger.log({ code, signal }, 'history auto pack exited')
  74. return (packWorker = null)
  75. })
  76. return res.send('pack started')
  77. }
  78. })
  79. app.get('/status', (req, res, next) => res.send('track-changes is alive'))
  80. app.get('/oops', function(req, res, next) {
  81. throw new Error('dummy test error')
  82. })
  83. app.get('/check_lock', HttpController.checkLock)
  84. app.get('/health_check', HttpController.healthCheck)
  85. app.use(function(error, req, res, next) {
  86. logger.error({ err: error, req }, 'an internal error occured')
  87. return res.send(500)
  88. })
  89. const port =
  90. __guard__(
  91. Settings.internal != null ? Settings.internal.trackchanges : undefined,
  92. x => x.port
  93. ) || 3015
  94. const host =
  95. __guard__(
  96. Settings.internal != null ? Settings.internal.trackchanges : undefined,
  97. x1 => x1.host
  98. ) || 'localhost'
  99. if (!module.parent) {
  100. // Called directly
  101. app.listen(port, host, function(error) {
  102. if (error != null) {
  103. return logger.error(
  104. { err: error },
  105. 'could not start track-changes server'
  106. )
  107. } else {
  108. return logger.info(
  109. `trackchanges starting up, listening on ${host}:${port}`
  110. )
  111. }
  112. })
  113. }
  114. module.exports = app
  115. function __guard__(value, transform) {
  116. return typeof value !== 'undefined' && value !== null
  117. ? transform(value)
  118. : undefined
  119. }