diff --git a/internal/config/config.go b/internal/config/config.go index 2a3a76f..3795d3b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -81,7 +81,7 @@ func (c Config) Validate() error { } } - if c.Configs.Host.set { + if c.Configs.Master.set { if err := c.Configs.Master.Validate(); err != nil { return fmt.Errorf("configs.master: %w", err) } diff --git a/internal/config/host.go b/internal/config/host.go index e3b3af5..9a302ef 100644 --- a/internal/config/host.go +++ b/internal/config/host.go @@ -30,19 +30,19 @@ func (c HostConfig) Validate() error { } if c.LocalAddress == "" { - return errors.New("missing local address") + return errors.New("missing local_address") } if c.InternalEntrypoint == "" { - return errors.New("missing internal entrypoint") + return errors.New("missing internal_entrypoint") } if c.ExternalEntrypoint == "" { - return errors.New("missing external entrypoint") + return errors.New("missing external_entrypoint") } if c.ListenAddress == "" { - return errors.New("missing listen address") + return errors.New("missing listen_address") } return nil diff --git a/internal/roles/host/host.go b/internal/roles/host/host.go index a97a80b..d82d0ee 100644 --- a/internal/roles/host/host.go +++ b/internal/roles/host/host.go @@ -23,6 +23,8 @@ type Role struct { externalDomains []string // TODO: i don't like hardcoding external/internal logic here internalDomains []string + + lock sync.RWMutex } func New(state *app.State, config config.HostConfig) *Role { @@ -56,6 +58,9 @@ func (r *Role) sendUpdate(domains []string, role types.Role) { } func (r *Role) OnTraefikUpdate(resp traefikResponse) { + r.lock.Lock() + defer r.lock.Unlock() + newInternal := resp.Domains(r.config.InternalEntrypoint) newExternal := resp.Domains(r.config.ExternalEntrypoint) @@ -73,6 +78,9 @@ func (r *Role) OnTraefikUpdate(resp traefikResponse) { } func (r *Role) Dns() (types.HostState, error) { + r.lock.RLock() + defer r.lock.RUnlock() + return types.HostState{ Domains: r.internalDomains, Address: r.config.IpAddress, @@ -81,6 +89,9 @@ func (r *Role) Dns() (types.HostState, error) { } func (r *Role) Nameserver() (types.HostState, error) { + r.lock.RLock() + defer r.lock.RUnlock() + return types.HostState{ Domains: r.externalDomains, Address: r.config.IpAddress, diff --git a/internal/roles/host/http.go b/internal/roles/host/http.go deleted file mode 100644 index cd147b7..0000000 --- a/internal/roles/host/http.go +++ /dev/null @@ -1 +0,0 @@ -package host diff --git a/internal/roles/master/observer.go b/internal/roles/master/observer.go index fab3478..281aa39 100644 --- a/internal/roles/master/observer.go +++ b/internal/roles/master/observer.go @@ -39,6 +39,9 @@ func newObserver( } func (o *observer) pollNodes(ctx context.Context, onLeave func(types.Node) error) { + o.lock.Lock() + defer o.lock.Unlock() + nodes := o.state.Registry.Nodes() toCheck := make([]types.Node, 0, len(nodes)) now := time.Now().UnixMilli() diff --git a/internal/web/client/client.go b/internal/web/client/client.go index 3396f6f..720ed9e 100644 --- a/internal/web/client/client.go +++ b/internal/web/client/client.go @@ -61,16 +61,16 @@ func (c *Client) Call(host string, path string, data any, out any) error { return fmt.Errorf("http %d: %s", httpResponse.StatusCode, string(b)) } + var resp web.Response[json.RawMessage] + if err := json.NewDecoder(httpResponse.Body).Decode(&resp); err != nil { + return fmt.Errorf("decode response wrapper: %w", err) + } + + if !resp.Ok { + return fmt.Errorf("error on the remote: %w", errors.New(resp.Err)) + } + if out != nil { - var resp web.Response[json.RawMessage] - if err := json.NewDecoder(httpResponse.Body).Decode(&resp); err != nil { - return fmt.Errorf("decode response wrapper: %w", err) - } - - if !resp.Ok { - return fmt.Errorf("error on the remote: %w", errors.New(resp.Err)) - } - if err := json.Unmarshal(resp.Data, out); err != nil { return fmt.Errorf("decode response body: %w", err) }