overleaf/services/spelling/app.js

65 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-07-03 08:41:01 -04:00
/*
* 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')
2019-07-03 08:41:01 -04:00
metrics.initialize('spelling')
const Settings = require('@overleaf/settings')
2019-07-03 08:41:01 -04:00
const logger = require('logger-sharelatex')
logger.initialize('spelling')
if ((Settings.sentry != null ? Settings.sentry.dsn : undefined) != null) {
logger.initializeErrorReporting(Settings.sentry.dsn)
}
metrics.memory.monitor(logger)
const mongodb = require('./app/js/mongodb')
2019-07-03 08:41:01 -04:00
const SpellingAPIController = require('./app/js/SpellingAPIController')
const express = require('express')
const app = express()
metrics.injectMetricsRoute(app)
2019-07-03 08:41:01 -04:00
const bodyParser = require('body-parser')
const HealthCheckController = require('./app/js/HealthCheckController')
app.use(bodyParser.json({ limit: '2mb' }))
app.use(metrics.http.monitor(logger))
2019-07-03 08:41:01 -04:00
2020-07-22 04:32:39 -04:00
app.delete('/user/:user_id', SpellingAPIController.deleteDic)
app.get('/user/:user_id', SpellingAPIController.getDic)
app.post('/user/:user_id/check', SpellingAPIController.check)
app.post('/user/:user_id/learn', SpellingAPIController.learn)
app.post('/user/:user_id/unlearn', SpellingAPIController.unlearn)
app.get('/status', (req, res) => res.send({ status: 'spelling api is up' }))
2019-07-03 08:41:01 -04:00
app.get('/health_check', HealthCheckController.healthCheck)
2019-07-03 08:41:01 -04:00
const settings =
Settings.internal && Settings.internal.spelling
? Settings.internal.spelling
2019-07-03 08:41:01 -04:00
: undefined
const host = settings && settings.host ? settings.host : 'localhost'
const port = settings && settings.port ? settings.port : 3005
if (!module.parent) {
// application entry point, called directly
mongodb
.waitForDb()
.then(() => {
app.listen(port, host, function (error) {
if (error != null) {
throw error
}
return logger.info(`spelling starting up, listening on ${host}:${port}`)
})
})
2021-07-13 07:04:47 -04:00
.catch(err => {
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
process.exit(1)
})
2019-07-03 08:41:01 -04:00
}
module.exports = app