remove init function\

remove global vars proxies, host
This commit is contained in:
onyx-and-iris 2024-09-28 15:36:24 +01:00
parent 2fc0f7a74f
commit 3d1c8f90f3
2 changed files with 46 additions and 50 deletions

View File

@ -3,7 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"strconv" "slices"
"strings" "strings"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -11,7 +11,33 @@ import (
"github.com/onyx-and-iris/q3rcon-proxy/pkg/udpproxy" "github.com/onyx-and-iris/q3rcon-proxy/pkg/udpproxy"
) )
func start(proxy string) { func main() {
logLevel, err := getEnvInt("Q3RCON_LOGLEVEL")
if err != nil {
log.Fatalf("unable to parse Q3RCON_LEVEL: %s", err.Error())
}
if slices.Contains(log.AllLevels, log.Level(logLevel)) {
log.SetLevel(log.Level(logLevel))
}
proxies := os.Getenv("Q3RCON_PROXY")
if proxies == "" {
log.Fatal("env Q3RCON_PROXY required")
}
host := os.Getenv("Q3RCON_HOST")
if host == "" {
host = "0.0.0.0"
}
for _, proxy := range strings.Split(proxies, ";") {
go start(host, proxy)
}
<-make(chan int)
}
func start(host, proxy string) {
port, target := func() (string, string) { port, target := func() (string, string) {
x := strings.Split(proxy, ":") x := strings.Split(proxy, ":")
return x[0], x[1] return x[0], x[1]
@ -26,51 +52,3 @@ func start(proxy string) {
log.Fatal(c.ListenAndServe()) log.Fatal(c.ListenAndServe())
} }
var (
proxies, host string
)
func getenvInt(key string) (int, error) {
s := os.Getenv(key)
if s == "" {
return 0, nil
}
v, err := strconv.Atoi(s)
if err != nil {
return 0, err
}
return v, nil
}
func init() {
proxies = os.Getenv("Q3RCON_PROXY")
if proxies == "" {
log.Fatal("env Q3RCON_PROXY required")
}
host = os.Getenv("Q3RCON_HOST")
if host == "" {
host = "0.0.0.0"
}
debug, err := getenvInt("Q3RCON_DEBUG")
if err != nil {
log.Fatal(err)
}
if debug == 1 {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
}
func main() {
for _, proxy := range strings.Split(proxies, ";") {
go start(proxy)
}
<-make(chan int)
}

18
cmd/q3rcon-proxy/util.go Normal file
View File

@ -0,0 +1,18 @@
package main
import (
"os"
"strconv"
)
func getEnvInt(key string) (int, error) {
s := os.Getenv(key)
if s == "" {
return 0, nil
}
v, err := strconv.Atoi(s)
if err != nil {
return 0, err
}
return v, nil
}