#include "CrossPointWebServer.h" #include #include "config.h" // Global instance CrossPointWebServer crossPointWebServer; // HTML page template static const char* HTML_PAGE = R"rawliteral( CrossPoint Reader

📚 CrossPoint Reader

Device Status

Version %VERSION%
WiFi Status Connected
IP Address %IP_ADDRESS%
Free Memory %FREE_HEAP% bytes

File Management

📁 File upload functionality coming soon...

CrossPoint E-Reader • Open Source

)rawliteral"; CrossPointWebServer::CrossPointWebServer() {} CrossPointWebServer::~CrossPointWebServer() { stop(); } void CrossPointWebServer::begin() { if (running) { Serial.printf("[%lu] [WEB] Web server already running\n", millis()); return; } if (WiFi.status() != WL_CONNECTED) { Serial.printf("[%lu] [WEB] Cannot start webserver - WiFi not connected\n", millis()); return; } Serial.printf("[%lu] [WEB] Creating web server on port %d...\n", millis(), port); server = new WebServer(port); if (!server) { Serial.printf("[%lu] [WEB] Failed to create WebServer!\n", millis()); return; } // Setup routes Serial.printf("[%lu] [WEB] Setting up routes...\n", millis()); server->on("/", HTTP_GET, [this]() { handleRoot(); }); server->on("/status", HTTP_GET, [this]() { handleStatus(); }); server->onNotFound([this]() { handleNotFound(); }); server->begin(); running = true; Serial.printf("[%lu] [WEB] Web server started on port %d\n", millis(), port); Serial.printf("[%lu] [WEB] Access at http://%s/\n", millis(), WiFi.localIP().toString().c_str()); } void CrossPointWebServer::stop() { if (!running || !server) { return; } server->stop(); delete server; server = nullptr; running = false; Serial.printf("[%lu] [WEB] Web server stopped\n", millis()); } void CrossPointWebServer::handleClient() { static unsigned long lastDebugPrint = 0; if (running && server) { // Print debug every 10 seconds to confirm handleClient is being called if (millis() - lastDebugPrint > 10000) { Serial.printf("[%lu] [WEB] handleClient active, server running on port %d\n", millis(), port); lastDebugPrint = millis(); } server->handleClient(); } } void CrossPointWebServer::handleRoot() { String html = HTML_PAGE; // Replace placeholders with actual values html.replace("%VERSION%", CROSSPOINT_VERSION); html.replace("%IP_ADDRESS%", WiFi.localIP().toString()); html.replace("%FREE_HEAP%", String(ESP.getFreeHeap())); server->send(200, "text/html", html); Serial.printf("[%lu] [WEB] Served root page\n", millis()); } void CrossPointWebServer::handleNotFound() { String message = "404 Not Found\n\n"; message += "URI: " + server->uri() + "\n"; server->send(404, "text/plain", message); } void CrossPointWebServer::handleStatus() { String json = "{"; json += "\"version\":\"" + String(CROSSPOINT_VERSION) + "\","; json += "\"ip\":\"" + WiFi.localIP().toString() + "\","; json += "\"rssi\":" + String(WiFi.RSSI()) + ","; json += "\"freeHeap\":" + String(ESP.getFreeHeap()) + ","; json += "\"uptime\":" + String(millis() / 1000); json += "}"; server->send(200, "application/json", json); }