From 0f187b3c56fc78ddef15a338138722f13bd8a813 Mon Sep 17 00:00:00 2001 From: Matti Airas Date: Thu, 10 Oct 2024 14:20:51 +0300 Subject: [PATCH] Store and transmit HTTP Server as shared_ptr --- src/sensesp/net/web/app_command_handler.cpp | 7 ++++--- src/sensesp/net/web/app_command_handler.h | 4 ++-- src/sensesp/net/web/base_command_handler.cpp | 10 +++++----- src/sensesp/net/web/base_command_handler.h | 2 +- src/sensesp/net/web/config_handler.cpp | 8 ++++---- src/sensesp/net/web/config_handler.h | 2 +- src/sensesp/net/web/static_file_handler.cpp | 2 +- src/sensesp/net/web/static_file_handler.h | 2 +- 8 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/sensesp/net/web/app_command_handler.cpp b/src/sensesp/net/web/app_command_handler.cpp index 936addd2f..bb2aea332 100644 --- a/src/sensesp/net/web/app_command_handler.cpp +++ b/src/sensesp/net/web/app_command_handler.cpp @@ -1,12 +1,13 @@ #include "app_command_handler.h" +#include #include -#include +#include "sensesp_app.h" namespace sensesp { -void add_scan_wifi_networks_handlers(HTTPServer* server) { +void add_scan_wifi_networks_handlers(std::shared_ptr& server) { auto scan_wifi_networks_handler = std::make_shared( 1 << HTTP_POST, "/api/wifi/scan", [](httpd_req_t* req) { auto networking = SensESPApp::get()->get_networking(); @@ -52,7 +53,7 @@ void add_scan_wifi_networks_handlers(HTTPServer* server) { server->add_handler(scan_results_handler); } -void add_app_http_command_handlers(HTTPServer* server) { +void add_app_http_command_handlers(std::shared_ptr& server) { add_scan_wifi_networks_handlers(server); } diff --git a/src/sensesp/net/web/app_command_handler.h b/src/sensesp/net/web/app_command_handler.h index f68f30ce3..c700c8f64 100644 --- a/src/sensesp/net/web/app_command_handler.h +++ b/src/sensesp/net/web/app_command_handler.h @@ -1,15 +1,15 @@ #ifndef SENSESP_NET_HTTP_APP_COMMAND_HANDLER_H_ #define SENSESP_NET_HTTP_APP_COMMAND_HANDLER_H_ +#include #include #include "ArduinoJson.h" #include "sensesp/net/http_server.h" -#include "sensesp_app.h" namespace sensesp { -void add_app_http_command_handlers(HTTPServer* server); +void add_app_http_command_handlers(std::shared_ptr& server); } // namespace sensesp diff --git a/src/sensesp/net/web/base_command_handler.cpp b/src/sensesp/net/web/base_command_handler.cpp index c3b7ad16d..1c784fe26 100644 --- a/src/sensesp/net/web/base_command_handler.cpp +++ b/src/sensesp/net/web/base_command_handler.cpp @@ -5,7 +5,7 @@ namespace sensesp { -void add_http_reset_handler(HTTPServer* server) { +void add_http_reset_handler(std::shared_ptr& server) { HTTPRequestHandler* reset_handler = new HTTPRequestHandler( 1 << HTTP_POST, "/api/device/reset", [](httpd_req_t* req) { httpd_resp_send(req, @@ -18,7 +18,7 @@ void add_http_reset_handler(HTTPServer* server) { server->add_handler(reset_handler); } -void add_http_restart_handler(HTTPServer* server) { +void add_http_restart_handler(std::shared_ptr& server) { HTTPRequestHandler* restart_handler = new HTTPRequestHandler( 1 << HTTP_POST, "/api/device/restart", [](httpd_req_t* req) { httpd_resp_send(req, "Restarting device", 0); @@ -28,7 +28,7 @@ void add_http_restart_handler(HTTPServer* server) { server->add_handler(restart_handler); } -void add_http_info_handler(HTTPServer* server) { +void add_http_info_handler(std::shared_ptr& server) { HTTPRequestHandler* info_handler = new HTTPRequestHandler(1 << HTTP_GET, "/api/info", [](httpd_req_t* req) { auto status_page_items = StatusPageItemBase::get_status_page_items(); @@ -50,7 +50,7 @@ void add_http_info_handler(HTTPServer* server) { server->add_handler(info_handler); } -void add_routes_handlers(HTTPServer* server) { +void add_routes_handlers(std::shared_ptr& server) { std::vector routes; routes.push_back(RouteDefinition("Status", "/status", "StatusPage")); @@ -114,7 +114,7 @@ void add_routes_handlers(HTTPServer* server) { } } -void add_base_app_http_command_handlers(HTTPServer* server) { +void add_base_app_http_command_handlers(std::shared_ptr& server) { add_http_reset_handler(server); add_http_restart_handler(server); add_http_info_handler(server); diff --git a/src/sensesp/net/web/base_command_handler.h b/src/sensesp/net/web/base_command_handler.h index 721c35a9e..d3198628e 100644 --- a/src/sensesp/net/web/base_command_handler.h +++ b/src/sensesp/net/web/base_command_handler.h @@ -34,7 +34,7 @@ class RouteDefinition { String component_name_; }; -void add_base_app_http_command_handlers(HTTPServer* server); +void add_base_app_http_command_handlers(std::shared_ptr& server); } // namespace sensesp diff --git a/src/sensesp/net/web/config_handler.cpp b/src/sensesp/net/web/config_handler.cpp index 2d487fc30..ae92d0091 100644 --- a/src/sensesp/net/web/config_handler.cpp +++ b/src/sensesp/net/web/config_handler.cpp @@ -79,12 +79,12 @@ esp_err_t handle_config_item_list(httpd_req_t* req) { return ESP_OK; } -void add_config_list_handler(HTTPServer* server) { +void add_config_list_handler(std::shared_ptr& server) { server->add_handler(new HTTPRequestHandler(1 << HTTP_GET, "/api/config", handle_config_item_list)); } -void add_config_get_handler(HTTPServer* server) { +void add_config_get_handler(std::shared_ptr& server) { server->add_handler(new HTTPRequestHandler( 1 << HTTP_GET, "/api/config/*", [](httpd_req_t* req) { ESP_LOGD("ConfigHandler", "GET request to URL %s", req->uri); @@ -126,7 +126,7 @@ void add_config_get_handler(HTTPServer* server) { })); } -void add_config_put_handler(HTTPServer* server) { +void add_config_put_handler(std::shared_ptr& server) { server->add_handler(new HTTPRequestHandler( 1 << HTTP_PUT, "/api/config/*", [](httpd_req_t* req) { // check that the content type is JSON @@ -201,7 +201,7 @@ void add_config_put_handler(HTTPServer* server) { })); } -void add_config_handlers(HTTPServer* server) { +void add_config_handlers(std::shared_ptr& server) { add_config_list_handler(server); add_config_get_handler(server); add_config_put_handler(server); diff --git a/src/sensesp/net/web/config_handler.h b/src/sensesp/net/web/config_handler.h index 490c8c900..43ac38e7e 100644 --- a/src/sensesp/net/web/config_handler.h +++ b/src/sensesp/net/web/config_handler.h @@ -15,7 +15,7 @@ namespace sensesp { * to provide a RESTful API for configuring Configurable objects. * */ -void add_config_handlers(HTTPServer* server); +void add_config_handlers(std::shared_ptr& server); } // namespace sensesp diff --git a/src/sensesp/net/web/static_file_handler.cpp b/src/sensesp/net/web/static_file_handler.cpp index 264e5973b..06c09b749 100644 --- a/src/sensesp/net/web/static_file_handler.cpp +++ b/src/sensesp/net/web/static_file_handler.cpp @@ -4,7 +4,7 @@ namespace sensesp { -void add_static_file_handlers(HTTPServer* server) { +void add_static_file_handlers(std::shared_ptr server) { for (int i = 0; kFrontendFiles[i].url != nullptr; i++) { const StaticFileData& data = kFrontendFiles[i]; HTTPRequestHandler* handler = new HTTPRequestHandler( diff --git a/src/sensesp/net/web/static_file_handler.h b/src/sensesp/net/web/static_file_handler.h index 685549df3..6e11d2370 100644 --- a/src/sensesp/net/web/static_file_handler.h +++ b/src/sensesp/net/web/static_file_handler.h @@ -13,7 +13,7 @@ namespace sensesp { * @brief Provide handlers for static web content. * */ -void add_static_file_handlers(HTTPServer* server); +void add_static_file_handlers(std::shared_ptr server); } // namespace sensesp