app.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 mongodb = require('./app/js/mongodb')
  14. const Errors = require('./app/js/Errors')
  15. const HttpController = require('./app/js/HttpController')
  16. logger.initialize('docstore')
  17. if (Metrics.event_loop != null) {
  18. Metrics.event_loop.monitor(logger)
  19. }
  20. const app = express()
  21. app.use(Metrics.http.monitor(logger))
  22. Metrics.injectMetricsRoute(app)
  23. app.param('project_id', function (req, res, next, projectId) {
  24. if (projectId != null ? projectId.match(/^[0-9a-f]{24}$/) : undefined) {
  25. return next()
  26. } else {
  27. return next(new Error('invalid project id'))
  28. }
  29. })
  30. app.param('doc_id', function (req, res, next, docId) {
  31. if (docId != null ? docId.match(/^[0-9a-f]{24}$/) : undefined) {
  32. return next()
  33. } else {
  34. return next(new Error('invalid doc id'))
  35. }
  36. })
  37. Metrics.injectMetricsRoute(app)
  38. app.get('/project/:project_id/doc', HttpController.getAllDocs)
  39. app.get('/project/:project_id/ranges', HttpController.getAllRanges)
  40. app.get('/project/:project_id/doc/:doc_id', HttpController.getDoc)
  41. app.get('/project/:project_id/doc/:doc_id/deleted', HttpController.isDocDeleted)
  42. app.get('/project/:project_id/doc/:doc_id/raw', HttpController.getRawDoc)
  43. // Add 64kb overhead for the JSON encoding, and double the size to allow for ranges in the json payload
  44. app.post(
  45. '/project/:project_id/doc/:doc_id',
  46. bodyParser.json({ limit: (Settings.max_doc_length + 64 * 1024) * 2 }),
  47. HttpController.updateDoc
  48. )
  49. app.delete('/project/:project_id/doc/:doc_id', HttpController.deleteDoc)
  50. app.post('/project/:project_id/archive', HttpController.archiveAllDocs)
  51. app.post('/project/:project_id/doc/:doc_id/archive', HttpController.archiveDoc)
  52. app.post('/project/:project_id/unarchive', HttpController.unArchiveAllDocs)
  53. app.post('/project/:project_id/destroy', HttpController.destroyAllDocs)
  54. app.get('/health_check', HttpController.healthCheck)
  55. app.get('/status', (req, res) => res.send('docstore is alive'))
  56. app.use(function (error, req, res, next) {
  57. logger.error({ err: error, req }, 'request errored')
  58. if (error instanceof Errors.NotFoundError) {
  59. return res.sendStatus(404)
  60. } else {
  61. return res.status(500).send('Oops, something went wrong')
  62. }
  63. })
  64. const { port } = Settings.internal.docstore
  65. const { host } = Settings.internal.docstore
  66. if (!module.parent) {
  67. // Called directly
  68. mongodb
  69. .waitForDb()
  70. .then(() => {
  71. app.listen(port, host, function (err) {
  72. if (err) {
  73. logger.fatal({ err }, `Cannot bind to ${host}:${port}. Exiting.`)
  74. process.exit(1)
  75. }
  76. return logger.info(`Docstore starting up, listening on ${host}:${port}`)
  77. })
  78. })
  79. .catch((err) => {
  80. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  81. process.exit(1)
  82. })
  83. }
  84. module.exports = app