2020-02-17 07:38:25 +00: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 11:57:24 +00:00
|
|
|
const Metrics = require('@overleaf/metrics')
|
2020-02-17 07:38:26 +00:00
|
|
|
Metrics.initialize('contacts')
|
2020-02-17 07:38:25 +00:00
|
|
|
|
2021-07-12 16:47:20 +00:00
|
|
|
const Settings = require('@overleaf/settings')
|
2021-12-14 13:00:35 +00:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-02-17 07:38:26 +00:00
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
2020-08-21 15:30:18 +00:00
|
|
|
const mongodb = require('./app/js/mongodb')
|
2020-02-17 07:38:26 +00:00
|
|
|
const Errors = require('./app/js/Errors')
|
|
|
|
const HttpController = require('./app/js/HttpController')
|
2020-02-17 07:38:25 +00:00
|
|
|
|
2020-02-17 07:38:26 +00:00
|
|
|
logger.initialize('contacts')
|
2020-02-17 07:38:25 +00:00
|
|
|
if (Metrics.event_loop != null) {
|
2020-02-17 07:38:26 +00:00
|
|
|
Metrics.event_loop.monitor(logger)
|
2020-02-17 07:38:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 07:38:26 +00:00
|
|
|
const app = express()
|
2020-02-17 07:38:25 +00:00
|
|
|
|
2020-02-17 07:38:26 +00:00
|
|
|
app.use(Metrics.http.monitor(logger))
|
2020-02-17 07:38:25 +00:00
|
|
|
|
2020-02-17 07:38:26 +00:00
|
|
|
Metrics.injectMetricsRoute(app)
|
2020-02-17 07:38:25 +00:00
|
|
|
|
2020-02-17 07:38:26 +00:00
|
|
|
app.get('/user/:user_id/contacts', HttpController.getContacts)
|
|
|
|
app.post(
|
|
|
|
'/user/:user_id/contacts',
|
|
|
|
bodyParser.json({ limit: '2mb' }),
|
|
|
|
HttpController.addContact
|
|
|
|
)
|
2020-02-17 07:38:25 +00:00
|
|
|
|
2020-02-17 07:38:26 +00:00
|
|
|
app.get('/status', (req, res) => res.send('contacts is alive'))
|
2020-02-17 07:38:25 +00:00
|
|
|
|
2020-06-04 07:49:46 +00:00
|
|
|
app.use(function (error, req, res, next) {
|
2020-02-17 07:38:26 +00:00
|
|
|
logger.error({ err: error }, 'request errored')
|
|
|
|
if (error instanceof Errors.NotFoundError) {
|
2020-03-18 14:08:14 +00:00
|
|
|
return res.sendStatus(404)
|
2020-02-17 07:38:26 +00:00
|
|
|
} else {
|
2020-03-18 14:08:14 +00:00
|
|
|
return res.status(500).send('Oops, something went wrong')
|
2020-02-17 07:38:26 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const { port } = Settings.internal.contacts
|
|
|
|
const { host } = Settings.internal.contacts
|
|
|
|
|
|
|
|
if (!module.parent) {
|
|
|
|
// Called directly
|
2020-08-21 15:30:18 +00:00
|
|
|
mongodb
|
|
|
|
.waitForDb()
|
|
|
|
.then(() => {
|
|
|
|
app.listen(port, host, function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.fatal({ err }, `Cannot bind to ${host}:${port}. Exiting.`)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
return logger.info(`contacts starting up, listening on ${host}:${port}`)
|
|
|
|
})
|
|
|
|
})
|
2021-07-13 11:04:47 +00:00
|
|
|
.catch(err => {
|
2020-08-21 15:30:18 +00:00
|
|
|
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
|
|
|
|
process.exit(1)
|
|
|
|
})
|
2020-02-17 07:38:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 07:38:26 +00:00
|
|
|
module.exports = app
|