feat: read config

This commit is contained in:
cijiugechu 2023-03-03 14:48:37 +08:00
parent 9f92a11da1
commit 8826e01499
2 changed files with 47 additions and 4 deletions

View File

@ -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.

40
main.go
View File

@ -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
}