diff --git a/README.md b/README.md index 6c370fd..b38869f 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,7 @@ 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 listening to `0.0.0.0:1188` by default, you can change it to what you want. - `deeplx` is using `DeepL` Free API. - `deeplx` is unlimited to the number of requests. @@ -83,6 +82,14 @@ systemctl daemon-reload systemctl enable deeplx ``` + +### Configuration + +You can change the default configuration by setting environment variables, such as setting the port: +```bash +env DEEPLX_PORT=27001 deeplx_linux_amd64 +``` + ## Setup on [Bob App](https://bobtranslate.com/) 1. Install [bob-plugin-deeplx](https://github.com/clubxdev/bob-plugin-deeplx) on Bob. diff --git a/main.go b/main.go index 4b9f048..5120b9f 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,8 @@ import ( "log" "math/rand" "net/http" + "os" + "strconv" "strings" "time" @@ -90,6 +92,34 @@ func getTimeStamp(iCount int64) int64 { } } +const ( + DEEPLXPORT = "DEEPLX_PORT" +) + +type Config struct { + Port uint16 +} + +var DefaultConfig = Config{ + Port: 1188, +} + +func (config *Config) readConfig() { + portStr := os.Getenv(DEEPLXPORT) + + if portStr == "" { + return + } + + port, err := strconv.ParseUint(portStr, 10, 32) + + if err != nil { + log.Printf("Unable to resolve %v, please give the correct port", DEEPLXPORT) + } + + config.Port = uint16(port) +} + type ResData struct { TransText string `json:"text"` SourceLang string `json:"source_lang"` @@ -97,8 +127,14 @@ type ResData struct { } func main() { + // read user config + config := DefaultConfig + config.readConfig() + + port := config.Port + // 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 @@ -216,5 +252,5 @@ func main() { } } }) - r.Run(":1188") // listen and serve on 0.0.0.0:1188 + r.Run(fmt.Sprintf(":%v", port)) //By default, listen and serve on 0.0.0.0:1188 }