package handlers import ( "encoding/json" "net/http" "github.com/scanner/backend/middleware" "github.com/scanner/backend/services" ) // ScanHandler manages scan-related HTTP endpoints type ScanHandler struct { scannerService *services.ScannerService } // NewScanHandler creates a new scan handler func NewScanHandler(scannerService *services.ScannerService) *ScanHandler { return &ScanHandler{ scannerService: scannerService, } } // GetScanStatus handles requests to get the current scan status func (h *ScanHandler) GetScanStatus(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } middleware.ApplyCors(w, r) status := h.scannerService.IsScanning() w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]bool{"isScanning": status}) } // StartScan handles requests to start a new scan func (h *ScanHandler) StartScan(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } middleware.ApplyCors(w, r) success, _ := h.scannerService.StartScan() if !success { http.Error(w, "Scan already in progress", http.StatusConflict) return } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]bool{"success": true}) } // AbortScan handles requests to abort an ongoing scan func (h *ScanHandler) AbortScan(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } middleware.ApplyCors(w, r) success, _ := h.scannerService.AbortScan() if !success { http.Error(w, "No scan in progress", http.StatusBadRequest) return } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]bool{"success": true}) } // GetDocuments handles requests to retrieve all scanned documents func (h *ScanHandler) GetDocuments(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } middleware.ApplyCors(w, r) documents := h.scannerService.GetDocuments() w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(documents) }