me-no-dev / ESPAsyncWebServer

Async Web Server for ESP8266 and ESP32
3.78k stars 1.23k forks source link

AsyncWebServerRequest Questions #1404

Open acztassi opened 6 months ago

acztassi commented 6 months ago

hello.

I'm building a little framework for manage a complex automation, then the URIs are not gonna be the same every time.. then I was intending to use AsyncWebHandler, but I really didn't understand if is it possible .. let me show the idea

class NewHandler: public AsyncWebHandler
{
public: 
       bool canHandle(AsyncWebServerRequest *request) override {
             return (request->url() == "/endpoint1" || request->url() == "/endpoint2");
       }  
        virtual esp_err_t handleRequest(AsyncWebServerRequest *request) override{
               if (request->url() == "/endpoint1")
                   { 
                           return request->reply(200, "text/html", "called endpoint 1");
                    }
               else
               if (request->url() == "/endpoint2")
                   { 
                           return request->reply(200, "text/html", "called endpoint 2");
                    }
              else
                    {
                            return request->reply(404, "text/html", "endpoint not managed");
                     }
        } 

}

only add this class and do not use "server.on(...." to set endpoints is it possible? Is there any issue?

CelliesProjects commented 6 months ago

EDIT: Deleted because not applicable.

acztassi commented 6 months ago

the problem i that I can't register handlers trough "server.on( ...", because the uris won't be the same every time .. for a better comprehension, there will be one main uri, like "/apiconfig" where i will register or remove addresses from the webserver. Then I was intending to do not register handlers, but use only one to manage all urls