Switched to go-flagsfiller to enable env var config
This commit is contained in:
+40
-31
@@ -2,8 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/itzg/go-flagsfiller"
|
||||||
"github.com/itzg/mc-router/server"
|
"github.com/itzg/mc-router/server"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"net"
|
"net"
|
||||||
@@ -15,18 +15,18 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
type Config struct {
|
||||||
port = flag.Int("port", 25565, "The port bound to listen for Minecraft client connections")
|
Port int `default:"25565" usage:"The [port] bound to listen for Minecraft client connections"`
|
||||||
apiBinding = flag.String("api-binding", "", "The host:port bound for servicing API requests")
|
Mapping string `usage:"Comma-separated mappings of externalHostname=host:port"`
|
||||||
mappings = flag.String("mapping", "", "Comma-separated mappings of externalHostname=host:port")
|
ApiBinding string `usage:"The [host:port] bound for servicing API requests"`
|
||||||
versionFlag = flag.Bool("version", false, "Output version and exit")
|
Version bool `usage:"Output version and exit"`
|
||||||
kubeConfigFile = flag.String("kube-config", "", "The path to a kubernetes configuration file")
|
CpuProfile string `usage:"Enables CPU profiling and writes to given path"`
|
||||||
inKubeCluster = flag.Bool("in-kube-cluster", false, "Use in-cluster kubernetes config")
|
Debug bool `usage:"Enable debug logs"`
|
||||||
cpuProfile = flag.String("cpu-profile", "", "Enables CPU profiling and writes to given path")
|
ConnectionRateLimit int `default:"1" usage:"Max number of connections to allow per second"`
|
||||||
debug = flag.Bool("debug", false, "Enable debug logs")
|
InKubeCluster bool `usage:"Use in-cluster kubernetes config"`
|
||||||
connRateLimit = flag.Int("connection-rate-limit", 1, "Max number of connections to allow per second")
|
KubeConfig string `usage:"The path to a kubernetes configuration file"`
|
||||||
metricsBackend = flag.String("metrics-backend", "discard", "Backend to use for metrics exposure/publishing: discard,expvar")
|
MetricsBackend string `default:"discard" usage:"Backend to use for metrics exposure/publishing: discard,expvar"`
|
||||||
)
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
version = "dev"
|
version = "dev"
|
||||||
@@ -39,26 +39,30 @@ func showVersion() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
var config Config
|
||||||
|
err := flagsfiller.Parse(&config, flagsfiller.WithEnv(""))
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
if *versionFlag {
|
if config.Version {
|
||||||
showVersion()
|
showVersion()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if *debug {
|
if config.Debug {
|
||||||
logrus.SetLevel(logrus.DebugLevel)
|
logrus.SetLevel(logrus.DebugLevel)
|
||||||
logrus.Debug("Debug logs enabled")
|
logrus.Debug("Debug logs enabled")
|
||||||
}
|
}
|
||||||
|
|
||||||
if *cpuProfile != "" {
|
if config.CpuProfile != "" {
|
||||||
cpuProfileFile, err := os.Create(*cpuProfile)
|
cpuProfileFile, err := os.Create(config.CpuProfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).Fatal("trying to create cpu profile file")
|
logrus.WithError(err).Fatal("trying to create cpu profile file")
|
||||||
}
|
}
|
||||||
defer cpuProfileFile.Close()
|
defer cpuProfileFile.Close()
|
||||||
|
|
||||||
logrus.WithField("file", *cpuProfile).Info("Starting cpu profiling")
|
logrus.WithField("file", config.CpuProfile).Info("Starting cpu profiling")
|
||||||
err = pprof.StartCPUProfile(cpuProfileFile)
|
err = pprof.StartCPUProfile(cpuProfileFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).Fatal("trying to start cpu profile")
|
logrus.WithError(err).Fatal("trying to start cpu profile")
|
||||||
@@ -68,33 +72,38 @@ func main() {
|
|||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
metricsBuilder := NewMetricsBuilder()
|
metricsBuilder := NewMetricsBuilder(config.MetricsBackend)
|
||||||
|
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
server.Routes.RegisterAll(parseMappings(*mappings))
|
server.Routes.RegisterAll(parseMappings(config.Mapping))
|
||||||
|
|
||||||
if *connRateLimit < 1 {
|
if config.ConnectionRateLimit < 1 {
|
||||||
*connRateLimit = 1
|
config.ConnectionRateLimit = 1
|
||||||
}
|
}
|
||||||
connector := server.NewConnector(metricsBuilder.BuildConnectorMetrics())
|
connector := server.NewConnector(metricsBuilder.BuildConnectorMetrics())
|
||||||
connector.StartAcceptingConnections(ctx, net.JoinHostPort("", strconv.Itoa(*port)), *connRateLimit)
|
err = connector.StartAcceptingConnections(ctx,
|
||||||
|
net.JoinHostPort("", strconv.Itoa(config.Port)),
|
||||||
if *apiBinding != "" {
|
config.ConnectionRateLimit,
|
||||||
server.StartApiServer(*apiBinding)
|
)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
if config.ApiBinding != "" {
|
||||||
if *inKubeCluster {
|
server.StartApiServer(config.ApiBinding)
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.InKubeCluster {
|
||||||
err = server.K8sWatcher.StartInCluster()
|
err = server.K8sWatcher.StartInCluster()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).Warn("Unable to start k8s integration")
|
logrus.WithError(err).Warn("Unable to start k8s integration")
|
||||||
} else {
|
} else {
|
||||||
defer server.K8sWatcher.Stop()
|
defer server.K8sWatcher.Stop()
|
||||||
}
|
}
|
||||||
} else if *kubeConfigFile != "" {
|
} else if config.KubeConfig != "" {
|
||||||
err := server.K8sWatcher.StartWithConfig(*kubeConfigFile)
|
err := server.K8sWatcher.StartWithConfig(config.KubeConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).Warn("Unable to start k8s integration")
|
logrus.WithError(err).Warn("Unable to start k8s integration")
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -11,14 +11,14 @@ type MetricsBuilder interface {
|
|||||||
BuildConnectorMetrics() *server.ConnectorMetrics
|
BuildConnectorMetrics() *server.ConnectorMetrics
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMetricsBuilder() MetricsBuilder {
|
func NewMetricsBuilder(backend string) MetricsBuilder {
|
||||||
switch *metricsBackend {
|
switch backend {
|
||||||
case "discard":
|
case "discard":
|
||||||
return &discardMetricsBuilder{}
|
return &discardMetricsBuilder{}
|
||||||
case "expvar":
|
case "expvar":
|
||||||
return &expvarMetricsBuilder{}
|
return &expvarMetricsBuilder{}
|
||||||
default:
|
default:
|
||||||
logrus.Fatalf("Unsupported metrics backend: %s", *metricsBackend)
|
logrus.Fatalf("Unsupported metrics backend: %s", backend)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ require (
|
|||||||
github.com/gorilla/mux v1.7.1
|
github.com/gorilla/mux v1.7.1
|
||||||
github.com/hashicorp/golang-lru v0.5.1 // indirect
|
github.com/hashicorp/golang-lru v0.5.1 // indirect
|
||||||
github.com/imdario/mergo v0.3.7 // indirect
|
github.com/imdario/mergo v0.3.7 // indirect
|
||||||
|
github.com/itzg/go-flagsfiller v1.4.1
|
||||||
github.com/json-iterator/go v1.1.6 // indirect
|
github.com/json-iterator/go v1.1.6 // indirect
|
||||||
github.com/juju/ratelimit v1.0.1
|
github.com/juju/ratelimit v1.0.1
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
@@ -20,14 +21,13 @@ require (
|
|||||||
github.com/pkg/errors v0.8.1
|
github.com/pkg/errors v0.8.1
|
||||||
github.com/sirupsen/logrus v1.4.1
|
github.com/sirupsen/logrus v1.4.1
|
||||||
github.com/spf13/pflag v1.0.3 // indirect
|
github.com/spf13/pflag v1.0.3 // indirect
|
||||||
github.com/stretchr/testify v1.2.2
|
github.com/stretchr/testify v1.4.0
|
||||||
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a // indirect
|
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a // indirect
|
||||||
golang.org/x/net v0.0.0-20190415214537-1da14a5a36f2 // indirect
|
golang.org/x/net v0.0.0-20190415214537-1da14a5a36f2 // indirect
|
||||||
golang.org/x/oauth2 v0.0.0-20190402181905-9f3314589c9a // indirect
|
golang.org/x/oauth2 v0.0.0-20190402181905-9f3314589c9a // indirect
|
||||||
golang.org/x/text v0.3.0
|
golang.org/x/text v0.3.0
|
||||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
|
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
|
||||||
gopkg.in/inf.v0 v0.9.1 // indirect
|
gopkg.in/inf.v0 v0.9.1 // indirect
|
||||||
gopkg.in/yaml.v2 v2.2.2 // indirect
|
|
||||||
k8s.io/api v0.0.0-20190313235455-40a48860b5ab
|
k8s.io/api v0.0.0-20190313235455-40a48860b5ab
|
||||||
k8s.io/apimachinery v0.0.0-20190313205120-d7deff9243b1
|
k8s.io/apimachinery v0.0.0-20190313205120-d7deff9243b1
|
||||||
k8s.io/client-go v11.0.0+incompatible
|
k8s.io/client-go v11.0.0+incompatible
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||||
github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE=
|
github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE=
|
||||||
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
|
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/go-kit/kit v0.9.0 h1:wDJmvq38kDhkVxi50ni9ykkdUr1PKgqKOoi01fa0Mdk=
|
github.com/go-kit/kit v0.9.0 h1:wDJmvq38kDhkVxi50ni9ykkdUr1PKgqKOoi01fa0Mdk=
|
||||||
@@ -20,8 +21,12 @@ github.com/gorilla/mux v1.7.1 h1:Dw4jY2nghMMRsh1ol8dv1axHkDwMQK2DHerMNJsIpJU=
|
|||||||
github.com/gorilla/mux v1.7.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
github.com/gorilla/mux v1.7.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
||||||
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
|
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
|
||||||
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||||
|
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334 h1:VHgatEHNcBFEB7inlalqfNqw65aNkM1lGX2yt3NmbS8=
|
||||||
|
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
|
||||||
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
|
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
|
||||||
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
|
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
|
||||||
|
github.com/itzg/go-flagsfiller v1.4.1 h1:h/t5g+WkvsOR449bz1ngU8UGosKNm4Sr3iMNNgOqHfo=
|
||||||
|
github.com/itzg/go-flagsfiller v1.4.1/go.mod h1:mfQgTahSs4OHn8PYev2Wwi1LJXUiYiGuZVCpBLxzbYs=
|
||||||
github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs=
|
github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs=
|
||||||
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
||||||
github.com/juju/ratelimit v1.0.1 h1:+7AIFJVQ0EQgq/K9+0Krm7m530Du7tIz0METWzN0RgY=
|
github.com/juju/ratelimit v1.0.1 h1:+7AIFJVQ0EQgq/K9+0Krm7m530Du7tIz0METWzN0RgY=
|
||||||
@@ -42,9 +47,12 @@ github.com/sirupsen/logrus v1.4.1 h1:GL2rEmy6nsikmW0r8opw9JIRScdMF5hA8cOYLH7In1k
|
|||||||
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
|
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
|
||||||
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
|
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
|
||||||
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
|
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
|
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||||
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a h1:Igim7XhdOpBnWPuYJ70XcNpq8q3BCACtVgNfoJxOV7g=
|
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a h1:Igim7XhdOpBnWPuYJ70XcNpq8q3BCACtVgNfoJxOV7g=
|
||||||
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
|
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
|
||||||
|
|||||||
Reference in New Issue
Block a user