overleaf/services/track-changes/app.js

164 lines
4.4 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 10:52:21 -05:00
// log updates as truncated strings
2021-07-13 07:04:43 -04:00
const truncateFn = updates =>
JSON.parse(
2020-06-04 04:24:21 -04: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 07:04:43 -04:00
lastUpdate: truncateFn,
})
2016-01-26 06:28:02 -05:00
const Path = require('path')
Metrics.memory.monitor(logger)
2014-05-09 07:44:13 -04:00
2020-06-04 04:37:43 -04: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 04:24:21 -04:00
const bodyParser = require('body-parser')
2020-03-23 06:10:24 -04:00
const app = express()
2020-03-23 06:10:24 -04:00
app.use(bodyParser.json())
app.use(Metrics.http.monitor(logger))
2014-02-26 07:11:45 -05:00
Metrics.injectMetricsRoute(app)
app.post('/project/:project_id/doc/:doc_id/flush', HttpController.flushDoc)
2013-08-23 09:35:13 -04: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 09:57:04 -05: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 11:57:17 -04:00
app.post(
'/project/:project_id/doc/:doc_id/version/:version/restore',
HttpController.restore
)
2014-03-10 12:58:26 -04: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 11:56:49 -05:00
app.post('/flush/all', HttpController.flushAll)
app.post('/check/dangling', HttpController.checkDanglingUpdates)
let packWorker = null // use a single packing worker
2020-06-04 04:24:21 -04:00
app.post('/pack', function (req, res, next) {
if (packWorker != null) {
return res.send('pack already running')
} else {
logger.log('running pack')
2020-06-04 04:37:43 -04:00
packWorker = childProcess.fork(
Path.join(__dirname, '/app/js/PackWorker.js'),
[
req.query.limit || 1000,
req.query.delay || 1000,
2021-07-13 07:04:43 -04:00
req.query.timeout || 30 * 60 * 1000,
2020-06-04 04:37:43 -04:00
]
)
2020-06-04 04:24:21 -04:00
packWorker.on('exit', function (code, signal) {
logger.log({ 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 04:24:21 -04: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 04:24:21 -04: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 07:04:43 -04:00
x => x.port
) || 3015
const host =
__guard__(
Settings.internal != null ? Settings.internal.trackchanges : undefined,
2021-07-13 07:04:43 -04: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.info(
`trackchanges starting up, listening on ${host}:${port}`
)
}
})
})
2021-07-13 07:04:43 -04: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
}