app.js 2.2 KB

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