app.js 2.7 KB

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