overleaf/services/docstore/app.coffee

68 lines
2.1 KiB
CoffeeScript
Raw Normal View History

2018-11-20 17:21:10 +00:00
Metrics = require "metrics-sharelatex"
2019-01-09 08:49:32 +00:00
Metrics.initialize("docstore")
Settings = require "settings-sharelatex"
logger = require "logger-sharelatex"
express = require "express"
bodyParser = require "body-parser"
Errors = require "./app/js/Errors"
HttpController = require "./app/js/HttpController"
2014-05-06 15:52:58 +00:00
Path = require "path"
2019-01-09 08:49:32 +00:00
logger.initialize("docstore")
2015-06-23 12:55:33 +00:00
Metrics.event_loop?.monitor(logger)
app = express()
2014-05-07 09:59:16 +00:00
app.use Metrics.http.monitor(logger)
2019-01-09 08:49:32 +00:00
Metrics.injectMetricsRoute(app)
2016-09-02 12:33:30 +00:00
app.param 'project_id', (req, res, next, project_id) ->
if project_id?.match /^[0-9a-f]{24}$/
next()
else
next new Error("invalid project id")
app.param 'doc_id', (req, res, next, doc_id) ->
if doc_id?.match /^[0-9a-f]{24}$/
next()
else
next new Error("invalid doc id")
2018-11-20 14:58:19 +00:00
Metrics.injectMetricsRoute(app)
2014-04-30 12:06:12 +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
2014-05-20 12:04:33 +00:00
app.get '/project/:project_id/doc/:doc_id/raw', HttpController.getRawDoc
# Add 64kb overhead for the JSON encoding, and double the size to allow for ranges in the json payload
app.post '/project/:project_id/doc/:doc_id', bodyParser.json(limit: (Settings.max_doc_length + 64 * 1024) * 2), HttpController.updateDoc
app.del '/project/:project_id/doc/:doc_id', HttpController.deleteDoc
2015-08-13 21:48:45 +00:00
app.post '/project/:project_id/archive', HttpController.archiveAllDocs
app.post '/project/:project_id/unarchive', HttpController.unArchiveAllDocs
app.post '/project/:project_id/destroy', HttpController.destroyAllDocs
app.get "/health_check", HttpController.healthCheck
2015-10-15 21:22:20 +00:00
app.get '/status', (req, res)->
res.send('docstore is alive')
app.use (error, req, res, next) ->
2019-07-31 11:40:59 +00:00
logger.error err: error, req:req, "request errored"
2014-04-28 16:43:19 +00:00
if error instanceof Errors.NotFoundError
res.send 404
else
res.send(500, "Oops, something went wrong")
port = Settings.internal.docstore.port
host = Settings.internal.docstore.host
2018-05-23 10:27:31 +00:00
if !module.parent # Called directly
app.listen port, host, (error) ->
throw error if error?
logger.info "Docstore starting up, listening on #{host}:#{port}"
2019-01-09 08:49:32 +00:00
module.exports = app