From c4e756570313b8d2fda27a9f58231f8886d50092 Mon Sep 17 00:00:00 2001 From: Vincent Young Date: Sun, 23 Apr 2023 13:25:47 +0800 Subject: [PATCH] feat: support for specified ports --- README.md | 16 +++++++++++----- go.mod | 2 +- main.go | 19 +++++++++++++++++-- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index bb81dc0..868e754 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ * @Author: Vincent Young * @Date: 2022-10-18 07:32:29 * @LastEditors: Vincent Young - * @LastEditTime: 2023-03-21 16:55:40 + * @LastEditTime: 2023-04-23 13:25:08 * @FilePath: /DeepLX/README.md * @Telegram: https://t.me/missuo * @@ -12,10 +12,9 @@ Permanently free DeepL API written in Golang ## Description -- `deeplx` in only run in port `1188`, later versions will do the specified port. -- `deeplx` is listening to `0.0.0.0:1188` by default. -- `deeplx` is using `DeepL` Free API. -- `deeplx` is unlimited to the number of requests. +- `DeepLX` is listening to `0.0.0.0:1188` by default. You can modify the listening port by yourself. +- `DeepLX` is using `DeepL` Free API. +- `DeepLX` is unlimited to the number of requests. ## Usage ### Request Parameters @@ -36,6 +35,13 @@ Permanently free DeepL API written in Golang "id": 8300079001 } ``` +### 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 +``` ### Run with Docker ```bash diff --git a/go.mod b/go.mod index e5af4e5..f6333d2 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/OwO-Network/deepl-api +module github.com/OwO-Network/DeepLX go 1.19 diff --git a/main.go b/main.go index 030b540..f585673 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "bytes" "encoding/json" + "flag" "fmt" "io" "log" @@ -17,7 +18,17 @@ import ( "github.com/tidwall/gjson" ) +var port int + func init() { + const ( + defaultPort = 1188 + usage = "set up the port to listen on" + ) + + flag.IntVar(&port, "port", defaultPort, usage) + flag.IntVar(&port, "p", defaultPort, usage) + log.SetFlags(log.LstdFlags | log.Lshortfile) } @@ -98,8 +109,11 @@ type ResData struct { } func main() { + // parse flags + flag.Parse() + // display information - fmt.Println("DeepL X has been successfully launched! Listening on 0.0.0.0:1188") + fmt.Printf("DeepL X has been successfully launched! Listening on 0.0.0.0:%v\n", port) fmt.Println("Made by sjlleo and missuo.") // create a random id @@ -225,5 +239,6 @@ func main() { } } }) - r.Run(":1188") // listen and serve on 0.0.0.0:1188 + // by default, listen and serve on 0.0.0.0:1188 + r.Run(fmt.Sprintf(":%v", port)) }