Implement GET and POST/PATCH /api/settings endpoints with: - GET /api/settings: Returns all configurable settings as JSON with default values merged in for any settings not explicitly set - POST/PATCH /api/settings: Partial update with merge semantics, only updating the keys provided in the request body - SQLite persistence: Settings stored in settings table with JSON-encoded values, loaded on startup - Comprehensive validation: All known settings validated with proper range checks (fusion_rate_hz, grid_cell_m, delta_rms_threshold, tau_s, fresnel_decay, n_subcarriers, breathing_sensitivity, motion_threshold, dwell_seconds, vacant_seconds, max_tracked_blobs, replay_retention_hours, replay_max_mb, security_mode, events_archive_days) - OpenAPI-style godoc comments: Documentation for all endpoints using swagger annotations - Table-driven tests: Comprehensive test coverage for all functionality including GET, POST, PATCH, validation, persistence, and error cases - Helper utilities: Extracted writeJSON, writeJSONError, writeJSONData to utils.go for reuse Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
30 lines
955 B
Go
30 lines
955 B
Go
// Package api provides REST API handlers for Spaxel.
|
|
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
// writeJSON writes a JSON response with the given status code.
|
|
func writeJSON(w http.ResponseWriter, status int, v interface{}) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
if err := json.NewEncoder(w).Encode(v); err != nil {
|
|
log.Printf("[ERROR] Failed to encode JSON response: %v", err)
|
|
}
|
|
}
|
|
|
|
// writeJSONData writes a JSON response without setting status (assumes status already set).
|
|
func writeJSONData(w http.ResponseWriter, v interface{}) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(v); err != nil {
|
|
log.Printf("[ERROR] Failed to encode JSON response: %v", err)
|
|
}
|
|
}
|
|
|
|
// writeJSONError writes a JSON error response.
|
|
func writeJSONError(w http.ResponseWriter, status int, message string) {
|
|
writeJSON(w, status, map[string]string{"error": message})
|
|
}
|