Skip to content

Commit

Permalink
pkg/atlas: Load rulesets
Browse files Browse the repository at this point in the history
  • Loading branch information
pg9182 committed Sep 14, 2023
1 parent 59f3909 commit 9d51f22
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/atlas/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ type Config struct {
// 192.168.0.0/24=1.2.3.4).
DevMapIP []string `env:"ATLAS_DEV_MAP_IP"`

// The path to a directory containing lexically-sorted rulesets.
Rules string `env:"ATLAS_RULES"`

// The maximum number of gameservers to allow. If -1, no limit is applied.
API0_MaxServers int `env:"ATLAS_API0_MAX_SERVERS=1000"`

Expand Down
23 changes: 23 additions & 0 deletions pkg/atlas/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/r2northstar/atlas/pkg/eax"
"github.com/r2northstar/atlas/pkg/memstore"
"github.com/r2northstar/atlas/pkg/nspkt"
"github.com/r2northstar/atlas/pkg/nsrule"
"github.com/r2northstar/atlas/pkg/origin"
"github.com/r2northstar/atlas/pkg/regionmap"
"github.com/rs/zerolog"
Expand All @@ -50,6 +51,7 @@ type Server struct {
API0 *api0.Handler
Middleware []func(http.Handler) http.Handler
TLSConfig *tls.Config
Rules *nsrule.RuleSet

reload []func()
closed bool
Expand Down Expand Up @@ -78,6 +80,26 @@ func NewServer(c *Config) (*Server, error) {

s.NotifySocket = c.NotifySocket

if c.Rules != "" {
s.Rules = new(nsrule.RuleSet)
if p, err := filepath.Abs(c.Rules); err == nil {
var err1 error
reload := func() {
if err := s.Rules.LoadFS(os.DirFS(p)); err != nil {
s.Logger.Err(err).Msgf("failed to load rules from %q", p)
err1 = fmt.Errorf("parse rulesets from %q: %w", p, err)
return
}
}
if reload(); err1 != nil {
return nil, fmt.Errorf("initialize rules: %w", err1)
}
s.reload = append(s.reload, reload)
} else {
return nil, fmt.Errorf("initialize rules: resolve path: %w", err)
}
}

if c.Web != "" {
if p, err := filepath.Abs(c.Web); err == nil {
var redirects sync.Map
Expand Down Expand Up @@ -285,6 +307,7 @@ func NewServer(c *Config) (*Server, error) {
MinimumLauncherVersionServer: c.API0_MinimumLauncherVersionServer,
TokenExpiryTime: c.API0_TokenExpiryTime,
AllowGameServerIPv6: c.API0_AllowGameServerIPv6,
Rules: s.Rules,
}
if v := c.API0_MinimumLauncherVersion; v != "" {
if s.API0.MinimumLauncherVersionClient == "" {
Expand Down

0 comments on commit 9d51f22

Please sign in to comment.