Skip to content

Commit

Permalink
Store and transmit HTTP Server as shared_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
mairas committed Oct 10, 2024
1 parent 0bb262b commit 0f187b3
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 18 deletions.
7 changes: 4 additions & 3 deletions src/sensesp/net/web/app_command_handler.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "app_command_handler.h"

#include <esp_http_server.h>
#include <memory>

#include <esp_http_server.h>
#include "sensesp_app.h"

namespace sensesp {

void add_scan_wifi_networks_handlers(HTTPServer* server) {
void add_scan_wifi_networks_handlers(std::shared_ptr<HTTPServer>& server) {
auto scan_wifi_networks_handler = std::make_shared<HTTPRequestHandler>(
1 << HTTP_POST, "/api/wifi/scan", [](httpd_req_t* req) {
auto networking = SensESPApp::get()->get_networking();
Expand Down Expand Up @@ -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<HTTPServer>& server) {
add_scan_wifi_networks_handlers(server);
}

Expand Down
4 changes: 2 additions & 2 deletions src/sensesp/net/web/app_command_handler.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#ifndef SENSESP_NET_HTTP_APP_COMMAND_HANDLER_H_
#define SENSESP_NET_HTTP_APP_COMMAND_HANDLER_H_

#include <memory>
#include <vector>

#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<HTTPServer>& server);

} // namespace sensesp

Expand Down
10 changes: 5 additions & 5 deletions src/sensesp/net/web/base_command_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace sensesp {

void add_http_reset_handler(HTTPServer* server) {
void add_http_reset_handler(std::shared_ptr<HTTPServer>& server) {
HTTPRequestHandler* reset_handler = new HTTPRequestHandler(
1 << HTTP_POST, "/api/device/reset", [](httpd_req_t* req) {
httpd_resp_send(req,
Expand All @@ -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<HTTPServer>& server) {
HTTPRequestHandler* restart_handler = new HTTPRequestHandler(
1 << HTTP_POST, "/api/device/restart", [](httpd_req_t* req) {
httpd_resp_send(req, "Restarting device", 0);
Expand All @@ -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<HTTPServer>& server) {
HTTPRequestHandler* info_handler =
new HTTPRequestHandler(1 << HTTP_GET, "/api/info", [](httpd_req_t* req) {
auto status_page_items = StatusPageItemBase::get_status_page_items();
Expand All @@ -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<HTTPServer>& server) {
std::vector<RouteDefinition> routes;

routes.push_back(RouteDefinition("Status", "/status", "StatusPage"));
Expand Down Expand Up @@ -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<HTTPServer>& server) {
add_http_reset_handler(server);
add_http_restart_handler(server);
add_http_info_handler(server);
Expand Down
2 changes: 1 addition & 1 deletion src/sensesp/net/web/base_command_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTTPServer>& server);

} // namespace sensesp

Expand Down
8 changes: 4 additions & 4 deletions src/sensesp/net/web/config_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTTPServer>& 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<HTTPServer>& 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);
Expand Down Expand Up @@ -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<HTTPServer>& server) {
server->add_handler(new HTTPRequestHandler(
1 << HTTP_PUT, "/api/config/*",
[](httpd_req_t* req) { // check that the content type is JSON
Expand Down Expand Up @@ -201,7 +201,7 @@ void add_config_put_handler(HTTPServer* server) {
}));
}

void add_config_handlers(HTTPServer* server) {
void add_config_handlers(std::shared_ptr<HTTPServer>& server) {
add_config_list_handler(server);
add_config_get_handler(server);
add_config_put_handler(server);
Expand Down
2 changes: 1 addition & 1 deletion src/sensesp/net/web/config_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTTPServer>& server);

} // namespace sensesp

Expand Down
2 changes: 1 addition & 1 deletion src/sensesp/net/web/static_file_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace sensesp {

void add_static_file_handlers(HTTPServer* server) {
void add_static_file_handlers(std::shared_ptr<HTTPServer> server) {
for (int i = 0; kFrontendFiles[i].url != nullptr; i++) {
const StaticFileData& data = kFrontendFiles[i];
HTTPRequestHandler* handler = new HTTPRequestHandler(
Expand Down
2 changes: 1 addition & 1 deletion src/sensesp/net/web/static_file_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTTPServer> server);

} // namespace sensesp

Expand Down

0 comments on commit 0f187b3

Please sign in to comment.