app.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * decaffeinate suggestions:
  3. * DS102: Remove unnecessary code created because of implicit returns
  4. * DS103: Rewrite code to no longer use __guard__
  5. * DS207: Consider shorter variations of null checks
  6. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  7. */
  8. const metrics = require('@overleaf/metrics')
  9. metrics.initialize('notifications')
  10. const Settings = require('@overleaf/settings')
  11. const logger = require('logger-sharelatex')
  12. logger.initialize('notifications-sharelatex')
  13. const express = require('express')
  14. const app = express()
  15. const methodOverride = require('method-override')
  16. const bodyParser = require('body-parser')
  17. const mongodb = require('./app/js/mongodb')
  18. const controller = require('./app/js/NotificationsController')
  19. metrics.memory.monitor(logger)
  20. const HealthCheckController = require('./app/js/HealthCheckController')
  21. app.use(methodOverride())
  22. app.use(bodyParser())
  23. app.use(metrics.http.monitor(logger))
  24. metrics.injectMetricsRoute(app)
  25. app.post('/user/:user_id', controller.addNotification)
  26. app.get('/user/:user_id', controller.getUserNotifications)
  27. app.delete(
  28. '/user/:user_id/notification/:notification_id',
  29. controller.removeNotificationId
  30. )
  31. app.delete('/user/:user_id', controller.removeNotificationKey)
  32. app.delete('/key/:key', controller.removeNotificationByKeyOnly)
  33. app.get('/status', (req, res) => res.send('notifications sharelatex up'))
  34. app.get('/health_check', (req, res) =>
  35. HealthCheckController.check(function (err) {
  36. if (err != null) {
  37. logger.err({ err }, 'error performing health check')
  38. return res.sendStatus(500)
  39. } else {
  40. return res.sendStatus(200)
  41. }
  42. })
  43. )
  44. app.get('*', (req, res) => res.sendStatus(404))
  45. const host =
  46. __guard__(
  47. Settings.internal != null ? Settings.internal.notifications : undefined,
  48. x => x.host
  49. ) || 'localhost'
  50. const port =
  51. __guard__(
  52. Settings.internal != null ? Settings.internal.notifications : undefined,
  53. x1 => x1.port
  54. ) || 3042
  55. mongodb
  56. .waitForDb()
  57. .then(() => {
  58. app.listen(port, host, () =>
  59. logger.info(`notifications starting up, listening on ${host}:${port}`)
  60. )
  61. })
  62. .catch(err => {
  63. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  64. process.exit(1)
  65. })
  66. function __guard__(value, transform) {
  67. return typeof value !== 'undefined' && value !== null
  68. ? transform(value)
  69. : undefined
  70. }