2019-01-08 11:11:56 -05:00
|
|
|
Metrics = require "metrics-sharelatex"
|
|
|
|
Metrics.initialize("track-changes")
|
2014-01-27 12:51:09 -05:00
|
|
|
Settings = require "settings-sharelatex"
|
2013-08-23 09:35:13 -04:00
|
|
|
logger = require "logger-sharelatex"
|
2016-01-26 06:28:02 -05:00
|
|
|
TrackChangesLogger = logger.initialize("track-changes").logger
|
2013-08-23 09:35:13 -04:00
|
|
|
|
2015-12-07 10:52:21 -05:00
|
|
|
if Settings.sentry?.dsn?
|
2015-12-08 05:44:26 -05:00
|
|
|
logger.initializeErrorReporting(Settings.sentry.dsn)
|
2015-12-07 10:52:21 -05:00
|
|
|
|
2016-01-26 06:28:02 -05:00
|
|
|
# log updates as truncated strings
|
|
|
|
truncateFn = (updates) ->
|
2016-01-26 09:54:06 -05:00
|
|
|
JSON.parse(
|
|
|
|
JSON.stringify updates, (key, value) ->
|
|
|
|
if typeof value == 'string' && (len = value.length) > 80
|
|
|
|
return value.substr(0,32) + "...(message of length #{len} truncated)..." + value.substr(-32)
|
|
|
|
else
|
|
|
|
return value
|
|
|
|
)
|
2016-01-26 06:28:02 -05:00
|
|
|
|
|
|
|
TrackChangesLogger.addSerializers {
|
2016-01-26 09:54:06 -05:00
|
|
|
rawUpdate: truncateFn
|
2016-01-26 06:28:02 -05:00
|
|
|
rawUpdates: truncateFn
|
|
|
|
newUpdates: truncateFn
|
|
|
|
lastUpdate: truncateFn
|
|
|
|
}
|
|
|
|
|
2014-05-09 07:44:13 -04:00
|
|
|
Path = require "path"
|
2019-01-08 11:11:56 -05:00
|
|
|
|
2016-01-14 11:24:13 -05:00
|
|
|
Metrics.memory.monitor(logger)
|
2014-05-09 07:44:13 -04:00
|
|
|
|
2015-06-04 11:24:35 -04:00
|
|
|
child_process = require "child_process"
|
|
|
|
|
2014-01-27 13:09:37 -05:00
|
|
|
HttpController = require "./app/js/HttpController"
|
|
|
|
express = require "express"
|
|
|
|
app = express()
|
|
|
|
|
2014-05-09 07:44:13 -04:00
|
|
|
app.use Metrics.http.monitor(logger)
|
2014-02-26 07:11:45 -05:00
|
|
|
|
2019-01-08 11:11:56 -05:00
|
|
|
Metrics.injectMetricsRoute(app)
|
|
|
|
|
2014-03-21 11:57:17 -04:00
|
|
|
app.post "/project/:project_id/doc/:doc_id/flush", HttpController.flushDoc
|
2013-08-23 09:35:13 -04:00
|
|
|
|
2014-03-04 10:27:03 -05:00
|
|
|
app.get "/project/:project_id/doc/:doc_id/diff", HttpController.getDiff
|
|
|
|
|
2015-12-09 09:57:04 -05:00
|
|
|
app.get "/project/:project_id/doc/:doc_id/check", HttpController.checkDoc
|
|
|
|
|
2014-03-20 08:10:04 -04:00
|
|
|
app.get "/project/:project_id/updates", HttpController.getUpdates
|
2014-03-05 10:59:40 -05:00
|
|
|
|
2014-03-21 11:57:17 -04:00
|
|
|
app.post "/project/:project_id/flush", HttpController.flushProject
|
|
|
|
|
2014-03-10 12:58:26 -04:00
|
|
|
app.post "/project/:project_id/doc/:doc_id/version/:version/restore", HttpController.restore
|
|
|
|
|
2016-03-09 11:56:49 -05:00
|
|
|
app.post '/project/:project_id/doc/:doc_id/push', HttpController.pushDocHistory
|
|
|
|
app.post '/project/:project_id/doc/:doc_id/pull', HttpController.pullDocHistory
|
|
|
|
|
2017-04-12 11:34:28 -04:00
|
|
|
app.post '/flush/all', HttpController.flushAll
|
2017-04-13 06:31:45 -04:00
|
|
|
app.post '/check/dangling', HttpController.checkDanglingUpdates
|
2017-04-12 11:34:28 -04:00
|
|
|
|
2015-06-04 11:24:35 -04:00
|
|
|
packWorker = null # use a single packing worker
|
|
|
|
|
|
|
|
app.post "/pack", (req, res, next) ->
|
|
|
|
if packWorker?
|
|
|
|
res.send "pack already running"
|
|
|
|
else
|
|
|
|
logger.log "running pack"
|
2015-06-05 08:38:47 -04:00
|
|
|
packWorker = child_process.fork(__dirname + '/app/js/PackWorker.js',
|
2017-05-09 06:33:21 -04:00
|
|
|
[req.query.limit || 1000, req.query.delay || 1000, req.query.timeout || 30*60*1000])
|
2015-06-04 11:24:35 -04:00
|
|
|
packWorker.on 'exit', (code, signal) ->
|
|
|
|
logger.log {code, signal}, "history auto pack exited"
|
|
|
|
packWorker = null
|
|
|
|
res.send "pack started"
|
|
|
|
|
2014-03-03 08:14:01 -05:00
|
|
|
app.get "/status", (req, res, next) ->
|
|
|
|
res.send "track-changes is alive"
|
|
|
|
|
2015-12-07 11:17:17 -05:00
|
|
|
app.get "/oops", (req, res, next) ->
|
|
|
|
throw new Error("dummy test error")
|
|
|
|
|
2015-10-29 06:52:23 -04:00
|
|
|
app.get "/check_lock", HttpController.checkLock
|
|
|
|
|
2015-10-19 05:59:39 -04:00
|
|
|
app.get "/health_check", HttpController.healthCheck
|
|
|
|
|
2013-08-23 09:35:13 -04:00
|
|
|
app.use (error, req, res, next) ->
|
2016-01-05 10:41:06 -05:00
|
|
|
logger.error err: error, req: req, "an internal error occured"
|
2014-02-26 07:11:45 -05:00
|
|
|
res.send 500
|
2013-08-23 09:35:13 -04:00
|
|
|
|
2014-03-03 08:31:10 -05:00
|
|
|
port = Settings.internal?.trackchanges?.port or 3015
|
2014-02-26 11:17:15 -05:00
|
|
|
host = Settings.internal?.trackchanges?.host or "localhost"
|
2018-05-24 07:02:27 -04:00
|
|
|
|
|
|
|
if !module.parent # Called directly
|
|
|
|
app.listen port, host, (error) ->
|
|
|
|
if error?
|
|
|
|
logger.error err: error, "could not start track-changes server"
|
|
|
|
else
|
|
|
|
logger.info "trackchanges starting up, listening on #{host}:#{port}"
|
|
|
|
|
|
|
|
module.exports = app
|
2014-01-27 12:51:09 -05:00
|
|
|
|