2020-01-13 14:01:22 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS103: Rewrite code to no longer use __guard__
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-11-25 06:57:20 -05:00
|
|
|
const metrics = require('@overleaf/metrics')
|
2020-01-13 14:01:23 -05:00
|
|
|
metrics.initialize('notifications')
|
2021-07-12 12:47:16 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-01-13 14:01:23 -05:00
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
logger.initialize('notifications-sharelatex')
|
|
|
|
const express = require('express')
|
|
|
|
const app = express()
|
2020-03-26 10:17:07 -04:00
|
|
|
const methodOverride = require('method-override')
|
|
|
|
const bodyParser = require('body-parser')
|
2020-08-31 04:58:47 -04:00
|
|
|
const mongodb = require('./app/js/mongodb')
|
2020-01-13 14:01:23 -05:00
|
|
|
const controller = require('./app/js/NotificationsController')
|
|
|
|
|
|
|
|
metrics.memory.monitor(logger)
|
|
|
|
|
|
|
|
const HealthCheckController = require('./app/js/HealthCheckController')
|
|
|
|
|
2020-03-26 10:17:07 -04:00
|
|
|
app.use(methodOverride())
|
|
|
|
app.use(bodyParser())
|
|
|
|
app.use(metrics.http.monitor(logger))
|
2020-01-13 14:01:23 -05:00
|
|
|
|
|
|
|
metrics.injectMetricsRoute(app)
|
|
|
|
|
|
|
|
app.post('/user/:user_id', controller.addNotification)
|
|
|
|
app.get('/user/:user_id', controller.getUserNotifications)
|
2020-03-18 11:29:28 -04:00
|
|
|
app.delete(
|
2020-01-13 14:01:23 -05:00
|
|
|
'/user/:user_id/notification/:notification_id',
|
|
|
|
controller.removeNotificationId
|
|
|
|
)
|
2020-03-18 11:29:28 -04:00
|
|
|
app.delete('/user/:user_id', controller.removeNotificationKey)
|
|
|
|
app.delete('/key/:key', controller.removeNotificationByKeyOnly)
|
2021-10-20 06:18:14 -04:00
|
|
|
app.get('/key/:key/count', controller.countNotificationsByKeyOnly)
|
|
|
|
app.delete('/key/:key/bulk', controller.deleteUnreadNotificationsByKeyOnlyBulk)
|
2020-01-13 14:01:23 -05:00
|
|
|
|
|
|
|
app.get('/status', (req, res) => res.send('notifications sharelatex up'))
|
|
|
|
|
|
|
|
app.get('/health_check', (req, res) =>
|
2020-06-04 03:50:05 -04:00
|
|
|
HealthCheckController.check(function (err) {
|
2020-01-13 14:01:23 -05:00
|
|
|
if (err != null) {
|
|
|
|
logger.err({ err }, 'error performing health check')
|
2020-03-18 11:29:28 -04:00
|
|
|
return res.sendStatus(500)
|
2020-01-13 14:01:23 -05:00
|
|
|
} else {
|
2020-03-18 11:29:28 -04:00
|
|
|
return res.sendStatus(200)
|
2020-01-13 14:01:23 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2020-03-18 11:29:28 -04:00
|
|
|
app.get('*', (req, res) => res.sendStatus(404))
|
2020-01-13 14:01:23 -05:00
|
|
|
|
|
|
|
const host =
|
|
|
|
__guard__(
|
|
|
|
Settings.internal != null ? Settings.internal.notifications : undefined,
|
2021-07-13 07:04:44 -04:00
|
|
|
x => x.host
|
2020-01-13 14:01:23 -05:00
|
|
|
) || 'localhost'
|
|
|
|
const port =
|
|
|
|
__guard__(
|
|
|
|
Settings.internal != null ? Settings.internal.notifications : undefined,
|
2021-07-13 07:04:44 -04:00
|
|
|
x1 => x1.port
|
2020-01-13 14:01:23 -05:00
|
|
|
) || 3042
|
2020-08-31 04:58:47 -04:00
|
|
|
|
|
|
|
mongodb
|
|
|
|
.waitForDb()
|
|
|
|
.then(() => {
|
|
|
|
app.listen(port, host, () =>
|
|
|
|
logger.info(`notifications starting up, listening on ${host}:${port}`)
|
|
|
|
)
|
|
|
|
})
|
2021-07-13 07:04:44 -04:00
|
|
|
.catch(err => {
|
2020-08-31 04:58:47 -04:00
|
|
|
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
|
|
|
|
process.exit(1)
|
|
|
|
})
|
2020-01-13 14:01:22 -05:00
|
|
|
|
|
|
|
function __guard__(value, transform) {
|
2020-01-13 14:01:23 -05:00
|
|
|
return typeof value !== 'undefined' && value !== null
|
|
|
|
? transform(value)
|
|
|
|
: undefined
|
|
|
|
}
|