2020-02-17 02:38:25 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-11-25 06:57:24 -05:00
|
|
|
const Metrics = require('@overleaf/metrics')
|
2020-02-17 02:38:26 -05:00
|
|
|
Metrics.initialize('contacts')
|
2020-02-17 02:38:25 -05:00
|
|
|
|
2021-07-12 12:47:20 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-02-17 02:38:26 -05:00
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
2020-08-21 11:30:18 -04:00
|
|
|
const mongodb = require('./app/js/mongodb')
|
2020-02-17 02:38:26 -05:00
|
|
|
const Errors = require('./app/js/Errors')
|
|
|
|
const HttpController = require('./app/js/HttpController')
|
2020-02-17 02:38:25 -05:00
|
|
|
|
2020-02-17 02:38:26 -05:00
|
|
|
logger.initialize('contacts')
|
2020-02-17 02:38:25 -05:00
|
|
|
if (Metrics.event_loop != null) {
|
2020-02-17 02:38:26 -05:00
|
|
|
Metrics.event_loop.monitor(logger)
|
2020-02-17 02:38:25 -05:00
|
|
|
}
|
|
|
|
|
2020-02-17 02:38:26 -05:00
|
|
|
const app = express()
|
2020-02-17 02:38:25 -05:00
|
|
|
|
2020-02-17 02:38:26 -05:00
|
|
|
app.use(Metrics.http.monitor(logger))
|
2020-02-17 02:38:25 -05:00
|
|
|
|
2020-02-17 02:38:26 -05:00
|
|
|
Metrics.injectMetricsRoute(app)
|
2020-02-17 02:38:25 -05:00
|
|
|
|
2020-02-17 02:38:26 -05:00
|
|
|
app.get('/user/:user_id/contacts', HttpController.getContacts)
|
|
|
|
app.post(
|
|
|
|
'/user/:user_id/contacts',
|
|
|
|
bodyParser.json({ limit: '2mb' }),
|
|
|
|
HttpController.addContact
|
|
|
|
)
|
2020-02-17 02:38:25 -05:00
|
|
|
|
2020-02-17 02:38:26 -05:00
|
|
|
app.get('/status', (req, res) => res.send('contacts is alive'))
|
2020-02-17 02:38:25 -05:00
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
app.use(function (error, req, res, next) {
|
2020-02-17 02:38:26 -05:00
|
|
|
logger.error({ err: error }, 'request errored')
|
|
|
|
if (error instanceof Errors.NotFoundError) {
|
2020-03-18 10:08:14 -04:00
|
|
|
return res.sendStatus(404)
|
2020-02-17 02:38:26 -05:00
|
|
|
} else {
|
2020-03-18 10:08:14 -04:00
|
|
|
return res.status(500).send('Oops, something went wrong')
|
2020-02-17 02:38:26 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const { port } = Settings.internal.contacts
|
|
|
|
const { host } = Settings.internal.contacts
|
|
|
|
|
|
|
|
if (!module.parent) {
|
|
|
|
// Called directly
|
2020-08-21 11:30:18 -04:00
|
|
|
mongodb
|
|
|
|
.waitForDb()
|
|
|
|
.then(() => {
|
|
|
|
app.listen(port, host, function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.fatal({ err }, `Cannot bind to ${host}:${port}. Exiting.`)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
2022-05-16 08:38:18 -04:00
|
|
|
return logger.debug(
|
|
|
|
`contacts starting up, listening on ${host}:${port}`
|
|
|
|
)
|
2020-08-21 11:30:18 -04:00
|
|
|
})
|
|
|
|
})
|
2021-07-13 07:04:47 -04:00
|
|
|
.catch(err => {
|
2020-08-21 11:30:18 -04:00
|
|
|
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
|
|
|
|
process.exit(1)
|
|
|
|
})
|
2020-02-17 02:38:25 -05:00
|
|
|
}
|
|
|
|
|
2020-02-17 02:38:26 -05:00
|
|
|
module.exports = app
|