Merge pull request #1467 from valadaptive/translate-router

Use Express router for translate endpoint
This commit is contained in:
Cohee 2023-12-05 00:13:44 +02:00 committed by GitHub
commit b171f0a868
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 262 additions and 266 deletions

View File

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

View File

@ -1,17 +1,16 @@
const fetch = require('node-fetch').default;
const https = require('https');
const express = require('express');
const { readSecret, SECRET_KEYS } = require('./secrets');
const { getConfigValue } = require('../util');
const { jsonParser } = require('../express-common');
const DEEPLX_URL_DEFAULT = 'http://127.0.0.1:1188/translate';
const ONERING_URL_DEFAULT = 'http://127.0.0.1:4990/translate';
/**
* @param {import("express").Express} app
* @param {any} jsonParser
*/
function registerEndpoints(app, jsonParser) {
app.post('/api/translate/libre', jsonParser, async (request, response) => {
const router = express.Router();
router.post('/libre', jsonParser, async (request, response) => {
const key = readSecret(SECRET_KEYS.LIBRE);
const url = readSecret(SECRET_KEYS.LIBRE_URL);
@ -58,7 +57,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/translate/google', jsonParser, async (request, response) => {
router.post('/google', jsonParser, async (request, response) => {
try {
const { generateRequestUrl, normaliseResponse } = require('google-translate-api-browser');
const text = request.body.text;
@ -99,7 +98,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/translate/deepl', jsonParser, async (request, response) => {
router.post('/deepl', jsonParser, async (request, response) => {
const key = readSecret(SECRET_KEYS.DEEPL);
if (!key) {
@ -153,7 +152,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/translate/onering', jsonParser, async (request, response) => {
router.post('/onering', jsonParser, async (request, response) => {
const secretUrl = readSecret(SECRET_KEYS.ONERING_URL);
const url = secretUrl || ONERING_URL_DEFAULT;
@ -206,7 +205,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/translate/deeplx', jsonParser, async (request, response) => {
router.post('/deeplx', jsonParser, async (request, response) => {
const secretUrl = readSecret(SECRET_KEYS.DEEPLX_URL);
const url = secretUrl || DEEPLX_URL_DEFAULT;
@ -262,7 +261,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/translate/bing', jsonParser, async (request, response) => {
router.post('/bing', jsonParser, async (request, response) => {
const bingTranslateApi = require('bing-translate-api');
const text = request.body.text;
let lang = request.body.lang;
@ -285,8 +284,5 @@ function registerEndpoints(app, jsonParser) {
return response.sendStatus(500);
});
});
}
module.exports = {
registerEndpoints,
};
module.exports = { router };