Refactor architecture: extract reconciler, add interfaces, reduce complexity, improve test coverage
Architecture: - Extract reconcileNetworking into internal/reconcile package with 7 consumer-side interfaces - Add locked modifyState helper to fix state.yaml read-modify-write race condition - Extract CrowdSec and VPN handler groups with interfaces documenting dependency surface - Replace raw map[string]any YAML manipulation with typed AddDHCPStaticLease/RemoveDHCPStaticLease - Extract Cloudflare API functions into cfClient struct, eliminating repeated auth boilerplate Complexity reduction: - haproxy.GenerateWithOpts: 50 → 6 (extracted 8 focused helpers) - reconcile.Reconcile: 42 → 11 (extracted buildRoutes, buildDNSEntries, writeHAProxyConfig) - AutheliaUpdateConfig: 25 → 10 (extracted enableAuthelia/disableAuthelia) Test coverage improvements: - reconcile: 4.5% → 56.8% (stub-based tests for route building, DNS entries, orchestration) - dnsfilter: 10.7% → 45.6% (Manager.Compile, AddList, ToggleList, custom entries) - config: 67.3% → 89.8% (DHCP static lease mutation tests)
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/wild-cloud/wild-central/internal/config"
|
||||
"github.com/wild-cloud/wild-central/internal/storage"
|
||||
)
|
||||
|
||||
// loadState loads state from disk, returning an empty state if the file doesn't exist.
|
||||
@@ -17,9 +18,21 @@ func (api *API) loadState() *config.State {
|
||||
return state
|
||||
}
|
||||
|
||||
// saveState writes state to disk and returns an error suitable for HTTP responses.
|
||||
func (api *API) saveState(state *config.State) error {
|
||||
return config.SaveState(state, api.statePath())
|
||||
// modifyState atomically reads state, applies fn, and writes it back under a
|
||||
// file lock. This prevents concurrent handlers from silently dropping each
|
||||
// other's changes via overlapping read-modify-write cycles.
|
||||
func (api *API) modifyState(fn func(state *config.State) error) error {
|
||||
lockPath := api.statePath() + ".lock"
|
||||
return storage.WithLock(lockPath, func() error {
|
||||
state, err := config.LoadState(api.statePath())
|
||||
if err != nil {
|
||||
state = &config.State{}
|
||||
}
|
||||
if err := fn(state); err != nil {
|
||||
return err
|
||||
}
|
||||
return config.SaveState(state, api.statePath())
|
||||
})
|
||||
}
|
||||
|
||||
// --- Operator ---
|
||||
@@ -37,15 +50,18 @@ func (api *API) UpdateOperator(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
state := api.loadState()
|
||||
state.Operator.Email = updates.Email
|
||||
if err := api.saveState(state); err != nil {
|
||||
var result any
|
||||
if err := api.modifyState(func(state *config.State) error {
|
||||
state.Operator.Email = updates.Email
|
||||
result = state.Operator
|
||||
return nil
|
||||
}); err != nil {
|
||||
respondError(w, http.StatusInternalServerError, "Failed to save state")
|
||||
return
|
||||
}
|
||||
|
||||
slog.Info("operator updated", "email", updates.Email)
|
||||
respondJSON(w, http.StatusOK, state.Operator)
|
||||
respondJSON(w, http.StatusOK, result)
|
||||
}
|
||||
|
||||
// --- Central Domain ---
|
||||
@@ -65,9 +81,10 @@ func (api *API) UpdateCentralDomain(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
state := api.loadState()
|
||||
state.Cloud.Central.Domain = updates.Domain
|
||||
if err := api.saveState(state); err != nil {
|
||||
if err := api.modifyState(func(state *config.State) error {
|
||||
state.Cloud.Central.Domain = updates.Domain
|
||||
return nil
|
||||
}); err != nil {
|
||||
respondError(w, http.StatusInternalServerError, "Failed to save state")
|
||||
return
|
||||
}
|
||||
@@ -95,25 +112,28 @@ func (api *API) UpdateDDNSConfig(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
state := api.loadState()
|
||||
if updates.Enabled != nil {
|
||||
state.Cloud.DDNS.Enabled = *updates.Enabled
|
||||
}
|
||||
if updates.Provider != "" {
|
||||
state.Cloud.DDNS.Provider = updates.Provider
|
||||
}
|
||||
if updates.IntervalMinutes != nil {
|
||||
state.Cloud.DDNS.IntervalMinutes = *updates.IntervalMinutes
|
||||
}
|
||||
if err := api.saveState(state); err != nil {
|
||||
var result any
|
||||
if err := api.modifyState(func(state *config.State) error {
|
||||
if updates.Enabled != nil {
|
||||
state.Cloud.DDNS.Enabled = *updates.Enabled
|
||||
}
|
||||
if updates.Provider != "" {
|
||||
state.Cloud.DDNS.Provider = updates.Provider
|
||||
}
|
||||
if updates.IntervalMinutes != nil {
|
||||
state.Cloud.DDNS.IntervalMinutes = *updates.IntervalMinutes
|
||||
}
|
||||
result = state.Cloud.DDNS
|
||||
return nil
|
||||
}); err != nil {
|
||||
respondError(w, http.StatusInternalServerError, "Failed to save state")
|
||||
return
|
||||
}
|
||||
|
||||
go api.reloadDDNSIfEnabled()
|
||||
|
||||
slog.Info("DDNS config updated", "enabled", state.Cloud.DDNS.Enabled)
|
||||
respondJSON(w, http.StatusOK, state.Cloud.DDNS)
|
||||
slog.Info("DDNS config updated")
|
||||
respondJSON(w, http.StatusOK, result)
|
||||
}
|
||||
|
||||
// --- DNS Settings (dnsmasq IP/interface) ---
|
||||
@@ -136,23 +156,26 @@ func (api *API) UpdateDNSSettings(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
state := api.loadState()
|
||||
if updates.IP != "" {
|
||||
state.Cloud.Dnsmasq.IP = updates.IP
|
||||
}
|
||||
if updates.Interface != "" {
|
||||
state.Cloud.Dnsmasq.Interface = updates.Interface
|
||||
}
|
||||
if err := api.saveState(state); err != nil {
|
||||
var result map[string]string
|
||||
if err := api.modifyState(func(state *config.State) error {
|
||||
if updates.IP != "" {
|
||||
state.Cloud.Dnsmasq.IP = updates.IP
|
||||
}
|
||||
if updates.Interface != "" {
|
||||
state.Cloud.Dnsmasq.Interface = updates.Interface
|
||||
}
|
||||
result = map[string]string{
|
||||
"ip": state.Cloud.Dnsmasq.IP,
|
||||
"interface": state.Cloud.Dnsmasq.Interface,
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
respondError(w, http.StatusInternalServerError, "Failed to save state")
|
||||
return
|
||||
}
|
||||
|
||||
slog.Info("DNS settings updated", "ip", state.Cloud.Dnsmasq.IP)
|
||||
respondJSON(w, http.StatusOK, map[string]string{
|
||||
"ip": state.Cloud.Dnsmasq.IP,
|
||||
"interface": state.Cloud.Dnsmasq.Interface,
|
||||
})
|
||||
slog.Info("DNS settings updated", "ip", result["ip"])
|
||||
respondJSON(w, http.StatusOK, result)
|
||||
}
|
||||
|
||||
// --- DHCP Config ---
|
||||
@@ -174,30 +197,33 @@ func (api *API) UpdateDHCPConfig(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
state := api.loadState()
|
||||
dhcp := &state.Cloud.Dnsmasq.DHCP
|
||||
if updates.Enabled != nil {
|
||||
dhcp.Enabled = *updates.Enabled
|
||||
}
|
||||
if updates.RangeStart != "" {
|
||||
dhcp.RangeStart = updates.RangeStart
|
||||
}
|
||||
if updates.RangeEnd != "" {
|
||||
dhcp.RangeEnd = updates.RangeEnd
|
||||
}
|
||||
if updates.LeaseTime != "" {
|
||||
dhcp.LeaseTime = updates.LeaseTime
|
||||
}
|
||||
if updates.Gateway != "" {
|
||||
dhcp.Gateway = updates.Gateway
|
||||
}
|
||||
if err := api.saveState(state); err != nil {
|
||||
var result any
|
||||
if err := api.modifyState(func(state *config.State) error {
|
||||
dhcp := &state.Cloud.Dnsmasq.DHCP
|
||||
if updates.Enabled != nil {
|
||||
dhcp.Enabled = *updates.Enabled
|
||||
}
|
||||
if updates.RangeStart != "" {
|
||||
dhcp.RangeStart = updates.RangeStart
|
||||
}
|
||||
if updates.RangeEnd != "" {
|
||||
dhcp.RangeEnd = updates.RangeEnd
|
||||
}
|
||||
if updates.LeaseTime != "" {
|
||||
dhcp.LeaseTime = updates.LeaseTime
|
||||
}
|
||||
if updates.Gateway != "" {
|
||||
dhcp.Gateway = updates.Gateway
|
||||
}
|
||||
result = state.Cloud.Dnsmasq.DHCP
|
||||
return nil
|
||||
}); err != nil {
|
||||
respondError(w, http.StatusInternalServerError, "Failed to save state")
|
||||
return
|
||||
}
|
||||
|
||||
slog.Info("DHCP config updated", "enabled", dhcp.Enabled)
|
||||
respondJSON(w, http.StatusOK, state.Cloud.Dnsmasq.DHCP)
|
||||
slog.Info("DHCP config updated")
|
||||
respondJSON(w, http.StatusOK, result)
|
||||
}
|
||||
|
||||
// --- nftables Config ---
|
||||
@@ -217,23 +243,26 @@ func (api *API) UpdateNftablesConfig(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
state := api.loadState()
|
||||
if updates.Enabled != nil {
|
||||
state.Cloud.Nftables.Enabled = updates.Enabled
|
||||
}
|
||||
if updates.WANInterface != "" {
|
||||
state.Cloud.Nftables.WANInterface = updates.WANInterface
|
||||
}
|
||||
if updates.ExtraPorts != nil {
|
||||
state.Cloud.Nftables.ExtraPorts = updates.ExtraPorts
|
||||
}
|
||||
if err := api.saveState(state); err != nil {
|
||||
var result any
|
||||
if err := api.modifyState(func(state *config.State) error {
|
||||
if updates.Enabled != nil {
|
||||
state.Cloud.Nftables.Enabled = updates.Enabled
|
||||
}
|
||||
if updates.WANInterface != "" {
|
||||
state.Cloud.Nftables.WANInterface = updates.WANInterface
|
||||
}
|
||||
if updates.ExtraPorts != nil {
|
||||
state.Cloud.Nftables.ExtraPorts = updates.ExtraPorts
|
||||
}
|
||||
result = state.Cloud.Nftables
|
||||
return nil
|
||||
}); err != nil {
|
||||
respondError(w, http.StatusInternalServerError, "Failed to save state")
|
||||
return
|
||||
}
|
||||
|
||||
slog.Info("nftables config updated")
|
||||
respondJSON(w, http.StatusOK, state.Cloud.Nftables)
|
||||
respondJSON(w, http.StatusOK, result)
|
||||
}
|
||||
|
||||
// --- HAProxy Custom Routes ---
|
||||
@@ -253,15 +282,18 @@ func (api *API) UpdateHAProxyRoutes(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
state := api.loadState()
|
||||
state.Cloud.HAProxy.CustomRoutes = updates.CustomRoutes
|
||||
if err := api.saveState(state); err != nil {
|
||||
var result any
|
||||
if err := api.modifyState(func(state *config.State) error {
|
||||
state.Cloud.HAProxy.CustomRoutes = updates.CustomRoutes
|
||||
result = map[string]any{
|
||||
"customRoutes": state.Cloud.HAProxy.CustomRoutes,
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
respondError(w, http.StatusInternalServerError, "Failed to save state")
|
||||
return
|
||||
}
|
||||
|
||||
slog.Info("HAProxy custom routes updated", "count", len(updates.CustomRoutes))
|
||||
respondJSON(w, http.StatusOK, map[string]any{
|
||||
"customRoutes": state.Cloud.HAProxy.CustomRoutes,
|
||||
})
|
||||
respondJSON(w, http.StatusOK, result)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user