app.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. const mongojs = require('mongojs')
  20. const db = mongojs(Settings.mongo.url, ['notifications'])
  21. const Path = require('path')
  22. metrics.memory.monitor(logger)
  23. const HealthCheckController = require('./app/js/HealthCheckController')
  24. app.use(methodOverride())
  25. app.use(bodyParser())
  26. app.use(metrics.http.monitor(logger))
  27. app.use(errorHandler())
  28. metrics.injectMetricsRoute(app)
  29. app.post('/user/:user_id', controller.addNotification)
  30. app.get('/user/:user_id', controller.getUserNotifications)
  31. app.delete(
  32. '/user/:user_id/notification/:notification_id',
  33. controller.removeNotificationId
  34. )
  35. app.delete('/user/:user_id', controller.removeNotificationKey)
  36. app.delete('/key/:key', controller.removeNotificationByKeyOnly)
  37. app.get('/status', (req, res) => res.send('notifications sharelatex up'))
  38. app.get('/health_check', (req, res) =>
  39. HealthCheckController.check(function(err) {
  40. if (err != null) {
  41. logger.err({ err }, 'error performing health check')
  42. return res.sendStatus(500)
  43. } else {
  44. return res.sendStatus(200)
  45. }
  46. })
  47. )
  48. app.get('*', (req, res) => res.sendStatus(404))
  49. const host =
  50. __guard__(
  51. Settings.internal != null ? Settings.internal.notifications : undefined,
  52. x => x.host
  53. ) || 'localhost'
  54. const port =
  55. __guard__(
  56. Settings.internal != null ? Settings.internal.notifications : undefined,
  57. x1 => x1.port
  58. ) || 3042
  59. app.listen(port, host, () =>
  60. logger.info(`notifications starting up, listening on ${host}:${port}`)
  61. )
  62. function __guard__(value, transform) {
  63. return typeof value !== 'undefined' && value !== null
  64. ? transform(value)
  65. : undefined
  66. }