Use Express router for translate endpoint

This commit is contained in:
valadaptive
2023-12-04 13:00:59 -05:00
parent 414c9bd5fb
commit 15ba2441ce
2 changed files with 262 additions and 266 deletions

View File

@@ -3615,7 +3615,7 @@ require('./src/endpoints/horde').registerEndpoints(app, jsonParser);
require('./src/endpoints/vectors').registerEndpoints(app, jsonParser); require('./src/endpoints/vectors').registerEndpoints(app, jsonParser);
// Chat translation // Chat translation
require('./src/endpoints/translate').registerEndpoints(app, jsonParser); app.use('/api/translate', require('./src/endpoints/translate').router);
// Emotion classification // Emotion classification
require('./src/endpoints/classify').registerEndpoints(app, jsonParser); require('./src/endpoints/classify').registerEndpoints(app, jsonParser);

View File

@@ -1,17 +1,16 @@
const fetch = require('node-fetch').default; const fetch = require('node-fetch').default;
const https = require('https'); const https = require('https');
const express = require('express');
const { readSecret, SECRET_KEYS } = require('./secrets'); const { readSecret, SECRET_KEYS } = require('./secrets');
const { getConfigValue } = require('../util'); const { getConfigValue } = require('../util');
const { jsonParser } = require('../express-common');
const DEEPLX_URL_DEFAULT = 'http://127.0.0.1:1188/translate'; const DEEPLX_URL_DEFAULT = 'http://127.0.0.1:1188/translate';
const ONERING_URL_DEFAULT = 'http://127.0.0.1:4990/translate'; const ONERING_URL_DEFAULT = 'http://127.0.0.1:4990/translate';
/** const router = express.Router();
* @param {import("express").Express} app
* @param {any} jsonParser router.post('/libre', jsonParser, async (request, response) => {
*/
function registerEndpoints(app, jsonParser) {
app.post('/api/translate/libre', jsonParser, async (request, response) => {
const key = readSecret(SECRET_KEYS.LIBRE); const key = readSecret(SECRET_KEYS.LIBRE);
const url = readSecret(SECRET_KEYS.LIBRE_URL); const url = readSecret(SECRET_KEYS.LIBRE_URL);
@@ -56,9 +55,9 @@ function registerEndpoints(app, jsonParser) {
console.log('Translation error: ' + error.message); console.log('Translation error: ' + error.message);
return response.sendStatus(500); return response.sendStatus(500);
} }
}); });
app.post('/api/translate/google', jsonParser, async (request, response) => { router.post('/google', jsonParser, async (request, response) => {
try { try {
const { generateRequestUrl, normaliseResponse } = require('google-translate-api-browser'); const { generateRequestUrl, normaliseResponse } = require('google-translate-api-browser');
const text = request.body.text; const text = request.body.text;
@@ -97,9 +96,9 @@ function registerEndpoints(app, jsonParser) {
console.log('Translation error', error); console.log('Translation error', error);
return response.sendStatus(500); return response.sendStatus(500);
} }
}); });
app.post('/api/translate/deepl', jsonParser, async (request, response) => { router.post('/deepl', jsonParser, async (request, response) => {
const key = readSecret(SECRET_KEYS.DEEPL); const key = readSecret(SECRET_KEYS.DEEPL);
if (!key) { if (!key) {
@@ -151,9 +150,9 @@ function registerEndpoints(app, jsonParser) {
console.log('Translation error: ' + error.message); console.log('Translation error: ' + error.message);
return response.sendStatus(500); return response.sendStatus(500);
} }
}); });
app.post('/api/translate/onering', jsonParser, async (request, response) => { router.post('/onering', jsonParser, async (request, response) => {
const secretUrl = readSecret(SECRET_KEYS.ONERING_URL); const secretUrl = readSecret(SECRET_KEYS.ONERING_URL);
const url = secretUrl || ONERING_URL_DEFAULT; const url = secretUrl || ONERING_URL_DEFAULT;
@@ -204,9 +203,9 @@ function registerEndpoints(app, jsonParser) {
console.log('Translation error: ' + error.message); console.log('Translation error: ' + error.message);
return response.sendStatus(500); return response.sendStatus(500);
} }
}); });
app.post('/api/translate/deeplx', jsonParser, async (request, response) => { router.post('/deeplx', jsonParser, async (request, response) => {
const secretUrl = readSecret(SECRET_KEYS.DEEPLX_URL); const secretUrl = readSecret(SECRET_KEYS.DEEPLX_URL);
const url = secretUrl || DEEPLX_URL_DEFAULT; const url = secretUrl || DEEPLX_URL_DEFAULT;
@@ -260,9 +259,9 @@ function registerEndpoints(app, jsonParser) {
console.log('DeepLX translation error: ' + error.message); console.log('DeepLX translation error: ' + error.message);
return response.sendStatus(500); return response.sendStatus(500);
} }
}); });
app.post('/api/translate/bing', jsonParser, async (request, response) => { router.post('/bing', jsonParser, async (request, response) => {
const bingTranslateApi = require('bing-translate-api'); const bingTranslateApi = require('bing-translate-api');
const text = request.body.text; const text = request.body.text;
let lang = request.body.lang; let lang = request.body.lang;
@@ -284,9 +283,6 @@ function registerEndpoints(app, jsonParser) {
console.log('Translation error: ' + err.message); console.log('Translation error: ' + err.message);
return response.sendStatus(500); return response.sendStatus(500);
}); });
}); });
}
module.exports = { module.exports = { router };
registerEndpoints,
};