Add Strix camera discovery system with comprehensive database
This commit adds the complete Strix IP camera stream discovery system: - Go-based API server with SSE support for real-time updates - 3,600+ camera brand database with stream URL patterns - Intelligent fuzzy search across camera models - ONVIF discovery and stream validation - RESTful API with health check, camera search, and stream discovery - Makefile for building and deployment - Comprehensive README documentation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/strix-project/strix/internal/camera/discovery"
|
||||
"github.com/strix-project/strix/internal/models"
|
||||
"github.com/strix-project/strix/pkg/sse"
|
||||
)
|
||||
|
||||
// DiscoverHandler handles stream discovery requests
|
||||
type DiscoverHandler struct {
|
||||
scanner *discovery.Scanner
|
||||
sseServer *sse.Server
|
||||
validator *validator.Validate
|
||||
logger interface{ Debug(string, ...any); Error(string, error, ...any); Info(string, ...any) }
|
||||
}
|
||||
|
||||
// NewDiscoverHandler creates a new discover handler
|
||||
func NewDiscoverHandler(
|
||||
scanner *discovery.Scanner,
|
||||
sseServer *sse.Server,
|
||||
logger interface{ Debug(string, ...any); Error(string, error, ...any); Info(string, ...any) },
|
||||
) *DiscoverHandler {
|
||||
return &DiscoverHandler{
|
||||
scanner: scanner,
|
||||
sseServer: sseServer,
|
||||
validator: validator.New(),
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// ServeHTTP handles discovery requests
|
||||
func (h *DiscoverHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse request body
|
||||
var req models.StreamDiscoveryRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
h.logger.Error("failed to decode discovery request", err)
|
||||
h.sendErrorResponse(w, "Invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Set defaults
|
||||
if req.ModelLimit <= 0 {
|
||||
req.ModelLimit = 6
|
||||
}
|
||||
if req.Timeout <= 0 {
|
||||
req.Timeout = 240 // 4 minutes
|
||||
}
|
||||
if req.MaxStreams <= 0 {
|
||||
req.MaxStreams = 10
|
||||
}
|
||||
|
||||
// Validate request
|
||||
if err := h.validator.Struct(req); err != nil {
|
||||
h.logger.Error("discovery request validation failed", err)
|
||||
h.sendErrorResponse(w, "Validation failed: "+err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
h.logger.Info("stream discovery requested",
|
||||
"target", req.Target,
|
||||
"model", req.Model,
|
||||
"timeout", req.Timeout,
|
||||
"max_streams", req.MaxStreams,
|
||||
"remote_addr", r.RemoteAddr,
|
||||
)
|
||||
|
||||
// Check if SSE is supported
|
||||
flusher, ok := w.(http.Flusher)
|
||||
if !ok {
|
||||
h.logger.Info("SSE not supported by client", "remote_addr", r.RemoteAddr)
|
||||
h.sendErrorResponse(w, "SSE not supported", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Set SSE headers
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("X-Accel-Buffering", "no") // Disable Nginx buffering
|
||||
|
||||
// Flush headers
|
||||
flusher.Flush()
|
||||
|
||||
// Create SSE stream writer
|
||||
streamWriter, err := h.sseServer.NewStreamWriter(w, r)
|
||||
if err != nil {
|
||||
h.logger.Error("failed to create SSE stream", err)
|
||||
return
|
||||
}
|
||||
defer streamWriter.Close()
|
||||
|
||||
// Perform discovery
|
||||
result, err := h.scanner.Scan(r.Context(), req, streamWriter)
|
||||
if err != nil {
|
||||
h.logger.Error("discovery failed", err)
|
||||
streamWriter.SendError(err)
|
||||
return
|
||||
}
|
||||
|
||||
// Send final summary
|
||||
streamWriter.SendJSON("summary", map[string]interface{}{
|
||||
"total_tested": result.TotalTested,
|
||||
"total_found": result.TotalFound,
|
||||
"duration": result.Duration.Seconds(),
|
||||
"streams_count": len(result.Streams),
|
||||
})
|
||||
|
||||
h.logger.Info("discovery completed",
|
||||
"target", req.Target,
|
||||
"tested", result.TotalTested,
|
||||
"found", result.TotalFound,
|
||||
"duration", result.Duration,
|
||||
)
|
||||
}
|
||||
|
||||
// sendErrorResponse sends an error response for non-SSE requests
|
||||
func (h *DiscoverHandler) sendErrorResponse(w http.ResponseWriter, message string, statusCode int) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(statusCode)
|
||||
|
||||
response := map[string]interface{}{
|
||||
"error": true,
|
||||
"message": message,
|
||||
"code": statusCode,
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
// HealthResponse represents health check response
|
||||
type HealthResponse struct {
|
||||
Status string `json:"status"`
|
||||
Version string `json:"version"`
|
||||
Uptime int64 `json:"uptime"` // seconds
|
||||
Timestamp string `json:"timestamp"`
|
||||
System SystemInfo `json:"system"`
|
||||
Services map[string]string `json:"services"`
|
||||
}
|
||||
|
||||
// SystemInfo contains system information
|
||||
type SystemInfo struct {
|
||||
GoVersion string `json:"go_version"`
|
||||
NumGoroutine int `json:"num_goroutines"`
|
||||
NumCPU int `json:"num_cpu"`
|
||||
MemoryMB uint64 `json:"memory_mb"`
|
||||
}
|
||||
|
||||
var startTime = time.Now()
|
||||
|
||||
// HealthHandler handles health check endpoint
|
||||
type HealthHandler struct {
|
||||
version string
|
||||
logger interface{ Info(string, ...any) }
|
||||
}
|
||||
|
||||
// NewHealthHandler creates a new health handler
|
||||
func NewHealthHandler(version string, logger interface{ Info(string, ...any) }) *HealthHandler {
|
||||
return &HealthHandler{
|
||||
version: version,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// ServeHTTP handles health check requests
|
||||
func (h *HealthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
h.logger.Info("health check requested", "remote_addr", r.RemoteAddr)
|
||||
|
||||
// Get memory stats
|
||||
var memStats runtime.MemStats
|
||||
runtime.ReadMemStats(&memStats)
|
||||
|
||||
response := HealthResponse{
|
||||
Status: "healthy",
|
||||
Version: h.version,
|
||||
Uptime: int64(time.Since(startTime).Seconds()),
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
System: SystemInfo{
|
||||
GoVersion: runtime.Version(),
|
||||
NumGoroutine: runtime.NumGoroutine(),
|
||||
NumCPU: runtime.NumCPU(),
|
||||
MemoryMB: memStats.Alloc / 1024 / 1024,
|
||||
},
|
||||
Services: map[string]string{
|
||||
"api": "running",
|
||||
"database": "loaded",
|
||||
"scanner": "ready",
|
||||
"sse": "active",
|
||||
},
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
h.logger.Info("failed to encode health response", "error", err.Error())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/strix-project/strix/internal/camera/database"
|
||||
"github.com/strix-project/strix/internal/models"
|
||||
)
|
||||
|
||||
// SearchHandler handles camera search requests
|
||||
type SearchHandler struct {
|
||||
searchEngine *database.SearchEngine
|
||||
validator *validator.Validate
|
||||
logger interface{ Debug(string, ...any); Error(string, error, ...any); Info(string, ...any) }
|
||||
}
|
||||
|
||||
// NewSearchHandler creates a new search handler
|
||||
func NewSearchHandler(
|
||||
searchEngine *database.SearchEngine,
|
||||
logger interface{ Debug(string, ...any); Error(string, error, ...any); Info(string, ...any) },
|
||||
) *SearchHandler {
|
||||
return &SearchHandler{
|
||||
searchEngine: searchEngine,
|
||||
validator: validator.New(),
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// ServeHTTP handles search requests
|
||||
func (h *SearchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse request body
|
||||
var req models.CameraSearchRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
h.logger.Error("failed to decode search request", err)
|
||||
h.sendErrorResponse(w, "Invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Set default limit if not provided
|
||||
if req.Limit <= 0 {
|
||||
req.Limit = 10
|
||||
}
|
||||
|
||||
// Validate request
|
||||
if err := h.validator.Struct(req); err != nil {
|
||||
h.logger.Error("search request validation failed", err)
|
||||
h.sendErrorResponse(w, "Validation failed: "+err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
h.logger.Info("camera search requested",
|
||||
"query", req.Query,
|
||||
"limit", req.Limit,
|
||||
"remote_addr", r.RemoteAddr,
|
||||
)
|
||||
|
||||
// Perform search
|
||||
response, err := h.searchEngine.Search(req.Query, req.Limit)
|
||||
if err != nil {
|
||||
h.logger.Error("search failed", err)
|
||||
h.sendErrorResponse(w, "Search failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Send response
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
h.logger.Error("failed to encode search response", err)
|
||||
}
|
||||
|
||||
h.logger.Info("search completed",
|
||||
"query", req.Query,
|
||||
"returned", response.Returned,
|
||||
"total", response.Total,
|
||||
)
|
||||
}
|
||||
|
||||
// sendErrorResponse sends an error response
|
||||
func (h *SearchHandler) sendErrorResponse(w http.ResponseWriter, message string, statusCode int) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(statusCode)
|
||||
|
||||
response := map[string]interface{}{
|
||||
"error": true,
|
||||
"message": message,
|
||||
"code": statusCode,
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
Reference in New Issue
Block a user