app.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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('metrics-sharelatex')
  9. metrics.initialize('notifications')
  10. const Settings = require('settings-sharelatex')
  11. const logger = require('logger-sharelatex')
  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 errorHandler = require('errorhandler')
  18. const controller = require('./app/js/NotificationsController')
  19. metrics.memory.monitor(logger)
  20. const HealthCheckController = require('./app/js/HealthCheckController')
  21. app.use(methodOverride())
  22. app.use(bodyParser())
  23. app.use(metrics.http.monitor(logger))
  24. app.use(errorHandler())
  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('/status', (req, res) => res.send('notifications sharelatex up'))
  35. app.get('/health_check', (req, res) =>
  36. HealthCheckController.check(function (err) {
  37. if (err != null) {
  38. logger.err({ err }, 'error performing health check')
  39. return res.sendStatus(500)
  40. } else {
  41. return res.sendStatus(200)
  42. }
  43. })
  44. )
  45. app.get('*', (req, res) => res.sendStatus(404))
  46. const host =
  47. __guard__(
  48. Settings.internal != null ? Settings.internal.notifications : undefined,
  49. (x) => x.host
  50. ) || 'localhost'
  51. const port =
  52. __guard__(
  53. Settings.internal != null ? Settings.internal.notifications : undefined,
  54. (x1) => x1.port
  55. ) || 3042
  56. app.listen(port, host, () =>
  57. logger.info(`notifications starting up, listening on ${host}:${port}`)
  58. )
  59. function __guard__(value, transform) {
  60. return typeof value !== 'undefined' && value !== null
  61. ? transform(value)
  62. : undefined
  63. }