2020-02-16 14:03:43 +00:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-09-10 17:07:14 +00:00
|
|
|
const Metrics = require('@overleaf/metrics')
|
2020-02-16 14:03:45 +00:00
|
|
|
Metrics.initialize('docstore')
|
2021-07-12 16:47:20 +00:00
|
|
|
const Settings = require('@overleaf/settings')
|
2021-12-14 13:00:35 +00:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-02-16 14:03:45 +00:00
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
2021-02-15 13:13:48 +00:00
|
|
|
const {
|
|
|
|
celebrate: validate,
|
|
|
|
Joi,
|
2021-07-13 11:04:48 +00:00
|
|
|
errors: handleValidationErrors,
|
2021-02-15 13:13:48 +00:00
|
|
|
} = require('celebrate')
|
2020-08-21 16:50:01 +00:00
|
|
|
const mongodb = require('./app/js/mongodb')
|
2020-02-16 14:03:45 +00:00
|
|
|
const Errors = require('./app/js/Errors')
|
|
|
|
const HttpController = require('./app/js/HttpController')
|
|
|
|
|
|
|
|
logger.initialize('docstore')
|
2020-02-16 14:03:43 +00:00
|
|
|
if (Metrics.event_loop != null) {
|
2020-02-16 14:03:45 +00:00
|
|
|
Metrics.event_loop.monitor(logger)
|
2020-02-16 14:03:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 14:03:45 +00:00
|
|
|
const app = express()
|
2020-02-16 14:03:43 +00:00
|
|
|
|
2020-02-16 14:03:45 +00:00
|
|
|
app.use(Metrics.http.monitor(logger))
|
2020-02-16 14:03:43 +00:00
|
|
|
|
2020-02-16 14:03:45 +00:00
|
|
|
Metrics.injectMetricsRoute(app)
|
2020-02-16 14:03:43 +00:00
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
app.param('project_id', function (req, res, next, projectId) {
|
2020-02-16 14:10:24 +00:00
|
|
|
if (projectId != null ? projectId.match(/^[0-9a-f]{24}$/) : undefined) {
|
2020-02-16 14:03:45 +00:00
|
|
|
return next()
|
|
|
|
} else {
|
|
|
|
return next(new Error('invalid project id'))
|
|
|
|
}
|
|
|
|
})
|
2020-02-16 14:03:43 +00:00
|
|
|
|
2020-05-28 13:20:54 +00:00
|
|
|
app.param('doc_id', function (req, res, next, docId) {
|
2020-02-16 14:10:24 +00:00
|
|
|
if (docId != null ? docId.match(/^[0-9a-f]{24}$/) : undefined) {
|
2020-02-16 14:03:45 +00:00
|
|
|
return next()
|
|
|
|
} else {
|
|
|
|
return next(new Error('invalid doc id'))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-02-18 10:10:14 +00:00
|
|
|
app.get('/project/:project_id/doc-deleted', HttpController.getAllDeletedDocs)
|
2020-02-16 14:03:45 +00:00
|
|
|
app.get('/project/:project_id/doc', HttpController.getAllDocs)
|
|
|
|
app.get('/project/:project_id/ranges', HttpController.getAllRanges)
|
|
|
|
app.get('/project/:project_id/doc/:doc_id', HttpController.getDoc)
|
2021-01-12 17:29:06 +00:00
|
|
|
app.get('/project/:project_id/doc/:doc_id/deleted', HttpController.isDocDeleted)
|
2020-02-16 14:03:45 +00:00
|
|
|
app.get('/project/:project_id/doc/:doc_id/raw', HttpController.getRawDoc)
|
2021-07-30 15:06:16 +00:00
|
|
|
app.get('/project/:project_id/doc/:doc_id/peek', HttpController.peekDoc)
|
2020-02-16 14:03:43 +00:00
|
|
|
// Add 64kb overhead for the JSON encoding, and double the size to allow for ranges in the json payload
|
2020-02-16 14:03:45 +00:00
|
|
|
app.post(
|
|
|
|
'/project/:project_id/doc/:doc_id',
|
2022-03-04 15:17:16 +00:00
|
|
|
bodyParser.json({ limit: Settings.maxJsonRequestSize }),
|
2020-02-16 14:03:45 +00:00
|
|
|
HttpController.updateDoc
|
|
|
|
)
|
2021-02-15 13:13:48 +00:00
|
|
|
app.patch(
|
|
|
|
'/project/:project_id/doc/:doc_id',
|
|
|
|
bodyParser.json(),
|
|
|
|
validate({
|
|
|
|
body: {
|
|
|
|
deleted: Joi.boolean(),
|
|
|
|
name: Joi.string().when('deleted', { is: true, then: Joi.required() }),
|
2021-07-13 11:04:48 +00:00
|
|
|
deletedAt: Joi.date().when('deleted', { is: true, then: Joi.required() }),
|
|
|
|
},
|
2021-02-15 13:13:48 +00:00
|
|
|
}),
|
|
|
|
HttpController.patchDoc
|
|
|
|
)
|
2021-04-12 16:31:18 +00:00
|
|
|
app.delete('/project/:project_id/doc/:doc_id', (req, res) => {
|
|
|
|
res.status(500).send('DELETE-ing a doc is DEPRECATED. PATCH the doc instead.')
|
|
|
|
})
|
2020-02-16 14:03:43 +00:00
|
|
|
|
2020-02-16 14:03:45 +00:00
|
|
|
app.post('/project/:project_id/archive', HttpController.archiveAllDocs)
|
2021-01-04 11:55:47 +00:00
|
|
|
app.post('/project/:project_id/doc/:doc_id/archive', HttpController.archiveDoc)
|
2020-02-16 14:03:45 +00:00
|
|
|
app.post('/project/:project_id/unarchive', HttpController.unArchiveAllDocs)
|
|
|
|
app.post('/project/:project_id/destroy', HttpController.destroyAllDocs)
|
2020-02-16 14:03:43 +00:00
|
|
|
|
2020-02-16 14:03:45 +00:00
|
|
|
app.get('/health_check', HttpController.healthCheck)
|
2020-02-16 14:03:43 +00:00
|
|
|
|
2020-02-16 14:03:45 +00:00
|
|
|
app.get('/status', (req, res) => res.send('docstore is alive'))
|
2020-02-16 14:03:43 +00:00
|
|
|
|
2021-02-15 13:13:48 +00:00
|
|
|
app.use(handleValidationErrors())
|
2020-05-28 13:20:54 +00:00
|
|
|
app.use(function (error, req, res, next) {
|
2020-02-16 14:03:45 +00:00
|
|
|
logger.error({ err: error, req }, 'request errored')
|
|
|
|
if (error instanceof Errors.NotFoundError) {
|
2020-03-18 14:07:01 +00:00
|
|
|
return res.sendStatus(404)
|
2021-08-02 09:25:18 +00:00
|
|
|
} else if (error instanceof Errors.DocModifiedError) {
|
|
|
|
return res.sendStatus(409)
|
2020-02-16 14:03:45 +00:00
|
|
|
} else {
|
2020-03-18 14:07:01 +00:00
|
|
|
return res.status(500).send('Oops, something went wrong')
|
2020-02-16 14:03:45 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const { port } = Settings.internal.docstore
|
|
|
|
const { host } = Settings.internal.docstore
|
|
|
|
|
|
|
|
if (!module.parent) {
|
|
|
|
// Called directly
|
2020-08-21 16:50:01 +00:00
|
|
|
mongodb
|
|
|
|
.waitForDb()
|
|
|
|
.then(() => {
|
2021-12-07 16:13:21 +00:00
|
|
|
const server = app.listen(port, host, function (err) {
|
2020-08-21 16:50:01 +00:00
|
|
|
if (err) {
|
|
|
|
logger.fatal({ err }, `Cannot bind to ${host}:${port}. Exiting.`)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
return logger.info(`Docstore starting up, listening on ${host}:${port}`)
|
|
|
|
})
|
2021-12-07 16:13:21 +00:00
|
|
|
server.timeout = 120000
|
|
|
|
server.keepAliveTimeout = 5000
|
|
|
|
server.requestTimeout = 60000
|
|
|
|
server.headersTimeout = 60000
|
2020-08-21 16:50:01 +00:00
|
|
|
})
|
2021-07-13 11:04:48 +00:00
|
|
|
.catch(err => {
|
2020-08-21 16:50:01 +00:00
|
|
|
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
|
|
|
|
process.exit(1)
|
|
|
|
})
|
2020-02-16 14:03:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 14:03:45 +00:00
|
|
|
module.exports = app
|