Skip to content

Commit

Permalink
switch to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
cijiugechu committed Mar 3, 2023
1 parent 9145b9c commit 4a09205
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 38 deletions.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -85,9 +85,11 @@ systemctl enable deeplx

### Configuration

You can change the default configuration by setting environment variables, such as setting the port:
You can change the default configuration in command line, such as setting the port:
```bash
env DEEPLX_PORT=27001 deeplx_linux_amd64
deeplx_linux_amd64 --port 27001
#or shorthand
deeplx_linux_amd64 -p 27001
```

## Setup on [Bob App](https://bobtranslate.com/)
Expand Down
49 changes: 13 additions & 36 deletions main.go
Expand Up @@ -3,13 +3,12 @@ package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"time"

Expand All @@ -18,7 +17,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)
}

Expand Down Expand Up @@ -92,47 +101,15 @@ 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)
return
}

config.Port = uint16(port)
}

type ResData struct {
TransText string `json:"text"`
SourceLang string `json:"source_lang"`
TargetLang string `json:"target_lang"`
}

func main() {
// read user config
config := DefaultConfig
config.readConfig()

port := config.Port
// parse flags
flag.Parse()

// display information
fmt.Printf("DeepL X has been successfully launched! Listening on 0.0.0.0:%v\n", port)
Expand Down

0 comments on commit 4a09205

Please sign in to comment.