2019-05-29 05:21:06 -04:00
|
|
|
const request = require('request')
|
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const AuthenticationController = require('../Authentication/AuthenticationController')
|
|
|
|
|
|
|
|
const TEN_SECONDS = 1000 * 10
|
|
|
|
|
2019-07-16 07:03:17 -04:00
|
|
|
const languageCodeIsSupported = code =>
|
|
|
|
Settings.languages.some(lang => lang.code === code)
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
proxyRequestToSpellingApi(req, res) {
|
|
|
|
const { language } = req.body
|
|
|
|
if (language && !languageCodeIsSupported(language)) {
|
2019-07-16 08:22:53 -04:00
|
|
|
logger.info({ language }, `language not supported`)
|
2019-07-17 05:25:29 -04:00
|
|
|
return res.status(422).send(JSON.stringify({ misspellings: [] }))
|
2019-07-16 07:03:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const userId = AuthenticationController.getLoggedInUserId(req)
|
2019-05-29 05:21:06 -04:00
|
|
|
let url = req.url.slice('/spelling'.length)
|
2019-07-16 07:03:17 -04:00
|
|
|
url = `/user/${userId}${url}`
|
2019-05-29 05:21:06 -04:00
|
|
|
req.headers['Host'] = Settings.apis.spelling.host
|
|
|
|
return request({
|
|
|
|
url: Settings.apis.spelling.url + url,
|
|
|
|
method: req.method,
|
|
|
|
headers: req.headers,
|
|
|
|
json: req.body,
|
|
|
|
timeout: TEN_SECONDS
|
|
|
|
})
|
|
|
|
.on('error', function(error) {
|
|
|
|
logger.error({ err: error }, 'Spelling API error')
|
|
|
|
return res.status(500).end()
|
|
|
|
})
|
|
|
|
.pipe(res)
|
|
|
|
}
|
|
|
|
}
|