Split /api/v1/state into dedicated resource endpoints: - /api/v1/operator, /api/v1/central-domain - /api/v1/ddns/config, /api/v1/dns/settings - /api/v1/dhcp/config (moved from /dnsmasq/dhcp/) - /api/v1/nftables/config, /api/v1/haproxy/routes Each page now talks to its own resource instead of deep-merging a blob. Removes useConfig hook, CentralState type, and legacy config methods.
268 lines
7.6 KiB
Go
268 lines
7.6 KiB
Go
package v1
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"github.com/wild-cloud/wild-central/internal/config"
|
|
)
|
|
|
|
// loadState loads state from disk, returning an empty state if the file doesn't exist.
|
|
func (api *API) loadState() *config.State {
|
|
state, err := config.LoadState(api.statePath())
|
|
if err != nil {
|
|
return &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())
|
|
}
|
|
|
|
// --- Operator ---
|
|
|
|
func (api *API) GetOperator(w http.ResponseWriter, r *http.Request) {
|
|
respondJSON(w, http.StatusOK, api.loadState().Operator)
|
|
}
|
|
|
|
func (api *API) UpdateOperator(w http.ResponseWriter, r *http.Request) {
|
|
var updates struct {
|
|
Email string `json:"email"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&updates); err != nil {
|
|
respondError(w, http.StatusBadRequest, "Invalid request body")
|
|
return
|
|
}
|
|
|
|
state := api.loadState()
|
|
state.Operator.Email = updates.Email
|
|
if err := api.saveState(state); err != nil {
|
|
respondError(w, http.StatusInternalServerError, "Failed to save state")
|
|
return
|
|
}
|
|
|
|
slog.Info("operator updated", "email", updates.Email)
|
|
respondJSON(w, http.StatusOK, state.Operator)
|
|
}
|
|
|
|
// --- Central Domain ---
|
|
|
|
func (api *API) GetCentralDomain(w http.ResponseWriter, r *http.Request) {
|
|
respondJSON(w, http.StatusOK, map[string]string{
|
|
"domain": api.loadState().Cloud.Central.Domain,
|
|
})
|
|
}
|
|
|
|
func (api *API) UpdateCentralDomain(w http.ResponseWriter, r *http.Request) {
|
|
var updates struct {
|
|
Domain string `json:"domain"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&updates); err != nil {
|
|
respondError(w, http.StatusBadRequest, "Invalid request body")
|
|
return
|
|
}
|
|
|
|
state := api.loadState()
|
|
state.Cloud.Central.Domain = updates.Domain
|
|
if err := api.saveState(state); err != nil {
|
|
respondError(w, http.StatusInternalServerError, "Failed to save state")
|
|
return
|
|
}
|
|
|
|
go api.EnsureCentralDomain()
|
|
|
|
slog.Info("central domain updated", "domain", updates.Domain)
|
|
respondJSON(w, http.StatusOK, map[string]string{"domain": updates.Domain})
|
|
}
|
|
|
|
// --- DDNS Config ---
|
|
|
|
func (api *API) GetDDNSConfig(w http.ResponseWriter, r *http.Request) {
|
|
respondJSON(w, http.StatusOK, api.loadState().Cloud.DDNS)
|
|
}
|
|
|
|
func (api *API) UpdateDDNSConfig(w http.ResponseWriter, r *http.Request) {
|
|
var updates struct {
|
|
Enabled *bool `json:"enabled,omitempty"`
|
|
Provider string `json:"provider,omitempty"`
|
|
IntervalMinutes *int `json:"intervalMinutes,omitempty"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&updates); err != nil {
|
|
respondError(w, http.StatusBadRequest, "Invalid request body")
|
|
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 {
|
|
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)
|
|
}
|
|
|
|
// --- DNS Settings (dnsmasq IP/interface) ---
|
|
|
|
func (api *API) GetDNSSettings(w http.ResponseWriter, r *http.Request) {
|
|
d := api.loadState().Cloud.Dnsmasq
|
|
respondJSON(w, http.StatusOK, map[string]string{
|
|
"ip": d.IP,
|
|
"interface": d.Interface,
|
|
})
|
|
}
|
|
|
|
func (api *API) UpdateDNSSettings(w http.ResponseWriter, r *http.Request) {
|
|
var updates struct {
|
|
IP string `json:"ip,omitempty"`
|
|
Interface string `json:"interface,omitempty"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&updates); err != nil {
|
|
respondError(w, http.StatusBadRequest, "Invalid request body")
|
|
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 {
|
|
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,
|
|
})
|
|
}
|
|
|
|
// --- DHCP Config ---
|
|
|
|
func (api *API) GetDHCPConfig(w http.ResponseWriter, r *http.Request) {
|
|
respondJSON(w, http.StatusOK, api.loadState().Cloud.Dnsmasq.DHCP)
|
|
}
|
|
|
|
func (api *API) UpdateDHCPConfig(w http.ResponseWriter, r *http.Request) {
|
|
var updates struct {
|
|
Enabled *bool `json:"enabled,omitempty"`
|
|
RangeStart string `json:"rangeStart,omitempty"`
|
|
RangeEnd string `json:"rangeEnd,omitempty"`
|
|
LeaseTime string `json:"leaseTime,omitempty"`
|
|
Gateway string `json:"gateway,omitempty"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&updates); err != nil {
|
|
respondError(w, http.StatusBadRequest, "Invalid request body")
|
|
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 {
|
|
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)
|
|
}
|
|
|
|
// --- nftables Config ---
|
|
|
|
func (api *API) GetNftablesConfig(w http.ResponseWriter, r *http.Request) {
|
|
respondJSON(w, http.StatusOK, api.loadState().Cloud.Nftables)
|
|
}
|
|
|
|
func (api *API) UpdateNftablesConfig(w http.ResponseWriter, r *http.Request) {
|
|
var updates struct {
|
|
Enabled *bool `json:"enabled,omitempty"`
|
|
WANInterface string `json:"wanInterface,omitempty"`
|
|
ExtraPorts []config.ExtraPort `json:"extraPorts,omitempty"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&updates); err != nil {
|
|
respondError(w, http.StatusBadRequest, "Invalid request body")
|
|
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 {
|
|
respondError(w, http.StatusInternalServerError, "Failed to save state")
|
|
return
|
|
}
|
|
|
|
slog.Info("nftables config updated")
|
|
respondJSON(w, http.StatusOK, state.Cloud.Nftables)
|
|
}
|
|
|
|
// --- HAProxy Custom Routes ---
|
|
|
|
func (api *API) GetHAProxyRoutes(w http.ResponseWriter, r *http.Request) {
|
|
respondJSON(w, http.StatusOK, map[string]any{
|
|
"customRoutes": api.loadState().Cloud.HAProxy.CustomRoutes,
|
|
})
|
|
}
|
|
|
|
func (api *API) UpdateHAProxyRoutes(w http.ResponseWriter, r *http.Request) {
|
|
var updates struct {
|
|
CustomRoutes []config.HAProxyCustomRoute `json:"customRoutes"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&updates); err != nil {
|
|
respondError(w, http.StatusBadRequest, "Invalid request body")
|
|
return
|
|
}
|
|
|
|
state := api.loadState()
|
|
state.Cloud.HAProxy.CustomRoutes = updates.CustomRoutes
|
|
if err := api.saveState(state); 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,
|
|
})
|
|
}
|