feat: add an option to listen on a specific IP address (#136)

This commit is contained in:
Long0x0 2024-08-15 19:19:12 +08:00 committed by GitHub
parent 7090eadfd5
commit 0d73552773
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 7 deletions

View File

@ -15,14 +15,26 @@ package main
import ( import (
"flag" "flag"
"os" "os"
"fmt"
) )
func initConfig() *Config { func initConfig() *Config {
cfg := &Config{ cfg := &Config{
IP: "0.0.0.0",
Port: 1188, 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 // 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, "port", cfg.Port, "set up the port to listen on")
flag.IntVar(&cfg.Port, "p", cfg.Port, "set up the port to listen on") flag.IntVar(&cfg.Port, "p", cfg.Port, "set up the port to listen on")

View File

@ -60,7 +60,7 @@ func authMiddleware(cfg *Config) gin.HandlerFunc {
func main() { func main() {
cfg := initConfig() 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 <i@leo.moe> and missuo <me@missuo.me>.") fmt.Println("Developed by sjlleo <i@leo.moe> and missuo <me@missuo.me>.")
// Set Proxy // Set Proxy
@ -255,10 +255,5 @@ func main() {
}) })
}) })
envPort, ok := os.LookupEnv("PORT") r.Run(fmt.Sprintf("%v:%v", cfg.IP, cfg.Port))
if ok {
r.Run(":" + envPort)
} else {
r.Run(fmt.Sprintf(":%v", cfg.Port))
}
} }

View File

@ -12,6 +12,7 @@
package main package main
type Config struct { type Config struct {
IP string
Port int Port int
Token string Token string
AuthKey string AuthKey string