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-02-17 02:38:26 -05:00
|
|
|
const Metrics = require('metrics-sharelatex')
|
|
|
|
Metrics.initialize('contacts')
|
2020-02-17 02:38:25 -05:00
|
|
|
|
2020-02-17 02:38:26 -05:00
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
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
|
|
|
|
|
|
|
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) {
|
|
|
|
return res.send(404)
|
|
|
|
} else {
|
|
|
|
return res.send(500, 'Oops, something went wrong')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const { port } = Settings.internal.contacts
|
|
|
|
const { host } = Settings.internal.contacts
|
|
|
|
|
|
|
|
if (!module.parent) {
|
|
|
|
// Called directly
|
|
|
|
app.listen(port, host, function(error) {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return logger.info(`contacts starting up, listening on ${host}:${port}`)
|
|
|
|
})
|
2020-02-17 02:38:25 -05:00
|
|
|
}
|
|
|
|
|
2020-02-17 02:38:26 -05:00
|
|
|
module.exports = app
|