app.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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('logger-sharelatex')
  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. Metrics.injectMetricsRoute(app)
  43. app.get('/project/:project_id/doc-deleted', HttpController.getAllDeletedDocs)
  44. app.get('/project/:project_id/doc', HttpController.getAllDocs)
  45. app.get('/project/:project_id/ranges', HttpController.getAllRanges)
  46. app.get('/project/:project_id/doc/:doc_id', HttpController.getDoc)
  47. app.get('/project/:project_id/doc/:doc_id/deleted', HttpController.isDocDeleted)
  48. app.get('/project/:project_id/doc/:doc_id/raw', HttpController.getRawDoc)
  49. app.get('/project/:project_id/doc/:doc_id/peek', HttpController.peekDoc)
  50. // Add 64kb overhead for the JSON encoding, and double the size to allow for ranges in the json payload
  51. app.post(
  52. '/project/:project_id/doc/:doc_id',
  53. bodyParser.json({ limit: (Settings.max_doc_length + 64 * 1024) * 2 }),
  54. HttpController.updateDoc
  55. )
  56. app.patch(
  57. '/project/:project_id/doc/:doc_id',
  58. bodyParser.json(),
  59. validate({
  60. body: {
  61. deleted: Joi.boolean(),
  62. name: Joi.string().when('deleted', { is: true, then: Joi.required() }),
  63. deletedAt: Joi.date().when('deleted', { is: true, then: Joi.required() }),
  64. },
  65. }),
  66. HttpController.patchDoc
  67. )
  68. app.delete('/project/:project_id/doc/:doc_id', (req, res) => {
  69. res.status(500).send('DELETE-ing a doc is DEPRECATED. PATCH the doc instead.')
  70. })
  71. app.post('/project/:project_id/archive', HttpController.archiveAllDocs)
  72. app.post('/project/:project_id/doc/:doc_id/archive', HttpController.archiveDoc)
  73. app.post('/project/:project_id/unarchive', HttpController.unArchiveAllDocs)
  74. app.post('/project/:project_id/destroy', HttpController.destroyAllDocs)
  75. app.get('/health_check', HttpController.healthCheck)
  76. app.get('/status', (req, res) => res.send('docstore is alive'))
  77. app.use(handleValidationErrors())
  78. app.use(function (error, req, res, next) {
  79. logger.error({ err: error, req }, 'request errored')
  80. if (error instanceof Errors.NotFoundError) {
  81. return res.sendStatus(404)
  82. } else if (error instanceof Errors.DocModifiedError) {
  83. return res.sendStatus(409)
  84. } else {
  85. return res.status(500).send('Oops, something went wrong')
  86. }
  87. })
  88. const { port } = Settings.internal.docstore
  89. const { host } = Settings.internal.docstore
  90. if (!module.parent) {
  91. // Called directly
  92. mongodb
  93. .waitForDb()
  94. .then(() => {
  95. app.listen(port, host, function (err) {
  96. if (err) {
  97. logger.fatal({ err }, `Cannot bind to ${host}:${port}. Exiting.`)
  98. process.exit(1)
  99. }
  100. return logger.info(`Docstore starting up, listening on ${host}:${port}`)
  101. })
  102. })
  103. .catch(err => {
  104. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  105. process.exit(1)
  106. })
  107. }
  108. module.exports = app