app.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * decaffeinate suggestions:
  3. * DS102: Remove unnecessary code created because of implicit returns
  4. * DS103: Rewrite code to no longer use __guard__
  5. * DS207: Consider shorter variations of null checks
  6. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  7. */
  8. const metrics = require('@overleaf/metrics')
  9. metrics.initialize('spelling')
  10. const Settings = require('@overleaf/settings')
  11. const logger = require('logger-sharelatex')
  12. logger.initialize('spelling')
  13. if ((Settings.sentry != null ? Settings.sentry.dsn : undefined) != null) {
  14. logger.initializeErrorReporting(Settings.sentry.dsn)
  15. }
  16. metrics.memory.monitor(logger)
  17. const mongodb = require('./app/js/mongodb')
  18. const SpellingAPIController = require('./app/js/SpellingAPIController')
  19. const express = require('express')
  20. const app = express()
  21. metrics.injectMetricsRoute(app)
  22. const bodyParser = require('body-parser')
  23. const HealthCheckController = require('./app/js/HealthCheckController')
  24. app.use(bodyParser.json({ limit: '2mb' }))
  25. app.use(metrics.http.monitor(logger))
  26. app.delete('/user/:user_id', SpellingAPIController.deleteDic)
  27. app.get('/user/:user_id', SpellingAPIController.getDic)
  28. app.post('/user/:user_id/check', SpellingAPIController.check)
  29. app.post('/user/:user_id/learn', SpellingAPIController.learn)
  30. app.post('/user/:user_id/unlearn', SpellingAPIController.unlearn)
  31. app.get('/status', (req, res) => res.send({ status: 'spelling api is up' }))
  32. app.get('/health_check', HealthCheckController.healthCheck)
  33. const settings =
  34. Settings.internal && Settings.internal.spelling
  35. ? Settings.internal.spelling
  36. : undefined
  37. const host = settings && settings.host ? settings.host : 'localhost'
  38. const port = settings && settings.port ? settings.port : 3005
  39. if (!module.parent) {
  40. // application entry point, called directly
  41. mongodb
  42. .waitForDb()
  43. .then(() => {
  44. app.listen(port, host, function (error) {
  45. if (error != null) {
  46. throw error
  47. }
  48. return logger.info(`spelling starting up, listening on ${host}:${port}`)
  49. })
  50. })
  51. .catch(err => {
  52. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  53. process.exit(1)
  54. })
  55. }
  56. module.exports = app