app.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/raw', HttpController.getRawDoc)
  42. // Add 64kb overhead for the JSON encoding, and double the size to allow for ranges in the json payload
  43. app.post(
  44. '/project/:project_id/doc/:doc_id',
  45. bodyParser.json({ limit: (Settings.max_doc_length + 64 * 1024) * 2 }),
  46. HttpController.updateDoc
  47. )
  48. app.delete('/project/:project_id/doc/:doc_id', HttpController.deleteDoc)
  49. app.post('/project/:project_id/archive', HttpController.archiveAllDocs)
  50. app.post('/project/:project_id/unarchive', HttpController.unArchiveAllDocs)
  51. app.post('/project/:project_id/destroy', HttpController.destroyAllDocs)
  52. app.get('/health_check', HttpController.healthCheck)
  53. app.get('/status', (req, res) => res.send('docstore is alive'))
  54. app.use(function (error, req, res, next) {
  55. logger.error({ err: error, req }, 'request errored')
  56. if (error instanceof Errors.NotFoundError) {
  57. return res.sendStatus(404)
  58. } else {
  59. return res.status(500).send('Oops, something went wrong')
  60. }
  61. })
  62. const { port } = Settings.internal.docstore
  63. const { host } = Settings.internal.docstore
  64. if (!module.parent) {
  65. // Called directly
  66. mongodb
  67. .waitForDb()
  68. .then(() => {
  69. app.listen(port, host, function (err) {
  70. if (err) {
  71. logger.fatal({ err }, `Cannot bind to ${host}:${port}. Exiting.`)
  72. process.exit(1)
  73. }
  74. return logger.info(`Docstore starting up, listening on ${host}:${port}`)
  75. })
  76. })
  77. .catch((err) => {
  78. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  79. process.exit(1)
  80. })
  81. }
  82. module.exports = app