app.js 3.7 KB

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