Skip to content

Commit

Permalink
功能:新增对env环境变量的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
guobao2333 committed Sep 25, 2024
1 parent cc822ff commit 0123aee
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ node_modules/

yarn.lock
package-lock.json

.env
15 changes: 15 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 在使用此示例前,请先将文件名'example'去掉

# 端口号
# type: Number
PORT=6119

# 是否允许请求备选翻译
# type: Boolean
ALTERNATIVE=true

# 跨域请求 (待完善)
# type: Boolean String RegExp
# 正则表达式请使用引号包裹。
# 示例: '*'(允许任何) 或者 false(不允许)
CORS.ORIGIN=false
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deeplx-serverless",
"version": "2.0.0",
"version": "2.1.0",
"description": "DeepL free API for Serverless",
"type": "module",
"main": "server.js",
Expand All @@ -18,8 +18,9 @@
"translate",
"serverless"
],
"license": "AGPLv3",
"license": "AGPL-3.0",
"dependencies": {
"dotenv": "^16.4.5",
"axios": "^1.6.3",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
Expand Down
20 changes: 11 additions & 9 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express from 'express';
import 'dotenv/config';
import cors from 'cors';
import bodyParser from 'body-parser';
import yargs from 'yargs/yargs';
Expand All @@ -10,20 +11,20 @@ const argv = yargs(hideBin(process.argv))
.option('port', {
alias: 'p',
describe: '端口号',
coerce: coerce_port,
default: 9000
coerce: check_port,
default: Number(process.env.PORT) || 6119
})
.option('alt', {
alias: 'a',
describe: '请求备选翻译',
type: 'boolean',
default: true
default: Boolean(process.env.ALTERNATIVE) || true
})
.option('cors', {
alias: 'c',
describe: '允许跨域访问的源(origin)',
coerce: coerce_cors,
default: false
coerce: check_cors,
default: process.env.CORS_ORIGIN || false
})
.help().alias('help', 'h')
.argv;
Expand Down Expand Up @@ -108,20 +109,21 @@ async function get(req, res) {
});
};

function coerce_cors(arg) {
function check_cors(arg) {
if (arg === undefined) return;
if (typeof arg === 'string' || typeof arg === 'boolean') {
return arg;
}
console.error("ParamTypeError: \x1b[33m'"+arg+"'\x1b[31m, origin should be Boolean or String.\n\x1b[0meg: \x1b[32m'*' or true or RegExp");
process.exit(1);
}

function coerce_port(arg) {
function check_port(arg) {
if (typeof arg === 'number' && !isNaN(arg) && Number.isInteger(arg) && arg >= 0 && arg <= 65535) {
return arg;
}
console.warn("WARNING:\x1b[0m port should be >= 0 and < 65536.\nUsed default value instead: 9000\n");
return 9000;
console.warn("WARNING:\x1b[0m port should be >= 0 and < 65536.\nUsed default value instead: 6119\n");
return 6119;
}

// 启动本地服务器
Expand Down
12 changes: 11 additions & 1 deletion translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import axios from 'axios';
import lodash from 'lodash';

const { random } = lodash;
const DEEPL_BASE_URL = 'https://www2.deepl.com/jsonrpc';
const DEEPL_BASE_URL = 'https://www2.deepl.com/jsonrpc',
DEEPL_PRO_URL = 'https://api.deepl.com',
DEEPL_FREE_URL = 'https://api-free.deepl.com';
const headers = {
'Content-Type': 'application/json',
Accept: '*/*',
Expand Down Expand Up @@ -34,6 +36,14 @@ function getTimestamp(iCount) {
return ts - (ts % iCount) + iCount;
}

/**
* @param {string} text - 待翻译的文本
* @param {string} [sourceLang='AUTO'] - 源语言国家/地区代号 默认自动识别
* @param {string} targetLang - 目标语言国家/地区代号
* @param {number} [alternativeCount] - 请求的备选翻译数量
* @param {boolean} [printResult] - 控制台打印返回结果
* @returns {object} 一般情况下返回JSON格式对象
*/
async function translate(
text = 'Error: The original text cannot be empty!',
sourceLang = 'AUTO',
Expand Down

0 comments on commit 0123aee

Please sign in to comment.