app.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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, {
  7. type Request,
  8. type Response,
  9. type ErrorRequestHandler,
  10. type NextFunction,
  11. } from 'express'
  12. import { mongoClient } from './app/js/mongodb.js'
  13. import NotificationsController from './app/js/NotificationsController.ts'
  14. import HealthCheckController from './app/js/HealthCheckController.ts'
  15. import {
  16. InvalidParamsError,
  17. InvalidRequestError,
  18. } from '@overleaf/validation-tools'
  19. const app = express()
  20. logger.initialize('notifications')
  21. metrics.memory.monitor(logger)
  22. metrics.open_sockets.monitor()
  23. app.use(express.json())
  24. app.use(metrics.http.monitor(logger))
  25. metrics.injectMetricsRoute(app)
  26. app.post('/user/:user_id', NotificationsController.addNotification)
  27. app.get('/user/:user_id', NotificationsController.getUserNotifications)
  28. app.delete(
  29. '/user/:user_id/notification/:notification_id',
  30. NotificationsController.removeNotificationId
  31. )
  32. app.delete('/user/:user_id', NotificationsController.removeNotificationKey)
  33. app.delete('/key/:key', NotificationsController.removeNotificationByKeyOnly)
  34. app.get('/key/:key/count', NotificationsController.countNotificationsByKeyOnly)
  35. app.delete(
  36. '/key/:key/bulk',
  37. NotificationsController.deleteUnreadNotificationsByKeyOnlyBulk
  38. )
  39. app.get('/status', (req, res) => res.send('notifications is up'))
  40. app.get('/health_check', HealthCheckController.check)
  41. app.get('*', (req, res) => res.sendStatus(404))
  42. const handleApiError: ErrorRequestHandler = (
  43. err: Error,
  44. req: Request,
  45. res: Response,
  46. next: NextFunction
  47. ) => {
  48. req.logger.addFields({ err })
  49. if (err instanceof InvalidParamsError) {
  50. req.logger.setLevel('warn')
  51. res.sendStatus(404)
  52. } else if (err instanceof InvalidRequestError) {
  53. req.logger.setLevel('warn')
  54. res.sendStatus(400)
  55. } else {
  56. req.logger.setLevel('error')
  57. res.sendStatus(500)
  58. }
  59. }
  60. app.use(handleApiError)
  61. const host = Settings.internal.notifications?.host || '127.0.0.1'
  62. const port = Settings.internal.notifications?.port || 3042
  63. try {
  64. await mongoClient.connect()
  65. } catch (err) {
  66. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  67. process.exit(1)
  68. }
  69. if (import.meta.url === `file://${process.argv[1]}`) {
  70. app.listen(port, host, () =>
  71. logger.debug({}, `notifications starting up, listening on ${host}:${port}`)
  72. )
  73. }
  74. export default app