app.js 2.5 KB

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