mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
5638f06b3a
[metrics] collect HTTP socket stats for all the services GitOrigin-RevId: 52872f4ca46d8cf351f7f4b4df90f9d6b05010b3
84 lines
2.5 KiB
JavaScript
84 lines
2.5 KiB
JavaScript
/*
|
|
* 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
|
|
*/
|
|
const metrics = require('@overleaf/metrics')
|
|
metrics.initialize('notifications')
|
|
const Settings = require('@overleaf/settings')
|
|
const logger = require('@overleaf/logger')
|
|
logger.initialize('notifications-sharelatex')
|
|
const express = require('express')
|
|
const app = express()
|
|
const methodOverride = require('method-override')
|
|
const bodyParser = require('body-parser')
|
|
const { mongoClient } = require('./app/js/mongodb')
|
|
const controller = require('./app/js/NotificationsController')
|
|
|
|
metrics.memory.monitor(logger)
|
|
metrics.open_sockets.monitor()
|
|
|
|
const HealthCheckController = require('./app/js/HealthCheckController')
|
|
|
|
app.use(methodOverride())
|
|
app.use(bodyParser())
|
|
app.use(metrics.http.monitor(logger))
|
|
|
|
metrics.injectMetricsRoute(app)
|
|
|
|
app.post('/user/:user_id', controller.addNotification)
|
|
app.get('/user/:user_id', controller.getUserNotifications)
|
|
app.delete(
|
|
'/user/:user_id/notification/:notification_id',
|
|
controller.removeNotificationId
|
|
)
|
|
app.delete('/user/:user_id', controller.removeNotificationKey)
|
|
app.delete('/key/:key', controller.removeNotificationByKeyOnly)
|
|
app.get('/key/:key/count', controller.countNotificationsByKeyOnly)
|
|
app.delete('/key/:key/bulk', controller.deleteUnreadNotificationsByKeyOnlyBulk)
|
|
|
|
app.get('/status', (req, res) => res.send('notifications sharelatex up'))
|
|
|
|
app.get('/health_check', (req, res) =>
|
|
HealthCheckController.check(function (err) {
|
|
if (err != null) {
|
|
logger.err({ err }, 'error performing health check')
|
|
return res.sendStatus(500)
|
|
} else {
|
|
return res.sendStatus(200)
|
|
}
|
|
})
|
|
)
|
|
|
|
app.get('*', (req, res) => res.sendStatus(404))
|
|
|
|
const host =
|
|
__guard__(
|
|
Settings.internal != null ? Settings.internal.notifications : undefined,
|
|
x => x.host
|
|
) || 'localhost'
|
|
const port =
|
|
__guard__(
|
|
Settings.internal != null ? Settings.internal.notifications : undefined,
|
|
x1 => x1.port
|
|
) || 3042
|
|
|
|
mongoClient
|
|
.connect()
|
|
.then(() => {
|
|
app.listen(port, host, () =>
|
|
logger.debug(`notifications starting up, listening on ${host}:${port}`)
|
|
)
|
|
})
|
|
.catch(err => {
|
|
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
|
|
process.exit(1)
|
|
})
|
|
|
|
function __guard__(value, transform) {
|
|
return typeof value !== 'undefined' && value !== null
|
|
? transform(value)
|
|
: undefined
|
|
}
|