overleaf/services/track-changes/app.js

165 lines
4.5 KiB
JavaScript
Raw Normal View History

/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS103: Rewrite code to no longer use __guard__
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const Metrics = require('@overleaf/metrics')
Metrics.initialize('track-changes')
const Settings = require('@overleaf/settings')
const logger = require('@overleaf/logger')
const TrackChangesLogger = logger.initialize('track-changes').logger
if ((Settings.sentry != null ? Settings.sentry.dsn : undefined) != null) {
logger.initializeErrorReporting(Settings.sentry.dsn)
}
2015-12-07 15:52:21 +00:00
// log updates as truncated strings
2021-07-13 11:04:43 +00:00
const truncateFn = updates =>
JSON.parse(
2020-06-04 08:24:21 +00:00
JSON.stringify(updates, function (key, value) {
let len
if (typeof value === 'string' && (len = value.length) > 80) {
return (
value.substr(0, 32) +
`...(message of length ${len} truncated)...` +
value.substr(-32)
)
} else {
return value
}
})
)
TrackChangesLogger.addSerializers({
rawUpdate: truncateFn,
rawUpdates: truncateFn,
newUpdates: truncateFn,
2021-07-13 11:04:43 +00:00
lastUpdate: truncateFn,
})
2016-01-26 11:28:02 +00:00
const Path = require('path')
Metrics.memory.monitor(logger)
Metrics.open_sockets.monitor()
2014-05-09 11:44:13 +00:00
2020-06-04 08:37:43 +00:00
const childProcess = require('child_process')
const mongodb = require('./app/js/mongodb')
const HttpController = require('./app/js/HttpController')
const express = require('express')
2020-06-04 08:24:21 +00:00
const bodyParser = require('body-parser')
2020-03-23 10:10:24 +00:00
const app = express()
2020-03-23 10:10:24 +00:00
app.use(bodyParser.json())
app.use(Metrics.http.monitor(logger))
2014-02-26 12:11:45 +00:00
Metrics.injectMetricsRoute(app)
app.post('/project/:project_id/doc/:doc_id/flush', HttpController.flushDoc)
2013-08-23 13:35:13 +00:00
app.get('/project/:project_id/doc/:doc_id/diff', HttpController.getDiff)
app.get('/project/:project_id/doc/:doc_id/check', HttpController.checkDoc)
2015-12-09 14:57:04 +00:00
app.get('/project/:project_id/updates', HttpController.getUpdates)
app.get('/project/:project_id/export', HttpController.exportProject)
app.get('/project/:project_id/zip', HttpController.zipProject)
app.post('/project/:project_id/flush', HttpController.flushProject)
2014-03-21 15:57:17 +00:00
app.post(
'/project/:project_id/doc/:doc_id/version/:version/restore',
HttpController.restore
)
2014-03-10 16:58:26 +00:00
app.post('/project/:project_id/doc/:doc_id/push', HttpController.pushDocHistory)
app.post('/project/:project_id/doc/:doc_id/pull', HttpController.pullDocHistory)
2016-03-09 16:56:49 +00:00
app.post('/flush/all', HttpController.flushAll)
app.post('/check/dangling', HttpController.checkDanglingUpdates)
let packWorker = null // use a single packing worker
2020-06-04 08:24:21 +00:00
app.post('/pack', function (req, res, next) {
if (packWorker != null) {
return res.send('pack already running')
} else {
logger.debug('running pack')
2020-06-04 08:37:43 +00:00
packWorker = childProcess.fork(
Path.join(__dirname, '/app/js/PackWorker.js'),
[
req.query.limit || 1000,
req.query.delay || 1000,
2021-07-13 11:04:43 +00:00
req.query.timeout || 30 * 60 * 1000,
2020-06-04 08:37:43 +00:00
]
)
2020-06-04 08:24:21 +00:00
packWorker.on('exit', function (code, signal) {
logger.debug({ code, signal }, 'history auto pack exited')
return (packWorker = null)
})
return res.send('pack started')
}
})
app.get('/status', (req, res, next) => res.send('track-changes is alive'))
2020-06-04 08:24:21 +00:00
app.get('/oops', function (req, res, next) {
throw new Error('dummy test error')
})
app.get('/check_lock', HttpController.checkLock)
app.get('/health_check', HttpController.healthCheck)
2020-06-04 08:24:21 +00:00
app.use(function (error, req, res, next) {
logger.error({ err: error, req }, 'an internal error occured')
return res.sendStatus(500)
})
const port =
__guard__(
Settings.internal != null ? Settings.internal.trackchanges : undefined,
2021-07-13 11:04:43 +00:00
x => x.port
) || 3015
const host =
__guard__(
Settings.internal != null ? Settings.internal.trackchanges : undefined,
2021-07-13 11:04:43 +00:00
x1 => x1.host
) || 'localhost'
if (!module.parent) {
// Called directly
mongodb
.waitForDb()
.then(() => {
app.listen(port, host, function (error) {
if (error != null) {
return logger.error(
{ err: error },
'could not start track-changes server'
)
} else {
return logger.debug(
`trackchanges starting up, listening on ${host}:${port}`
)
}
})
})
2021-07-13 11:04:43 +00:00
.catch(err => {
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
process.exit(1)
})
}
module.exports = app
function __guard__(value, transform) {
return typeof value !== 'undefined' && value !== null
? transform(value)
: undefined
}