Files
remote-rig/internal/api/status.go
T
Hermes 1f253283f8
CI/CD / lint-and-typecheck (pull_request) Failing after 12m11s
CI/CD / test (pull_request) Has been cancelled
CI/CD / build (pull_request) Has been cancelled
CI/CD / deploy (pull_request) Has been cancelled
fix: harden camera API endpoints (CUB-234)
- Add request validation: Content-Type check, body size limit (64KB)
- Add field length validation (camera_id: 64, friendly_name: 128, mode: 32, resolution: 32)
- Add FPS range validation (0-240)
- Add battery_pct range validation (0-100)
- Replace ad-hoc map[string]string errors with structured APIError {error, code, details}
- Fix isUniqueConstraintErr to catch both camera_id and mac_address constraint violations
- Fix MacAddress model field from string to *string for NULL handling
- Fix splitSQL to strip -- line comments before splitting (was causing migration failures with modernc.org/sqlite)
- Add 30 integration tests covering all endpoints
- All tests pass: ok github.com/cubecraft/remoterig/internal/api
2026-05-23 08:50:21 -04:00

118 lines
3.6 KiB
Go

// Package api provides HTTP handlers for camera operations.
package api
import (
"log"
"net/http"
"github.com/cubecraft/remoterig/internal/db"
"github.com/go-chi/chi/v5"
)
// PushStatus accepts a status update from an ESP32 node and persists it.
func PushStatus(database *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cameraID := chi.URLParam(r, "id")
if !validateCameraID(w, cameraID) {
return
}
var req struct {
BatteryPct *int `json:"battery_pct"`
VideoRemainingSec *int `json:"video_remaining_sec"`
Recording bool `json:"recording"`
Mode string `json:"mode"`
Resolution string `json:"resolution"`
FPS int `json:"fps"`
Online bool `json:"online"`
RawBatteryPct *float64 `json:"raw_battery_pct"`
Timestamp *string `json:"ts"`
}
if !decodeJSONBody(w, r, &req) {
return
}
if !validateStatusFields(w, req.Mode, req.Resolution, req.FPS) {
return
}
// Validate battery percentage range if provided
if req.BatteryPct != nil && (*req.BatteryPct < 0 || *req.BatteryPct > 100) {
respondError(w, http.StatusBadRequest, "battery_pct must be between 0 and 100")
return
}
// Check if camera is registered
var exists int
err := database.QueryRowContext(r.Context(),
"SELECT COUNT(*) FROM cameras WHERE camera_id = ?", cameraID).Scan(&exists)
if err != nil {
log.Printf("Error checking camera existence: %v", err)
respondError(w, http.StatusInternalServerError, "database error", err.Error())
return
}
if exists == 0 {
respondError(w, http.StatusNotFound, "camera not found")
return
}
// Insert status log
result, err := database.ExecContext(r.Context(), `
INSERT INTO status_logs (camera_id, battery_pct, video_remaining_sec,
recording_state, mode, resolution, fps, online, raw_battery_pct)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`, cameraID, req.BatteryPct, req.VideoRemainingSec,
boolToInt(req.Recording), req.Mode, req.Resolution,
req.FPS, boolToInt(req.Online), req.RawBatteryPct)
if err != nil {
log.Printf("Error inserting status log: %v", err)
respondError(w, http.StatusInternalServerError, "database error", err.Error())
return
}
// Check if recording state changed - update recording_events if so
var prevRecording int
err = database.QueryRowContext(r.Context(), `
SELECT recording_state FROM status_logs
WHERE camera_id = ? AND recorded_at > datetime('now', '-60 seconds')
ORDER BY recorded_at DESC LIMIT 1
`, cameraID).Scan(&prevRecording)
if err == nil && prevRecording != boolToInt(req.Recording) {
reason := "manual"
if req.Recording {
// Start recording - open a new event
_, err := database.ExecContext(r.Context(), `
INSERT INTO recording_events (camera_id, started_at, reason)
VALUES (?, datetime('now'), ?)
`, cameraID, reason)
if err != nil {
log.Printf("Error inserting recording event: %v", err)
}
} else {
// Stop recording - close the most recent open event
_, err := database.ExecContext(r.Context(), `
UPDATE recording_events SET stopped_at = datetime('now')
WHERE camera_id = ? AND stopped_at IS NULL
ORDER BY started_at DESC LIMIT 1
`, cameraID)
if err != nil {
log.Printf("Error updating recording event: %v", err)
}
}
}
_, _ = result.RowsAffected() // consume the result
respondJSON(w, http.StatusOK, map[string]string{
"status": "accepted",
})
}
}
// boolToInt converts a bool to 0 or 1 for SQLite storage.
func boolToInt(b bool) int {
if b {
return 1
}
return 0
}