Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release: 重构和发布 v3.0.0 #25

Merged
merged 31 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
52f5551
变化:修改docker标签
guobao2333 Sep 26, 2024
7b7a34d
修复:修改获取日期方式
guobao2333 Sep 26, 2024
605af97
修复:尝试修复netlify构建错误
guobao2333 Sep 26, 2024
460ea5b
更新:调整文档
guobao2333 Sep 26, 2024
71fd259
修复:调整部署配置
guobao2333 Sep 26, 2024
c9eb569
修复:调整部署配置
guobao2333 Sep 26, 2024
70b8e11
修复:防止静态检查的报错
guobao2333 Sep 26, 2024
df2f5b4
变化:补全url访问重定向
guobao2333 Sep 26, 2024
41f6535
变化:重命名环境变量文件
guobao2333 Nov 22, 2024
cba19e7
变化:彻底取消对netlify的支持
guobao2333 Nov 22, 2024
ba1dced
变化:移除无用代码
guobao2333 Nov 22, 2024
98b6f7c
变化:调整文档
guobao2333 Nov 22, 2024
298bcb2
wip: 优化和调整api请求
guobao2333 Nov 24, 2024
b5271df
chore: 构建时移动代码
guobao2333 Nov 24, 2024
33659a7
chore: 调整依赖版本axios>=1.7.4
guobao2333 Nov 24, 2024
5f98039
ci: 添加依赖自动更新工作流程
guobao2333 Nov 24, 2024
3f0f9d2
chore: 调整目录结构
guobao2333 Nov 24, 2024
6ec3e9a
chore: 移除无用依赖包
guobao2333 Nov 24, 2024
12ba898
docs: 移除大量无用词汇
guobao2333 Nov 24, 2024
7c76b1d
chore: 弃用旧结构代码
guobao2333 Nov 27, 2024
dbc5bb2
chore: 一些小调整
guobao2333 Nov 27, 2024
836f1ee
test: 测试用例修改
guobao2333 Nov 27, 2024
f347a62
refactor: 弃用旧代码并重写翻译接口
guobao2333 Nov 27, 2024
1d055a8
chore: 更新版本号
guobao2333 Nov 27, 2024
327ef18
docs: 更新文档
guobao2333 Nov 27, 2024
bdb230e
chore: 琐碎更新
guobao2333 Nov 27, 2024
776c85b
perf: 报错信息优化
guobao2333 Nov 28, 2024
543dae1
docs: 更新文档
guobao2333 Nov 28, 2024
4c6a52a
build: 调整构建步骤
guobao2333 Nov 28, 2024
fe05f8f
fix: 添加缺少的逗号
guobao2333 Nov 28, 2024
474b827
merge: 解决冲突并合并
guobao2333 Nov 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Project
README.md
test.js
test_post.js
src/translate_old.js
node_modules/

# git
Expand All @@ -8,9 +11,4 @@ node_modules/
.gitignore

# vercel
api/
vercel.json

# netlify
functions/
netlify.toml
vercel.json
6 changes: 4 additions & 2 deletions example.env → .env
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# 在使用此示例前,请先将文件名'example'去掉

# 端口号
# type: Number
# default: 6119
PORT=6119

# 是否允许请求备选翻译
# type: Boolean
# default: true
# 不允许时报错
ALTERNATIVE=true

# 跨域请求 (待完善)
# type: Boolean String RegExp
# default: false
# 正则表达式请使用引号包裹。
# 示例: '*'(允许任何) 或者 false(不允许)
CORS_ORIGIN=false
9 changes: 9 additions & 0 deletions .github/workflows/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Please see the documentation for all configuration options:
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file

version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
4 changes: 1 addition & 3 deletions .github/workflows/docker-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ jobs:
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=schedule,pattern={{date 'YYYY/MM/DD-HH:mm' tz='Asia/Shanghai'}}
type=semver,pattern={{version}}
type=ref,event=branch
type=ref,event=pr
type=raw,value=${{ github.ref }},enable=${{ startsWith(github.ref, 'refs/tags/v') }}
type=raw,value=${{ format('YYYY/MM/DD-HH:mm', 'now', 'Asia/Shanghai') }},enable=${{ github.event.ref_type == 'tag' }}
type=raw,value=latest,enable=${{ github.event.ref_type == 'tag' }}

# Build and push Docker image with Buildx (don't push on PR)
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# backup
*.bak
src/translate_old.js

# Logs
logs
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# 阶段 1: 依赖安装
FROM node:22-alpine AS deps
FROM node:lts-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm install

# 阶段 2: 构建和运行应用
FROM node:22-alpine
FROM node:lts-alpine
WORKDIR /app

