app.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * decaffeinate suggestions:
  3. * DS102: Remove unnecessary code created because of implicit returns
  4. * DS207: Consider shorter variations of null checks
  5. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  6. */
  7. const Metrics = require('metrics-sharelatex')
  8. Metrics.initialize('contacts')
  9. const Settings = require('settings-sharelatex')
  10. const logger = require('logger-sharelatex')
  11. const express = require('express')
  12. const bodyParser = require('body-parser')
  13. const Errors = require('./app/js/Errors')
  14. const HttpController = require('./app/js/HttpController')
  15. logger.initialize('contacts')
  16. if (Metrics.event_loop != null) {
  17. Metrics.event_loop.monitor(logger)
  18. }
  19. const app = express()
  20. app.use(Metrics.http.monitor(logger))
  21. Metrics.injectMetricsRoute(app)
  22. app.get('/user/:user_id/contacts', HttpController.getContacts)
  23. app.post(
  24. '/user/:user_id/contacts',
  25. bodyParser.json({ limit: '2mb' }),
  26. HttpController.addContact
  27. )
  28. app.get('/status', (req, res) => res.send('contacts is alive'))
  29. app.use(function(error, req, res, next) {
  30. logger.error({ err: error }, 'request errored')
  31. if (error instanceof Errors.NotFoundError) {
  32. return res.send(404)
  33. } else {
  34. return res.send(500, 'Oops, something went wrong')
  35. }
  36. })
  37. const { port } = Settings.internal.contacts
  38. const { host } = Settings.internal.contacts
  39. if (!module.parent) {
  40. // Called directly
  41. app.listen(port, host, function(error) {
  42. if (error != null) {
  43. throw error
  44. }
  45. return logger.info(`contacts starting up, listening on ${host}:${port}`)
  46. })
  47. }
  48. module.exports = app