Easy-to-use ESP32 IDF webserver C++ wrapper class
#include <esp_http_server.h>
/**
* HTTP server.
* Works with ESP-IDF library internally.
*
* Usage:
* First call http.StartServer()
* Then call http.RegisterHandler(...) for all handlers
*/
class HTTPServer {
public:
HTTPServer(): conf(HTTPD_DEFAULT_CONFIG()) {
}
void StartServer() {
if (httpd_start(&server, &conf) != ESP_OK) {
ESP_LOGE("HTTP server", "Error starting server!");
}
}
void RegisterHandler(const httpd_uri_t *uri_handler) {
httpd_register_uri_handler(this->server, uri_handler);
}
httpd_handle_t server = nullptr;
httpd_config_t conf;
};
Usage example:
// Declare server globally
HTTPServer http;
// Example handler
static const httpd_uri_t rebootHandler = {
.uri = "/api/reboot",
.method = HTTP_GET,
.handler = [](httpd_req_t *req) {
SendStatusOK(req);
delay(20);
ESP.restart();
return ESP_OK;
}
};
void setup() {
// TODO setup wifi or Ethernet
http.StartServer();
http.RegisterHandler(&myHandler);
}