app.ts 2.5 KB

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