Skip to content

Commit

Permalink
feat: add an option to listen on a specific IP address (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
Long0x0 authored Aug 15, 2024
1 parent 7090ead commit 0d73552
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
12 changes: 12 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,26 @@ package main
import (
"flag"
"os"
"fmt"
)

func initConfig() *Config {
cfg := &Config{
IP: "0.0.0.0",
Port: 1188,
}

// IP flag
if ip, ok := os.LookupEnv("IP"); ok && ip != "" {
cfg.IP = ip
}
flag.StringVar(&cfg.IP, "ip", cfg.IP, "set up the IP address to bind to")
flag.StringVar(&cfg.IP, "i", cfg.IP, "set up the IP address to bind to")

// Port flag
if port, ok := os.LookupEnv("PORT"); ok && port != "" {
fmt.Sscanf(port, "%d", &cfg.Port)
}
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")

Expand Down
9 changes: 2 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func authMiddleware(cfg *Config) gin.HandlerFunc {
func main() {
cfg := initConfig()

fmt.Printf("DeepL X has been successfully launched! Listening on 0.0.0.0:%v\n", cfg.Port)
fmt.Printf("DeepL X has been successfully launched! Listening on %v:%v\n", cfg.IP, cfg.Port)
fmt.Println("Developed by sjlleo <[email protected]> and missuo <[email protected]>.")

// Set Proxy
Expand Down Expand Up @@ -255,10 +255,5 @@ func main() {
})
})

envPort, ok := os.LookupEnv("PORT")
if ok {
r.Run(":" + envPort)
} else {
r.Run(fmt.Sprintf(":%v", cfg.Port))
}
r.Run(fmt.Sprintf("%v:%v", cfg.IP, cfg.Port))
}
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package main

type Config struct {
IP string
Port int
Token string
AuthKey string
Expand Down

0 comments on commit 0d73552

Please sign in to comment.