Skip to content

Create a ResourceNode for a method within a class #90

Open
@surfsoft

Description

@surfsoft

I've a situation where the method for handling a particular HTTP request is inside a class and I'm unable to work out how to registerit with the HTTP server. Here's a minimal self-contained example of my issue:

#include <HTTPServer.hpp>
#include <HTTPRequest.hpp>
#include <HTTPResponse.hpp>

using namespace httpsserver;

class ConfigurationModule {

  public:

    void handleDisplayConfig(HTTPRequest *req, HTTPResponse *res) {
      
    }

    void init(HTTPServer server) {
        std::function<void(HTTPRequest *, HTTPResponse *)> handler = std::bind(&ConfigurationModule::handleDisplayConfig, this, std::placeholders::_1, std::placeholders::_2);
        server.registerNode(new ResourceNode("/configure", "GET", handler));
    }
    
};

HTTPServer webserver = HTTPServer();
ConfigurationModule config = ConfigurationModule();

void handler1(HTTPRequest *req, HTTPResponse *res) {
    
}

void handler2(HTTPRequest *req, HTTPResponse *res) {
    
}

void setup() {

    Serial.begin(115200);

    webserver.registerNode(new ResourceNode("/htmlResponse", "GET", handler1));
    webserver.registerNode(new ResourceNode("/jsonResponse", "GET", handler2));

    config.init(webserver);
    webserver.start();

}

void loop() {
    webserver.loop();
}

This is a technique I've used across my fairly extensive codebase (which I'm currently porting from the ESP8266). However, the code in the init function won't compile, specifically it complains that handler has an incorrect signature, even though the method in question has the same signature as handler1 and handler2:

no known conversion for argument 3 from 'std::function<void(httpsserver::HTTPRequest*, httpsserver::HTTPResponse*)>' to 'void (*)(httpsserver::HTTPRequest*, httpsserver::HTTPResponse*)'

I'm unable to work out the correct syntax for resolving the class method to the typedef HTTPSCallbackFunction, or how to tweak the existing code to get rid of the error I have.

Can anybody help me here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions