overleaf/services/docstore/app.coffee

84 lines
2.5 KiB
CoffeeScript
Raw Normal View History

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 11:52:58 -04:00
Metrics = require "metrics-sharelatex"
Path = require "path"
2014-05-06 11:52:58 -04:00
Metrics.initialize("docstore")
logger.initialize("docstore")
2014-05-06 11:52:58 -04:00
Metrics.mongodb.monitor(Path.resolve(__dirname + "/node_modules/mongojs/node_modules/mongodb"), logger)
2015-06-23 08:55:33 -04:00
Metrics.event_loop?.monitor(logger)
app = express()
2014-05-07 05:59:16 -04:00
app.use Metrics.http.monitor(logger)
2014-04-30 08:06:12 -04:00
app.get '/project/:project_id/doc', HttpController.getAllDocs
app.get '/project/:project_id/doc/:doc_id', HttpController.getDoc
2014-05-20 08:04:33 -04:00
app.get '/project/:project_id/doc/:doc_id/raw', HttpController.getRawDoc
2014-05-13 07:54:58 -04:00
app.post '/project/:project_id/doc/:doc_id', bodyParser.json(limit: "2mb"), HttpController.updateDoc
app.del '/project/:project_id/doc/:doc_id', HttpController.deleteDoc
2015-08-13 17:48:45 -04:00
app.post '/project/:project_id/archive', HttpController.archiveAllDocs
app.post '/project/:project_id/unarchive', HttpController.unArchiveAllDocs
2015-10-15 17:22:20 -04:00
ObjectId = require("mongojs").ObjectId
request = require("request")
async = require("async")
_ = require("underscore")
crypto = require("crypto")
app.get "/health_check", (req, res)->
doc_id = ObjectId()
project_id = ObjectId()
url = "http://localhost:#{port}/project/#{project_id}/doc/#{doc_id}"
lines = ["smoke test - delete me", "#{crypto.randomBytes(32).toString("hex")}"]
logger.log lines:lines, url:url, doc_id:doc_id, project_id:project_id, "running health check"
jobs = [
(cb)->
opts =
url:url
json: {lines: lines}
request.post(opts, cb)
(cb)->
request.get {url:url, json:true}, (err, res, body)->
if res.statusCode != 200
cb("status code not 200, its #{res.statusCode}")
else if _.isEqual(body.lines, lines) and body._id == doc_id.toString()
cb()
else
cb("lines not equal ")
(cb)->
request.del url, cb
]
async.series jobs, (err)->
if err?
logger.err err:err, "error running health check"
res.send 500
else
res.send()
app.get '/status', (req, res)->
res.send('docstore is alive')
app.use (error, req, res, next) ->
logger.error err: error, "request errored"
2014-04-28 12:43:19 -04: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
app.listen port, host, (error) ->
throw error if error?
2015-04-30 10:04:26 -04:00
logger.info "Docstore starting up, listening on #{host}:#{port}"