From 905db684eb40b6f1fa5f89d9416784a6f08787cb Mon Sep 17 00:00:00 2001 From: Florian Treml Date: Tue, 7 Dec 2021 09:36:33 +0100 Subject: [PATCH] Read base url from env --- frontend/resources/.env | 3 +++ frontend/src/routes.js | 13 +++++-------- frontend/src/utils.js | 13 ++++++++++++- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/frontend/resources/.env b/frontend/resources/.env index a7f5bf1..356245d 100644 --- a/frontend/resources/.env +++ b/frontend/resources/.env @@ -4,6 +4,9 @@ DEBUG=botium* # Comma-separated list of API Tokens to allow. If empty, API Tokens are not required. BOTIUM_API_TOKENS= +# Server URL as seen by clients +#BOTIUM_SPEECH_URL=https://speech.botiumbox.com + # Maximum file size for STT BOTIUM_SPEECH_UPLOAD_LIMIT=50mb diff --git a/frontend/src/routes.js b/frontend/src/routes.js index 165a04f..aa4528c 100644 --- a/frontend/src/routes.js +++ b/frontend/src/routes.js @@ -10,7 +10,7 @@ const sanitize = require('sanitize-filename') const contentDisposition = require('content-disposition') const { WebSocketServer } = require('ws') const { runconvert } = require('./convert/convert') -const { wer } = require('./utils') +const { wer, readBaseUrls } = require('./utils') const debug = require('debug')('botium-speech-processing-routes') const cachePathStt = (process.env.BOTIUM_SPEECH_CACHE_DIR && path.join(process.env.BOTIUM_SPEECH_CACHE_DIR, 'stt')) || './resources/.cache/stt' @@ -714,14 +714,11 @@ const wssStreams = {} stream.events.on('close', () => delete wssStreams[streamId]) wssStreams[streamId] = stream - const proxyHost = req.headers['x-forwarded-host'] - const host = proxyHost || req.get('host') - - const wsProtocol = (req.protocol === 'https' ? 'wss' : 'ws') + const baseUrls = readBaseUrls(req) res.json({ - wsUri: `${wsProtocol}://${host}/${streamId}`, - statusUri: `${req.protocol}://${host}/api/sttstatus/${streamId}`, - endUri: `${req.protocol}://${host}/api/sttend/${streamId}` + wsUri: `${baseUrls.wsUri}/${streamId}`, + statusUri: `${baseUrls.baseUri}/api/sttstatus/${streamId}`, + endUri: `${baseUrls.baseUri}/api/sttend/${streamId}` }).end() } catch (err) { return next(err) diff --git a/frontend/src/utils.js b/frontend/src/utils.js index 3bff657..4327253 100644 --- a/frontend/src/utils.js +++ b/frontend/src/utils.js @@ -74,11 +74,22 @@ const ibmTtsOptions = (req) => { throw new Error('IBM Cloud credentials not found') } +const readBaseUrls = (req) => { + const proto = process.env.BOTIUM_SPEECH_URL ? process.env.BOTIUM_SPEECH_URL.split('://')[0] : req.protocol + const host = process.env.BOTIUM_SPEECH_URL ? process.env.BOTIUM_SPEECH_URL.split('://')[1] : req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'] : req.get('host') + + return { + wsUri: `${proto === 'https' ? 'wss' : 'ws'}://${host}`, + baseUri: `${proto}://${host}` + } +} + module.exports = { asJson, wer, ttsFilename, googleOptions, ibmSttOptions, - ibmTtsOptions + ibmTtsOptions, + readBaseUrls }