Skip to content

Commit

Permalink
合并:优化actions和细节调整 (#14)
Browse files Browse the repository at this point in the history
Merge pull request #14 from guobao2333/dev
  • Loading branch information
guobao2333 committed Sep 13, 2024
2 parents 63056ca + 3ede3b5 commit 551537b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 55 deletions.
15 changes: 13 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Project
README.md
LICENSE
node_modules/

# git
.git/
.github/
.gitignore
vercel.json

# vercel
api/
vercel.json

# netlify
functions/
netlify.toml
3 changes: 3 additions & 0 deletions .github/workflows/docker-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: Docker Image CI

on:
push:
paths:
- '**.js'
- '*.json'
branches:
- 'main'
- 'dev'
Expand Down
97 changes: 53 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,59 @@
# DeepLX Serverless
[本项目](https://github.com/guobao2333/DeepLX-Serverless)是一个基于 DeepL翻译API 的无服务器函数部署项目,旨在提供一个可直接对接的免费翻译解决方案,很适合需要在无服务器环境中集成DeepL翻译服务的开发者使用。

DeepLX 免费翻译API**函数部署版**与原项目[DeepLX](https://github.com/OwO-Network/DeepLX)的区别在于**利用了无服务器函数(也叫边缘函数)请求IP不固定的特性,极大程度上避免了`Error 429`请求太频繁**
与原项目[DeepLX](https://github.com/OwO-Network/DeepLX)的区别在于**利用了无服务器函数(边缘函数)请求IP不固定的特性**,有效避免了`Error 429`请求太频繁的报错(不过嘛凡事总有例外¯\\_(ツ)_/¯)

**如果本项目对你有用的话,不妨点个`Star`❤️**
**Click `Star` if you like!! thanks❤️**

## Major Changes | 重大改变
虽然因此不会立马被*暂时封禁*,但也请不要滥用!目前我部署在vercel上会有流量限制,如果您有大量内容需要翻译,请购买DeepL的付费版,当前项目使用的free接口会受到DeepL政策限制
由于目前项目未对pro接口进行适配,所以现在还只能使用free接口,不过在将来会完善,敬请期待~ („• ֊ •„)੭

如果您在这之前不使用本项目,此部分可以跳过。
1. 新增了docker部署支持
2. 请求参数变化: `alternative_number` -> `alt_count`
3. 许可证变化: `MIT` -> `AGPLv3`

## Let's Go | 开始使用
### Prerequisites | 你需要准备什么
**如果本项目对你有用的话,不妨点个`Star`❤️**
**Click `Star` if you like!! thanks❤️**

- 一双灵活的小手
- 一个聪明的小脑袋瓜
- 支持 `Nodejs ≥16.13``Docker``Serverless Function` 的服务器
- (可选) 拥有[Vercel](https://vercel.com)账号
<!-- # Let's Go | 开始使用 -->
## Prerequisites | 你需要准备什么
在正式开始使用前,我们还需要做一些准备工作呢 (^o^)/
你需要:
- 一双灵活的小手🙌🏻
- 一个聪明的小脑袋瓜🧠
- 支持 `Nodejs ≥16.13``Docker``Serverless Function` 的服务器💻
- (可选) 拥有[Vercel](https://vercel.com)[Netlify](https://netlify.com)的账号

### Deploy | 部署
## Deploy | 部署

使用任意支持无服务器函数部署的服务器,比如可以使用 `Vercel` 或者 `Netlify` 进行部署,又或者其他能够使用nodejs的服务器。(大多数服务器提供商都提供函数计算服务器)

如果你拥有[Vercel](https://vercel.com)账号的话那就很简单了,因为你只需要点击下方按钮即可一键部署到Vercel:

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/guobao2333/DeepLX-Serverless)

你也可以在[Netlify](https://netlify.com)上一键部署本项目:
或者你拥有[Netlify](https://netlify.com)账号的话,也可以一键部署到Netlify:

[![Deploy to Netlify](https://www.netlify.com/img/deploy/button.svg)](https://app.netlify.com/start/deploy?repository=https://github.com/guobao2333/DeepLX-Serverless)

因维护者我有多个仓库需要维护,短时间内将无法对多平台部署方案进行兼容,您可以先打开一个`issues`并选择**自托管**方案
因维护者我有多个仓库需要维护,短时间内将无法对多平台部署方案进行兼容,您可以打开一个`issues📌`或打开一个`Pull Request📎`贡献您的代码
✨项目当前没有任何需要填写的变量值,但在后续可能会添加。

#### Docker | 容器部署
> [!IMPORTANT]
> 如果您需要跨域访问,请在`server.js`中配置`CORS`
> Vercel这类平台支持更便捷的方式配置,详请查阅其平台文档。

### Docker | 容器部署

直接运行预构建容器:
🐳直接运行预构建容器:
```bash
docker run -d -p 9000:9000 ghcr.io/guobao2333/deeplx-serverless
```

你还可以自己构建后运行:
🚧你还可以自己构建后运行:
```bash
docker build -t deeplx-serverless .
docker run -d -p 9000:9000 deeplx-serverless
docker build -t DeepLXS .
docker run -d -p 9000:9000 DeepLXS
```

#### Self hosting | 源码部署

### Self hosting | 源码部署

✅尽管本项目是专为 serverless 适配的方案,但是也能使用自己的服务器直接部署源码:
```bash
Expand All @@ -63,15 +68,19 @@ npm run start
git clone https://github.com/guobao2333/DeepLX-Serverless && cd DeepLX-Serverless && npm i && npm run start
```

🚧你可以运行`npm run test`用来测试翻译接口。
🛠️你可以运行`npm run test`用来测试翻译接口。
⚠️注意!测试命令仅返回翻译内容,获取所有结果需要使用`POST`

如果部署完成了,就可以开始使用啦!🎉
### How To Use | 如何使用

使用post通过 `域名地址` + `/translate` + `json请求体` 这样的形式获取json响应。

详细API文档,请查看本项目的wiki:
## How To Use | 如何使用
> [!IMPORTANT]
> 请求时视部署服务的地区而定,可能会存在返回速度不一的情况,在中国地区可能会出现完全无法访问的情况,届时请尝试更换部署服务的地区,或让您部署的服务能够正常向DeepL发送请求即可。
📡使用post通过 `域名地址` + `/translate` + `json请求体` 这样的形式获取json响应。

❔详细API文档,请查看本项目的wiki:
[English](https://github.com/guobao2333/DeepLX-Serverless/wiki/API-Parameters) | [简体中文](https://github.com/guobao2333/DeepLX-Serverless/wiki/API-%E5%8F%82%E6%95%B0)

📋你可以直接复制到命令行运行**本地测试:**
Expand All @@ -81,7 +90,7 @@ curl --location --request POST 'http://localhost:9000/translate' --header 'Conte

✨部署完成后,建议搭配浏览器插件「沉浸式翻译」一同使用。

## Use On Browser Plugin | 沉浸式翻译设置
## Use In Browser Extension Plugin | 在沉浸式翻译中使用

1. 在浏览器上安装最新的 [沉浸式翻译](https://github.com/immersive-translate/immersive-translate/releases)
2. 点击左下角的 "开发者设置"。启用测试版实验功能。
Expand All @@ -90,17 +99,6 @@ curl --location --request POST 'http://localhost:9000/translate' --header 'Conte

![沉浸式翻译](https://github.com/LegendLeo/deeplx-serverless/assets/25115173/d3affe2b-9e99-4d5c-bc8c-cd67e70d0368)

## Contribute | 贡献
> [!IMPORTANT]
> 在您做出贡献之前请先阅读理解并遵守以下内容:
1. 先切换到`dev`分支,并同步最新代码。
2. 需要进行完整测试后才可在`main`分支打开拉取请求。
3. 不要对**相同代码**进行多次拉取请求!!

本人因时间(和各种各样的)原因,故无法及时对您的贡献进行测试,所以您还需要**自行测试**

如果你是第一次贡献,并且真的想贡献点什么,那么请查看[如何为开源做贡献](https://opensource.guide/how-to-contribute/),不过我喜欢叫它「开源贡献指南」,那里有你需要知道的一切。

## Star History

<a href="https://star-history.com/#guobao2333/DeepLX-Serverless&Date">
Expand All @@ -111,8 +109,19 @@ curl --location --request POST 'http://localhost:9000/translate' --header 'Conte
</picture>
</a>

## License | 许可证
本项目遵循[GNU/AGPL-3.0 许可证](./LICENSE)的条款发布。
## Contribute | 贡献
> [!IMPORTANT]
> **在您做出贡献之前请先阅读理解并遵守以下内容:**
1. 先切换到`dev`分支,同步最新代码。
2. 如果在贡献时对贡献规范有疑惑,请打开一个`issues`
3. 不要对**相同代码**进行多次拉取请求!!

本人因时间(和各种各样的)原因,故无法及时对您的贡献进行测试,所以您可能还需要**自行测试**

如果你是第一次贡献,并且真的想贡献点什么,那么请查看[《如何为开源做贡献》](https://opensource.guide/how-to-contribute/),不过我喜欢叫它「开源贡献指南」,那里有你需要知道的一切。

## License | 开源许可
本项目遵循[GNU/AGPLv3 许可证](./LICENSE)的条款发布。

DeepL free translate API for Serverless
Copyright (C) 2024 shiguobaona
Expand All @@ -132,6 +141,6 @@ curl --location --request POST 'http://localhost:9000/translate' --header 'Conte

## Acknowledgments | 鸣谢

1. [OwO-Network/DeepLX](https://github.com/OwO-Network/DeepLX) - 一切的开始
2. [LegendLeo/deeplx-serverless](https://github.com/LegendLeo/deeplx-serverless) - 本项目的重构前项目
1. [OwO-Network/DeepLX](https://github.com/OwO-Network/DeepLX) - 感谢OwO-Network的优秀项目(•̀ᴗ•́)و̑̑
2. [LegendLeo/deeplx-serverless](https://github.com/LegendLeo/deeplx-serverless) - 本项目的重构前项目,接过维护的大旗(ง •̀_•́)ง
3. [bropines/Deeplx-vercel](https://github.com/bropines/Deeplx-vercel) - 参考部分实现
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "deeplx-serverless",
"version": "1.1.0",
"description": "DeepLX Free API for serverless",
"version": "1.1.2",
"description": "DeepL free API for Serverless",
"type": "module",
"main": "server.js",
"engines": {
Expand Down
5 changes: 3 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const app = express(),
preflightContinue: false
};

// 其他平台支持配置CORS,我就不兼容了
app.use(cors(CORS));
app.use(bodyParser.json());

Expand All @@ -33,7 +34,7 @@ async function post(req, res) {
});
}

// 判断是否备选翻译
// 是否允许备选翻译
if (!allowAlternative) alt_count = 0;
const { text, source_lang, target_lang, alt_count } = req.body;

Expand Down Expand Up @@ -67,7 +68,7 @@ async function post(req, res) {
async function get(req, res) {
res.json({
code: 200,
message: "Welcome to the DeepL Free API. Please POST to '/translate'. This program is published in accordance with the terms of GUN AGPL-3.0. Visit 'https://github.com/guobao2333/DeepLX-Serverless' for more information."
message: "Welcome to the DeepL Free API. Please POST to '/translate'. This program is published in accordance with the terms of GNU/AGPLv3. Visit 'https://github.com/guobao2333/DeepLX-Serverless' for more information."
});
};

Expand Down
8 changes: 3 additions & 5 deletions translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function getTimestamp(iCount) {
}

async function translate(
text,
text = 'Error: The original text cannot be empty!',
sourceLang = 'AUTO',
targetLang = 'ZH',
alternativeCount = 0,
Expand Down Expand Up @@ -75,9 +75,7 @@ async function translate(
});

if (response.status === 429) {
throw new Error(
`Too many requests, your IP has been blocked by DeepL temporarily, please don't request it frequently in a short time.`
);
throw new Error(`Too many requests, your IP has been blocked by DeepL temporarily, please don't request it frequently in a short time.`);
}

if (response.status !== 200) {
Expand All @@ -94,7 +92,7 @@ async function translate(
}
return result;
} catch (err) {
console.error(err);
console.error(err, err.stack);
}
}

Expand Down

0 comments on commit 551537b

Please sign in to comment.