app.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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('settings-sharelatex')
  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 errorHandler = require('errorhandler')
  18. const mongodb = require('./app/js/mongodb')
  19. const controller = require('./app/js/NotificationsController')
  20. metrics.memory.monitor(logger)
  21. const HealthCheckController = require('./app/js/HealthCheckController')
  22. app.use(methodOverride())
  23. app.use(bodyParser())
  24. app.use(metrics.http.monitor(logger))
  25. app.use(errorHandler())
  26. metrics.injectMetricsRoute(app)
  27. app.post('/user/:user_id', controller.addNotification)
  28. app.get('/user/:user_id', controller.getUserNotifications)
  29. app.delete(
  30. '/user/:user_id/notification/:notification_id',
  31. controller.removeNotificationId
  32. )
  33. app.delete('/user/:user_id', controller.removeNotificationKey)
  34. app.delete('/key/:key', controller.removeNotificationByKeyOnly)
  35. app.get('/status', (req, res) => res.send('notifications sharelatex up'))
  36. app.get('/health_check', (req, res) =>
  37. HealthCheckController.check(function (err) {
  38. if (err != null) {
  39. logger.err({ err }, 'error performing health check')
  40. return res.sendStatus(500)
  41. } else {
  42. return res.sendStatus(200)
  43. }
  44. })
  45. )
  46. app.get('*', (req, res) => res.sendStatus(404))
  47. const host =
  48. __guard__(
  49. Settings.internal != null ? Settings.internal.notifications : undefined,
  50. (x) => x.host
  51. ) || 'localhost'
  52. const port =
  53. __guard__(
  54. Settings.internal != null ? Settings.internal.notifications : undefined,
  55. (x1) => x1.port
  56. ) || 3042
  57. mongodb
  58. .waitForDb()
  59. .then(() => {
  60. app.listen(port, host, () =>
  61. logger.info(`notifications starting up, listening on ${host}:${port}`)
  62. )
  63. })
  64. .catch((err) => {
  65. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  66. process.exit(1)
  67. })
  68. function __guard__(value, transform) {
  69. return typeof value !== 'undefined' && value !== null
  70. ? transform(value)
  71. : undefined
  72. }