From d02c7b58e7c25c548de413b74592b82fc30322a3 Mon Sep 17 00:00:00 2001 From: pg9182 <96569817+pg9182@users.noreply.github.com> Date: Sun, 20 Nov 2022 03:24:54 -0500 Subject: pkg/atlas: Add ATLAS_API0_REGION_MAP config option, use pkg/regionmap by default --- pkg/atlas/config.go | 4 ++++ pkg/atlas/server.go | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) (limited to 'pkg/atlas') diff --git a/pkg/atlas/config.go b/pkg/atlas/config.go index 4f411bc..149c9ab 100644 --- a/pkg/atlas/config.go +++ b/pkg/atlas/config.go @@ -100,6 +100,10 @@ type Config struct { // allowed. API0_MinimumLauncherVersion string `env:"ATLAS_API0_MINIMUM_LAUNCHER_VERSION"` + // Region mapping to use for server list. If set to an empty string or + // "none", region maps are disabled. Options: none, default. + API0_RegionMap string `env:"ATLAS_API0_REGION_MAP?=default"` + // The time after registration for a gameserver to complete verification by. API0_ServerList_VerifyTime time.Duration `env:"ATLAS_API0_SERVERLIST_VERIFY_TIME=10s"` diff --git a/pkg/atlas/server.go b/pkg/atlas/server.go index 2e90af2..7b25dad 100644 --- a/pkg/atlas/server.go +++ b/pkg/atlas/server.go @@ -21,12 +21,14 @@ import ( "github.com/VictoriaMetrics/metrics" "github.com/klauspost/compress/gzip" + "github.com/pg9182/ip2x/ip2location" "github.com/r2northstar/atlas/db/atlasdb" "github.com/r2northstar/atlas/db/pdatadb" "github.com/r2northstar/atlas/pkg/api/api0" "github.com/r2northstar/atlas/pkg/cloudflare" "github.com/r2northstar/atlas/pkg/memstore" "github.com/r2northstar/atlas/pkg/origin" + "github.com/r2northstar/atlas/pkg/regionmap" "github.com/rs/zerolog" "github.com/rs/zerolog/hlog" "golang.org/x/mod/semver" @@ -254,6 +256,11 @@ func NewServer(c *Config) (*Server, error) { } else { return nil, fmt.Errorf("initialize ip2location: %w", err) } + if m, err := configureRegionMap(c); err == nil { + s.API0.GetRegion = m + } else { + return nil, fmt.Errorf("initialize region map: %w", err) + } s.MetricsSecret = c.MetricsSecret @@ -631,6 +638,17 @@ func configureIP2Location(c *Config) (*ip2locationMgr, error) { return mgr, mgr.Load(c.IP2Location) } +func configureRegionMap(c *Config) (func(netip.Addr, ip2location.Record) (string, error), error) { + switch m := c.API0_RegionMap; m { + case "", "none": + return nil, nil + case "default": + return regionmap.GetRegion, nil + default: + return nil, fmt.Errorf("unknown region map type %q", m) + } +} + // Run runs the server, shutting it down gracefully when ctx is canceled, then // waiting indefinitely for it to exit. It must only ever be called once, and // the server is useless afterwards. -- cgit v1.2.3