From 3af64c8499b663a265700f3f2c0aee9a9c74c40b Mon Sep 17 00:00:00 2001 From: John Varghese Date: Mon, 9 Jun 2025 23:26:02 +0530 Subject: [PATCH] Fix --- ESP32CAM-ONVIF/web_config.cpp | 48 ++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/ESP32CAM-ONVIF/web_config.cpp b/ESP32CAM-ONVIF/web_config.cpp index f8aea8e..795be61 100644 --- a/ESP32CAM-ONVIF/web_config.cpp +++ b/ESP32CAM-ONVIF/web_config.cpp @@ -1,9 +1,9 @@ #include "web_config.h" #include #include -#include "rtsp_server.h" // For getRTSPUrl() -#include "onvif_server.h" // For ONVIF URL if needed -#include "motion_detection.h" // For motion_detected() +#include "rtsp_server.h" +#include "onvif_server.h" +#include "motion_detection.h" #include #include #include @@ -12,7 +12,6 @@ WebServer webConfigServer(80); -// --- Helper: HTTP Basic Auth --- const char* WEB_USER = "admin"; const char* WEB_PASS = "esp123"; @@ -25,17 +24,36 @@ bool isAuthenticated(WebServer &server) { } void web_config_start() { - // Mount SPIFFS for static files if (!SPIFFS.begin(true)) { Serial.println("SPIFFS/LittleFS Mount Failed"); return; } - // Protect static files with authentication - webConfigServer.serveStatic("/", SPIFFS, "/"); - webConfigServer.setDefaultFile("index.html"); - webConfigServer.setAuthentication(WEB_USER, WEB_PASS); - // === API ENDPOINTS === + // All route definitions go here, inside this function! + webConfigServer.on("/", []() { + File file = SPIFFS.open("/index.html", "r"); + if (!file) { + webConfigServer.send(404, "text/plain", "File not found"); + return; + } + webConfigServer.streamFile(file, "text/html"); + file.close(); + }); + + webConfigServer.on("/", HTTP_GET, []() { + if (!webConfigServer.authenticate(WEB_USER, WEB_PASS)) { + return webConfigServer.requestAuthentication(); + } + File file = SPIFFS.open("/index.html", "r"); + if (!file) { + webConfigServer.send(404, "text/plain", "File not found"); + return; + } + webConfigServer.streamFile(file, "text/html"); + file.close(); + }); + + // --- API ENDPOINTS --- webConfigServer.on("/api/status", HTTP_GET, []() { if (!isAuthenticated(webConfigServer)) return; String json = "{"; @@ -200,9 +218,9 @@ void web_config_start() { }); webConfigServer.begin(); - Serial.println("[INFO] Web config server started."); -} + Serial.println("[INFO] Web config server started."); + } -void web_config_loop() { - webConfigServer.handleClient(); -} + void web_config_loop() { + webConfigServer.handleClient(); + } \ No newline at end of file