Compare commits

...
35 Commits
Author SHA1 Message Date
Vincent Young 5e080f3871 fix: remove unnecessary variables 2023-11-27 15:10:39 -05:00
Vincent Young d93ac81ea2 docs: fix related projects 2023-11-27 15:08:16 -05:00
Vincent Young fdcac338fe docs: fix 2023-11-27 15:04:46 -05:00
Vincent Young 5489e11113 refactor: unified parameter 2023-11-27 14:50:32 -05:00
Vincent Young 365cf185a4 chore: fix 2023-11-27 14:06:45 -05:00
Vincent Young 6710536837 chore: fix compose demo 2023-11-27 14:01:24 -05:00
Vincent Young 128225979a feat: support Official API (#74) 2023-11-27 12:03:32 -05:00
Vincent Young cba84471ba fix: golint 2023-11-20 19:58:46 -05:00
Vincent Young 7617110dbd ci: add golint 2023-11-19 19:52:14 -05:00
Vincent Young 7705e7ae1c ci: remove support for deb 2023-11-19 19:48:46 -05:00
Vincent Young 0bcb4bbfcc fix 2023-11-19 19:45:32 -05:00
Vincent Young 8209c080e3 fix 2023-11-19 19:43:11 -05:00
Vincent Young 8853c4b44e fix 2023-11-19 19:39:15 -05:00
Vincent Young ea27af8c80 fix: actions 2023-11-19 19:33:09 -05:00
Vincent Young 60d1f387e4 ci: upgrade actions/setup-go 2023-11-19 19:27:33 -05:00
Vincent Young ddd4de353d ci: add ci 2023-11-19 19:26:07 -05:00
Vincent Young cd4848e739 Update README.md 2023-11-12 18:18:13 -05:00
Vincent Young 11b9dbff58 Update README.md 2023-11-12 18:16:27 -05:00
Vincent Young f2d416b189 Update README.md 2023-11-12 18:16:15 -05:00
Vincent Young ecf3ab9aeb Update README.md 2023-11-08 15:50:35 -05:00
fossabotandVincent Young d32f667869 Add license scan report and status (#71)
Signed off by: fossabot <badges@fossa.com>

Co-authored-by: Vincent Young <missuo@pm.me>
2023-11-08 15:49:21 -05:00
Vincent Young 558eeaa51c Update README.md 2023-11-08 15:34:37 -05:00
Vincent Young c3787f9dff Update README.md 2023-11-08 15:25:53 -05:00
Vincent Young f3797aa3c3 Update README.md 2023-11-08 15:25:07 -05:00
Vincent Young 874fec1446 Update README.md 2023-11-08 15:24:20 -05:00
Vincent Young 37a8a25111 Update README.md 2023-11-08 15:18:56 -05:00
Vincent Young ca08fa9804 Update README.md 2023-11-08 15:12:35 -05:00
Vincent Young 6688c9d35c Update README.md 2023-11-08 15:09:35 -05:00
Vincent Young 181db37210 feat: support access token (#68) 2023-10-28 22:48:58 -04:00
Vincent Young 5c6b8802e4 fix 2023-10-20 05:50:05 -04:00
Vincent Young fb0dac1889 fix 2023-10-20 05:49:39 -04:00
Vincent Young 781467bb69 Update build.yml 2023-10-20 05:40:38 -04:00
dependabot[bot] 03093a658a chore(deps): bump golang.org/x/net from 0.10.0 to 0.17.0 (#66)
Bumps [golang.org/x/net](https://github.com/golang/net) from 0.10.0 to 0.17.0.
- [Commits](https://github.com/golang/net/compare/v0.10.0...v0.17.0)

---
updated-dependencies:
- dependency-name: golang.org/x/net
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-13 05:02:35 -04:00
Vincent Young 90fc4b96c6 Update Install link 2023-09-27 10:15:12 -04:00
Vincent Young f8b6821fb4 docs: update response demo 2023-09-14 13:59:15 -04:00
9 changed files with 252 additions and 83 deletions
+27
View File
@@ -0,0 +1,27 @@
on:
push:
pull_request:
name: CI
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.19'
- name: Install golint
run: go install golang.org/x/lint/golint@latest
- name: Build
run: go build ./...
- name: Test
run: go test ./...
+1 -2
View File
@@ -8,12 +8,11 @@ on:
- 'v*'
env:
# github.repository as <account>/<repo>
DOCKER_IMAGE_NAME: missuo/deeplx
GHCR_IMAGE_NAME: ${{ github.repository }}
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
GHCR_TOKEN: ${{ secrets.GT_Token }}
GHCR_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GHCR_USERNAME: ${{ github.repository_owner }}
jobs:
@@ -4,16 +4,16 @@ on:
- 'v*'
pull_request:
name: Build Release
name: Release
jobs:
Build:
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
runs-on: self-hosted
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v2
- uses: actions/setup-go@v4
with:
go-version: "1.19"
+64 -36
View File
@@ -2,29 +2,35 @@
* @Author: Vincent Young
* @Date: 2022-10-18 07:32:29
* @LastEditors: Vincent Young
* @LastEditTime: 2023-07-23 20:42:10
* @LastEditTime: 2023-11-27 15:06:22
* @FilePath: /DeepLX/README.md
* @Telegram: https://t.me/missuo
*
* Copyright © 2022 by Vincent, All Rights Reserved.
-->
<h1 align="center">
<br>DeepL X<br>
</h1>
<h4 align="center">Permanently free DeepL API written in Golang.</h4>
<p align="center">
<a href="https://goreportcard.com/report/github.com/OwO-Network/DeepLX">
<img src="https://goreportcard.com/badge/github.com/OwO-Network/DeepLX?style=flat-square">
</a>
<a href="https://github.com/OwO-Network/DeepLX/releases">
<img src="https://img.shields.io/github/release/OwO-Network/DeepLX/all.svg?style=flat-square">
</a>
</p>
[![GitHub Workflow][1]](https://github.com/OwO-Network/DeepLX/actions)
[![Go Version][2]](https://github.com/OwO-Network/DeepLX/blob/main/go.mod)
[![Go Report][3]](https://goreportcard.com/badge/github.com/OwO-Network/DeepLX)
[![Maintainability][4]](https://codeclimate.com/github/OwO-Network/DeepLX/maintainability)
[![GitHub License][5]](https://github.com/OwO-Network/DeepLX/blob/main/LICENSE)
[![Docker Pulls][6]](https://hub.docker.com/r/missuo/deeplx)
[![Releases][7]](https://github.com/OwO-Network/DeepLX/releases)
[1]: https://img.shields.io/github/actions/workflow/status/OwO-Network/DeepLX/release.yaml?logo=github
[2]: https://img.shields.io/github/go-mod/go-version/OwO-Network/DeepLX?logo=go
[3]: https://goreportcard.com/badge/github.com/OwO-Network/DeepLX
[4]: https://api.codeclimate.com/v1/badges/b5b30239174fc6603aca/maintainability
[5]: https://img.shields.io/github/license/OwO-Network/DeepLX
[6]: https://img.shields.io/docker/pulls/missuo/deeplx?logo=docker
[7]: https://img.shields.io/github/v/release/OwO-Network/DeepLX?logo=smartthings
## **Related Projects**
[OwO-Network/PyDeepLX](https://github.com/OwO-Network/PyDeepLX): Python Package for DeepLX.
[OwO-Network/gdeeplx](https://github.com/OwO-Network/gdeeplx): Golang Package for DeepLX.
| Link | Description | Maintainer |
| ------------------------------------------ | -------------------------------------- | ----------------- |
| [OwO-Network/PyDeepLX](https://github.com/OwO-Network/PyDeepLX) | Python Package for DeepLX | [Vincent Yang](https://github.com/missuo) |
| [OwO-Network/gdeeplx](https://github.com/OwO-Network/gdeeplx) | Golang Package for DeepLX | [Vincent Yang](https://github.com/missuo) |
| [ifyour/deeplx](https://github.com/ifyour/deeplx) | JS Package for DeepLX (JavaScript) | [ifyour](https://github.com/ifyour) |
## Discussion Group
[Telegram Group](https://t.me/+8KDGHKJCxEVkNzll)
@@ -35,30 +41,44 @@
- `DeepLX` is unlimited to the number of requests.
## Usage
### Request Parameters
### For Developer
#### Request Parameters
- text: string
- source_lang: string
- target_lang: string
### Response
#### Response
```json
{
"alternatives": [
"Undisputed",
"Unquestionable",
"Unquestionably"
"Did you hear about this?",
"You've heard about this?",
"You've heard of this?"
],
"code": 200,
"data": "Undoubtedly",
"id": 8300079001
"data": "Have you heard about this?",
"id": 8356681003,
"method": "Free",
"source_lang": "ZH",
"target_lang": "EN"
}
```
### Specify the port
**Thanks to [cijiugechu](https://github.com/cijiugechu) for [his contribution](https://github.com/OwO-Network/DeepLX/commit/4a0920579ea868b0f05ccdff6bceae316bfd5dc8) to make this feature possible for this project!**
```bash
./deeplx -p 3333
# or
./deeplx -port 3333
### Custom Options
**The following settings are optional and not required.**
- `-port` or `-p` : Listening port. Default is `1188`.
- `-token` : Access token. If you have set it up, each request will need to include an `Authorization` header.
- `-authkey` : DeepL Official `AuthKey`. If you have set it up, after the 429 response, the official AuthKey will be used for the request.
#### Requesting a token-protected **DeepLX API** instance using the `curl`
```
curl -X POST http://localhost:1188/translate \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_access_token" \
-d '{
"text": "Hello, world!",
"source_lang": "EN",
"target_lang": "DE"
}'
```
### Run with Docker
@@ -66,18 +86,26 @@
# ghcr.io
docker run -itd -p 1188:1188 ghcr.io/owo-network/deeplx:latest
# custom environment variables
docker run -itd -p 1188:1188 -e "TOKEN=helloxxx" -e "AUTHKEY=xxxx:fx" ghcr.io/owo-network/deeplx:latest
# dockerhub
docker run -itd -p 1188:1188 missuo/deeplx:latest
# custom environment variables
docker run -itd -p 1188:1188 -e "TOKEN=helloxxx" -e "AUTHKEY=xxxx:fx" missuo/deeplx:latest
```
### Run with Docker Compose
```bash
mkdir deeplx
cd deeplx
wget https://raw.githubusercontent.com/OwO-Network/DeepLX/main/docker-compose.yaml
# docker compose v1
docker-compose up -d
# or docker compose v2
wget https://raw.githubusercontent.com/OwO-Network/DeepLX/main/compose.yaml
# modify environment variables
# environment:
# - TOKEN=helloxxx
# - AUTHKEY=xxxxxxx:fx
# docker compose
docker compose up -d
```
@@ -85,7 +113,7 @@ docker compose up -d
```bash
bash <(curl -Ls https://raw.githubusercontent.com/OwO-Network/DeepLX/main/install.sh)
# or
bash <(curl -Ls https://cpp.li/deeplx)
bash <(curl -Ls https://qwq.mx/deeplx)
```
### Run on Mac
@@ -174,5 +202,5 @@ docker run -itd -p 1188:80 missuo/deeplx-bk
```
**This docker image is not related to this project, as the original author deleted the image, it is only for backup.**
## Author
**DeepL X** © [DeepL X Contributors](https://github.com/OwO-Network/DeepLX/graphs/contributors), Released under the [MIT](./LICENSE) License.<br>
## License
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FOwO-Network%2FDeepLX.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2FOwO-Network%2FDeepLX?ref=badge_large)
+4 -1
View File
@@ -5,4 +5,7 @@ services:
image: ghcr.io/owo-network/deeplx:latest
restart: always
ports:
- "1188:1188"
- "1188:1188"
# environment:
# - TOKEN=helloworld
# - AUTHKEY=xxxxxxx:fx
+5 -5
View File
@@ -4,13 +4,13 @@ go 1.19
require (
github.com/abadojack/whatlanggo v1.0.1
github.com/andybalholm/brotli v1.0.5
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.9.1
github.com/tidwall/gjson v1.14.3
)
require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
@@ -31,10 +31,10 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
+8 -8
View File
@@ -100,24 +100,24 @@ golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUu
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+139 -27
View File
@@ -2,7 +2,7 @@
* @Author: Vincent Young
* @Date: 2023-07-01 21:45:34
* @LastEditors: Vincent Young
* @LastEditTime: 2023-09-14 13:34:42
* @LastEditTime: 2023-11-27 15:10:06
* @FilePath: /DeepLX/main.go
* @Telegram: https://t.me/missuo
*
@@ -17,6 +17,7 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
@@ -31,18 +32,36 @@ import (
"github.com/tidwall/gjson"
)
var port int
type Config struct {
Port int
Token string
AuthKey string
}
func init() {
const (
defaultPort = 1188
usage = "set up the port to listen on"
)
func InitConfig() *Config {
cfg := &Config{
Port: 1188,
}
flag.IntVar(&port, "port", defaultPort, usage)
flag.IntVar(&port, "p", defaultPort, usage)
flag.IntVar(&cfg.Port, "port", cfg.Port, "set up the port to listen on")
flag.IntVar(&cfg.Port, "p", cfg.Port, "set up the port to listen on")
log.SetFlags(log.LstdFlags | log.Lshortfile)
flag.StringVar(&cfg.Token, "token", "", "set the access token for /translate endpoint")
if cfg.Token == "" {
if token, ok := os.LookupEnv("TOKEN"); ok {
cfg.Token = token
}
}
flag.StringVar(&cfg.AuthKey, "authkey", "", "The authentication key for DeepL API")
if cfg.AuthKey == "" {
if authKey, ok := os.LookupEnv("AUTHKEY"); ok {
cfg.AuthKey = authKey
}
}
flag.Parse()
return cfg
}
type Lang struct {
@@ -115,20 +134,90 @@ func getTimeStamp(iCount int64) int64 {
}
}
type ResData struct {
type PayloadFree struct {
TransText string `json:"text"`
SourceLang string `json:"source_lang"`
TargetLang string `json:"target_lang"`
}
func main() {
// Parsing the command-line flags
flag.Parse()
type PayloadAPI struct {
Text []string `json:"text"`
TargetLang string `json:"target_lang"`
SourceLang string `json:"source_lang"`
}
// Displaying initialization information
fmt.Printf("DeepL X has been successfully launched! Listening on 0.0.0.0:%v\n", port)
type Translation struct {
Text string `json:"text"`
}
type TranslationResponse struct {
Translations []Translation `json:"translations"`
}
func translateByAPI(text string, targetLang string, sourceLang string, authKey string) (string, error) {
url := "https://api-free.deepl.com/v2/translate"
textArray := strings.Split(text, "\n")
payload := PayloadAPI{
Text: textArray,
TargetLang: targetLang,
SourceLang: sourceLang,
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes))
if err != nil {
return "", err
}
req.Header.Set("Authorization", "DeepL-Auth-Key "+authKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
// Parsing the response
var translationResponse TranslationResponse
err = json.Unmarshal(body, &translationResponse)
if err != nil {
return "", err
}
// Concatenating the translations
var sb strings.Builder
for _, translation := range translationResponse.Translations {
sb.WriteString(translation.Text)
}
return sb.String(), nil
}
func main() {
cfg := InitConfig()
fmt.Printf("DeepL X has been successfully launched! Listening on 0.0.0.0:%v\n", cfg.Port)
fmt.Println("Developed by sjlleo <i@leo.moe> and missuo <me@missuo.me>.")
if cfg.Token != "" {
fmt.Println("Access token is set.")
}
if cfg.AuthKey != "" {
fmt.Println("DeepL Official Authentication key is set.")
}
// Generating a random ID
id := getRandomNumber()
@@ -141,19 +230,30 @@ func main() {
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"code": 200,
"message": "DeepL Free API, Developed by sjlleo <i@leo.moe> and missuo <me@missuo.me>. Go to /translate with POST. http://github.com/OwO-Network/DeepLX",
"message": "DeepL Free API, Developed by sjlleo and missuo. Go to /translate with POST. http://github.com/OwO-Network/DeepLX",
})
})
// Defining the translation endpoint which receives translation requests and returns translations
r.POST("/translate", func(c *gin.Context) {
reqj := ResData{}
c.BindJSON(&reqj)
req := PayloadFree{}
c.BindJSON(&req)
if cfg.Token != "" {
providedToken := c.GetHeader("Authorization")
if providedToken != "Bearer "+cfg.Token {
c.JSON(http.StatusUnauthorized, gin.H{
"code": http.StatusUnauthorized,
"message": "Invalid access token",
})
return
}
}
// Extracting details from the request JSON
sourceLang := reqj.SourceLang
targetLang := reqj.TargetLang
translateText := reqj.TransText
sourceLang := req.SourceLang
targetLang := req.TargetLang
translateText := req.TransText
// If source language is not specified, auto-detect it
if sourceLang == "" {
@@ -252,9 +352,21 @@ func main() {
}
if resp.StatusCode == http.StatusTooManyRequests {
c.JSON(http.StatusTooManyRequests, gin.H{
"code": http.StatusTooManyRequests,
"message": "Too Many Requests",
translatedText, err := translateByAPI(translateText, sourceLang, targetLang, cfg.AuthKey)
if err != nil {
c.JSON(http.StatusTooManyRequests, gin.H{
"code": http.StatusTooManyRequests,
"message": "Too Many Requests",
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"id": 114514,
"data": translatedText,
"source_lang": sourceLang,
"target_lang": targetLang,
"method": "Official API",
})
} else {
var alternatives []string
@@ -269,6 +381,7 @@ func main() {
"alternatives": alternatives,
"source_lang": sourceLang,
"target_lang": targetLang,
"method": "Free",
})
}
})
@@ -281,11 +394,10 @@ func main() {
})
})
// Determining which port to run the server on, with a fallback to a default port
envPort, ok := os.LookupEnv("PORT")
if ok {
r.Run(":" + envPort)
} else {
r.Run(fmt.Sprintf(":%v", port))
r.Run(fmt.Sprintf(":%v", cfg.Port))
}
}