# 复制依赖
Expand Down
680 changes: 19 additions & 661 deletions LICENSE

Large diffs are not rendered by default.

142 changes: 42 additions & 100 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,119 +1,81 @@
# DeepLX Serverless
[本项目](https://github.com/guobao2333/DeepLX-Serverless)是一个基于 DeepL翻译API 的无服务器函数部署项目,旨在提供一个可直接对接的免费翻译解决方案,很适合需要在无服务器环境中集成DeepL翻译服务的开发者使用。

与原项目[DeepLX](https://github.com/OwO-Network/DeepLX)的区别在于**利用了无服务器函数(边缘函数)请求IP不固定的特性**,有效避免了`Error 429`请求太频繁的报错(不过嘛凡事总有例外¯\\\_(ツ)_/¯)
***本项目3.0版本开始完全基于[OwO-Network/DeepLX](https://github.com/OwO-Network/DeepLX)和DeepL网页端API进行重写。***
**2.0及之前版本在[LegendLeo/deeplx-serverless](https://github.com/LegendLeo/deeplx-serverless)的基础上进行重构。**

虽然因此不会立马被*暂时封禁*,但也请不要滥用!目前我部署在vercel上会有流量限制,如果您有大量内容需要翻译,请购买DeepL的付费版,当前项目使用的free接口会受到DeepL政策限制
由于目前项目未对pro接口进行适配,所以现在还只能使用free接口,不过在将来会完善,敬请期待~(„• ֊ •„)੭
DeepLX Serverless是一个基于DeepL翻译且无需令牌的网页API的Serverless版本,与原项目[DeepLX](https://github.com/OwO-Network/DeepLX)的区别在于**利用了无服务器函数(边缘函数)请求IP不固定的特性**,有效避免了`Error 429`(不过嘛凡事总有例外¯\\\_(ツ)_/¯)

请勿滥用本项目!如果您有大量内容需要翻译,请购买DeepL的付费服务,项目使用的网页版接口会受到DeepL政策限制。

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

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

## Deploy | 部署
## Prerequisites | 准备工作
> [!IMPORTANT]
> 有时`main`分支的合并可能不及时,或者你希望提前体验新功能,请切换到`dev`分支获取代码。

使用任意支持无服务器函数部署的服务器,比如可以使用 `Vercel` <!-- 或者 `Netlify` --> 进行部署,又或者其他能够使用nodejs的服务器。(大多数服务器提供商都提供函数计算服务器)
- 支持 `Nodejs ≥18` 或 `Docker` 或 `Serverless Function` 的服务器
- (可选) 拥有[Vercel](https://vercel.com)的账号

如果你拥有 [Vercel](https://vercel.com) <!-- 或者 [Netlify](https://netlify.com) --> 账号的话那就很简单了,因为你只需要点击下方按钮即可一键部署:
### Deploy | 部署

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/guobao2333/DeepLX-Serverless)
<!-- [![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📌`或打开一个`Pull Request📎`贡献您的代码。
✨项目当前没有任何需要填写的变量值,但在后续可能会添加。
```bash
git clone https://github.com/guobao2333/DeepLX-Serverless
cd DeepLX-Serverless
npm i
npm run start
```

配置可编辑`.env`或者提供启动参数来修改。

### Docker | 容器部署

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

🚧你还可以自己构建后运行
自行构建
```bash
docker build -t deeplxs .
docker run -d -p 6119:6119 deeplxs
```


### Self hosting | 源码部署

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

📋复制到命令行运行来一键启动服务:
```bash
git clone https://github.com/guobao2333/DeepLX-Serverless && cd DeepLX-Serverless && npm i && npm run start
```

如果部署完成了,就可以开始使用啦!🎉

#### startup parameters | 启动参数
### Startup Parameters | 启动参数
> [!IMPORTANT]
> 此功能在 v2.0.0 及以上版本中可用
> 此功能在 2.0.0 及以上版本中可用。
> 启动参数会覆盖`.env`

如果你想的话,可以在启动时添加参数
比如添加一个`--no`前缀和`-a`参数来禁止备选翻译
有时你可能想在启动服务时添加参数
比如添加一个`-c`参数来允许所有跨域请求
```bash
npm run start -- --no-a
npm run start -- -c
```

💡使用`-h`来获取所有参数。

## How To Use | 如何使用
> [!IMPORTANT]
> 请求时视部署服务的地区而定,可能会存在返回速度不一的情况,在中国地区可能会出现完全无法访问的情况,届时请尝试更换部署服务的地区,或让您部署的服务能够正常向DeepL发送请求即可。

📡使用post通过 `域名地址` + `/translate` + `json请求体` 这样的形式获取json响应
* 详细调用参数请查看[项目Wiki](https://github.com/guobao2333/DeepLX-Serverless/wiki)

❔详细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)
### Http Call | 网络请求

📋你可以直接复制到命令行运行**本地测试:**
```bash
curl --location --request POST 'http://localhost:6119/translate' --header 'Content-Type: application/json' --data '{"text": "你好,世界!", "source_lang": "zh", "target_lang": "en"}'
curl -X POST 'http://localhost:6119/translate' -H 'Content-Type: application/json' -d '{"text": "你好,世界!", "source_lang": "zh", "target_lang": "en"}'
```

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

### NON-HTTP Call And example | 测试运行与内部调用
🛠️你可以在不启动服务的情况下直接运行`npm test`用来测试翻译接口以及当前网络与DeepL的连通性。
⚠️注意!测试仅返回翻译内容,获取所有结果需要使用`POST`!
运行`npm test`来测试翻译接口。还可以集成到你的项目中来使用DeepL翻译服务。

由于本项目并没有只局限于http协议的访问,所以完全可以集成到你的Serverless项目中来使用DeepL,下面是一个简单的调用示例
简单的示例
```javascript
import { translate } from './translate.js';
translate('你好,世界!', 'zh', 'en', 3)
translate('how are you?', 'en', 'zh', '', false, false)
.then(result => {
console.log(result)
});
```

具体可以查看`test.js`,其中对调用函数的参数有详细说明。

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

1. 在浏览器上安装最新的 [沉浸式翻译](https://github.com/immersive-translate/immersive-translate/releases)。
2. 点击左下角的 "开发者设置"。启用测试版实验功能。
3. 翻译服务选中 `DeepLX(beta)`
4. 设置 URL 为刚才获取的访问路径(需带translate)。

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

## Star History | 收藏趋势

<a href="https://star-history.com/#guobao2333/DeepLX-Serverless&Date">
Expand All @@ -125,36 +87,16 @@ translate('你好,世界!', 'zh', 'en', 3)
</a>

## Contribute | 贡献
> [!IMPORTANT]
> **如果您想要做出贡献,可以按照以下顺序操作:**
1. 先切换到`dev`分支,`pull`或者`fork`最新的代码。
2. 提交并***签名***你的更改。(对提交进行签名可以防止冒名顶替)
3. 打开一个PR或多或少说明更改内容,接下来请等待合并。

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

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

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

Copyright (C) 2024 shiguobaona

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
1. 获取`dev`或`main`分支的代码
2. 提交你的更改并描述提交内容
3. 打开一个`Pull Requests`

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
如果你是第一次贡献,那么请查看[《如何为开源做贡献》](https://opensource.guide/how-to-contribute/)

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
## Thanks | 感谢

## Acknowledgments | 鸣谢
它们是本项目的根本,没有它们就没有本项目。

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) - 参考部分实现
1. [OwO-Network/DeepLX](https://github.com/OwO-Network/DeepLX)
2. [LegendLeo/deeplx-serverless](https://github.com/LegendLeo/deeplx-serverless)
3. [bropines/Deeplx-vercel](https://github.com/bropines/Deeplx-vercel)
16 changes: 0 additions & 16 deletions functions/index.js

This file was deleted.

2 changes: 0 additions & 2 deletions netlify.toml

This file was deleted.

29 changes: 15 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
{
"name": "deeplx-serverless",
"version": "2.1.0",
"version": "3.0.0",
"description": "DeepL free API for Serverless",
"type": "module",
"main": "server.js",
"engines": {
"node": ">=18"
},
"scripts": {
"start": "node server.js",
"test": "node test.js"
},
"keywords": [
"deeplxs",
"deeplx",
"deepl",
"translate",
"serverless"
],
"license": "AGPL-3.0",
"author": "guobao2333",
"license": "MIT",
"engines": {
"node": ">=18"
},
"scripts": {
"build:vercel": "mv src/* api/",
"start": "node src/server.js",
"test": "node test.js",
"start:sl": "node src/index.js",
"test:post": "node test_post.js"
},
"dependencies": {
"axios": "^1.7.4",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"axios": "^1.6.3",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"express": "^4.18.2",
"lodash": "^4.17.21",
"node-fetch": "^3.3.2",
"random-int": "^3.0.0",
"yargs": "^17.7.2"
}
}
3 changes: 1 addition & 2 deletions api/index.js → src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import express from 'express';
import fetch from 'node-fetch';
import { post, get } from '../server.js';
import { post, get } from './server.js';

const app = express();

Expand Down
Loading
Loading