me-no-dev / ESPAsyncWebServer

Async Web Server for ESP8266 and ESP32
3.71k stars 1.21k forks source link

WebServer inside class pass processor template #1344

Open Maverik1408 opened 1 year ago

Maverik1408 commented 1 year ago

hi i have create a class with che async web server but not setting processor

my h file is

class Webserver { public: explicit Webserver(Config *cfg, Debug *debug, uint8_t port = 80) : cfg(cfg), debug(debug), server(port) {}; void Start(void); private: Config *cfg; Debug *debug; AsyncWebServer server; String WebpageProcessor_(const String &var); };

and my cpp file is

` String Webserver::WebpageProcessor_(const String &var) { if (var == "fw_name") { return cfg->GetFWName(); } } void Webserver::Start(void) { debug->DEBUG_PRINTLN("[WEBSERVER] Initializze WebServer..."); debug->DEBUG_PRINTLN(cfg->GetFWName()); //Route for restart ESP server.on("/restart", HTTP_POST, [](AsyncWebServerRequest *request) { ESP.restart(); });

// Route for web page server.on("/", HTTPGET, [](AsyncWebServerRequest *request) { request->send(SD, "/index.html", "text/html", false, WebpageProcessor); });

// Route for not found web page server.onNotFound([](AsyncWebServerRequest *request) { Serial.printf("[404] Not Found: http://%s%s", request->host().c_str(), request->url().c_str()); request->send(404); });

server.serveStatic("/", SD, "/");

server.begin(); debug->DEBUG_PRINTLN("[WEBSERVER] WebServer Started..."); }

`

but this row request->send(SD, "/index.html", "text/html", false, WebpageProcessor_); not work :( :(

we can help me please?

dontsovcmc commented 11 months ago

I think the save handlers paths is a problem. See: server.serveStatic("/", SD, "/"); server.on("/",

I thinks this code can helps:

server.serveStatic("/", SD, "/").setDefaultFile("index.html");
server.on("/index.html", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(SD, "/index.html", "text/html", false, WebpageProcessor_);
});

If WebpageProcessor_ wont' work in this case, I think you should separate html files and others and write:

server.serveStatic("/", SD, "/").setDefaultFile("index.html").setTemplateProcessor(WebpageProcessor_);
server.serveStatic("/static", SD, "/static");