app.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Metrics must be initialized before importing anything else
  2. require('@overleaf/metrics/initialize')
  3. const metrics = require('@overleaf/metrics')
  4. const Settings = require('@overleaf/settings')
  5. const logger = require('@overleaf/logger')
  6. logger.initialize('notifications')
  7. const express = require('express')
  8. const app = express()
  9. const methodOverride = require('method-override')
  10. const bodyParser = require('body-parser')
  11. const { mongoClient } = require('./app/js/mongodb')
  12. const controller = require('./app/js/NotificationsController')
  13. metrics.memory.monitor(logger)
  14. metrics.open_sockets.monitor()
  15. const HealthCheckController = require('./app/js/HealthCheckController')
  16. app.use(methodOverride())
  17. app.use(bodyParser())
  18. app.use(metrics.http.monitor(logger))
  19. metrics.injectMetricsRoute(app)
  20. app.post('/user/:user_id', controller.addNotification)
  21. app.get('/user/:user_id', controller.getUserNotifications)
  22. app.delete(
  23. '/user/:user_id/notification/:notification_id',
  24. controller.removeNotificationId
  25. )
  26. app.delete('/user/:user_id', controller.removeNotificationKey)
  27. app.delete('/key/:key', controller.removeNotificationByKeyOnly)
  28. app.get('/key/:key/count', controller.countNotificationsByKeyOnly)
  29. app.delete('/key/:key/bulk', controller.deleteUnreadNotificationsByKeyOnlyBulk)
  30. app.get('/status', (req, res) => res.send('notifications is up'))
  31. app.get('/health_check', (req, res) =>
  32. HealthCheckController.check(function (err) {
  33. if (err) {
  34. logger.err({ err }, 'error performing health check')
  35. res.sendStatus(500)
  36. } else {
  37. res.sendStatus(200)
  38. }
  39. })
  40. )
  41. app.get('*', (req, res) => res.sendStatus(404))
  42. const host = Settings.internal?.notifications?.host || 'localhost'
  43. const port = Settings.internal?.notifications?.port || 3042
  44. mongoClient
  45. .connect()
  46. .then(() => {
  47. app.listen(port, host, () =>
  48. logger.debug(`notifications starting up, listening on ${host}:${port}`)
  49. )
  50. })
  51. .catch(err => {
  52. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  53. process.exit(1)
  54. })