app.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * decaffeinate suggestions:
  3. * DS102: Remove unnecessary code created because of implicit returns
  4. * DS207: Consider shorter variations of null checks
  5. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  6. */
  7. const Metrics = require('@overleaf/metrics')
  8. Metrics.initialize('docstore')
  9. const Settings = require('@overleaf/settings')
  10. const logger = require('@overleaf/logger')
  11. const express = require('express')
  12. const bodyParser = require('body-parser')
  13. const {
  14. celebrate: validate,
  15. Joi,
  16. errors: handleValidationErrors,
  17. } = require('celebrate')
  18. const mongodb = require('./app/js/mongodb')
  19. const Errors = require('./app/js/Errors')
  20. const HttpController = require('./app/js/HttpController')
  21. logger.initialize('docstore')
  22. if (Metrics.event_loop != null) {
  23. Metrics.event_loop.monitor(logger)
  24. }
  25. const app = express()
  26. app.use(Metrics.http.monitor(logger))
  27. Metrics.injectMetricsRoute(app)
  28. app.param('project_id', function (req, res, next, projectId) {
  29. if (projectId != null ? projectId.match(/^[0-9a-f]{24}$/) : undefined) {
  30. return next()
  31. } else {
  32. return next(new Error('invalid project id'))
  33. }
  34. })
  35. app.param('doc_id', function (req, res, next, docId) {
  36. if (docId != null ? docId.match(/^[0-9a-f]{24}$/) : undefined) {
  37. return next()
  38. } else {
  39. return next(new Error('invalid doc id'))
  40. }
  41. })
  42. app.get('/project/:project_id/doc-deleted', HttpController.getAllDeletedDocs)
  43. app.get('/project/:project_id/doc', HttpController.getAllDocs)
  44. app.get('/project/:project_id/ranges', HttpController.getAllRanges)
  45. app.get('/project/:project_id/doc/:doc_id', HttpController.getDoc)
  46. app.get('/project/:project_id/doc/:doc_id/deleted', HttpController.isDocDeleted)
  47. app.get('/project/:project_id/doc/:doc_id/raw', HttpController.getRawDoc)
  48. app.get('/project/:project_id/doc/:doc_id/peek', HttpController.peekDoc)
  49. // Add 64kb overhead for the JSON encoding, and double the size to allow for ranges in the json payload
  50. app.post(
  51. '/project/:project_id/doc/:doc_id',
  52. bodyParser.json({ limit: Settings.maxJsonRequestSize }),
  53. HttpController.updateDoc
  54. )
  55. app.patch(
  56. '/project/:project_id/doc/:doc_id',
  57. bodyParser.json(),
  58. validate({
  59. body: {
  60. deleted: Joi.boolean(),
  61. name: Joi.string().when('deleted', { is: true, then: Joi.required() }),
  62. deletedAt: Joi.date().when('deleted', { is: true, then: Joi.required() }),
  63. },
  64. }),
  65. HttpController.patchDoc
  66. )
  67. app.delete('/project/:project_id/doc/:doc_id', (req, res) => {
  68. res.status(500).send('DELETE-ing a doc is DEPRECATED. PATCH the doc instead.')
  69. })
  70. app.post('/project/:project_id/archive', HttpController.archiveAllDocs)
  71. app.post('/project/:project_id/doc/:doc_id/archive', HttpController.archiveDoc)
  72. app.post('/project/:project_id/unarchive', HttpController.unArchiveAllDocs)
  73. app.post('/project/:project_id/destroy', HttpController.destroyProject)
  74. app.get('/health_check', HttpController.healthCheck)
  75. app.get('/status', (req, res) => res.send('docstore is alive'))
  76. app.use(handleValidationErrors())
  77. app.use(function (error, req, res, next) {
  78. logger.error({ err: error, req }, 'request errored')
  79. if (error instanceof Errors.NotFoundError) {
  80. return res.sendStatus(404)
  81. } else if (error instanceof Errors.DocModifiedError) {
  82. return res.sendStatus(409)
  83. } else {
  84. return res.status(500).send('Oops, something went wrong')
  85. }
  86. })
  87. const { port } = Settings.internal.docstore
  88. const { host } = Settings.internal.docstore
  89. if (!module.parent) {
  90. // Called directly
  91. mongodb
  92. .waitForDb()
  93. .then(() => {
  94. const server = app.listen(port, host, function (err) {
  95. if (err) {
  96. logger.fatal({ err }, `Cannot bind to ${host}:${port}. Exiting.`)
  97. process.exit(1)
  98. }
  99. return logger.debug(
  100. `Docstore starting up, listening on ${host}:${port}`
  101. )
  102. })
  103. server.timeout = 120000
  104. server.keepAliveTimeout = 5000
  105. server.requestTimeout = 60000
  106. server.headersTimeout = 60000
  107. })
  108. .catch(err => {
  109. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  110. process.exit(1)
  111. })
  112. }
  113. module.exports = app