app.js 2.0 KB

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