create seperate api class

This commit is contained in:
Florian Hoss 2022-04-20 07:58:06 +02:00
parent 1a2b495ff7
commit b2c2a967d9
5 changed files with 114 additions and 40 deletions

View file

@ -1,22 +1,8 @@
#include "esp32Firewall.hpp"
esp32Firewall::esp32Firewall(const uint16_t api_port)
esp32Firewall::esp32Firewall()
{
this->setup_eeprom();
this->setup_certificate();
this->firewall_api = new HTTPSServer(this->certificate, api_port, 5);
this->setup_routing();
log_i("Starting server...");
this->firewall_api->start();
if (this->firewall_api->isRunning())
{
log_i("Server ready.");
}
}
void esp32Firewall::handle_firewall_api_clients()
{
this->firewall_api->loop();
}
String esp32Firewall::protocol_to_string(firewall_protocol_t &protocol)
@ -242,14 +228,10 @@ void esp32Firewall::setup_routing()
ResourceNode *get_firewall_rules = new ResourceNode("/api/v1/firewall", "GET", std::bind(&esp32Firewall::get_firewall_rules_handler, this, std::placeholders::_1, std::placeholders::_2));
ResourceNode *post_firewall = new ResourceNode("/api/v1/firewall", "POST", std::bind(&esp32Firewall::post_firewall_handler, this, std::placeholders::_1, std::placeholders::_2));
ResourceNode *delete_firewall = new ResourceNode("/api/v1/firewall/*", "DELETE", std::bind(&esp32Firewall::delete_firewall_handler, this, std::placeholders::_1, std::placeholders::_2));
ResourceNode *restart_device = new ResourceNode("/api/v1/device/restart", "GET", std::bind(&esp32Firewall::restart_device_handler, this, std::placeholders::_1, std::placeholders::_2));
ResourceNode *not_found = new ResourceNode("", "GET", std::bind(&esp32Firewall::not_found_handler, this, std::placeholders::_1, std::placeholders::_2));
this->firewall_api->registerNode(get_firewall_rule);
this->firewall_api->registerNode(get_firewall_rules);
this->firewall_api->registerNode(post_firewall);
this->firewall_api->registerNode(delete_firewall);
this->firewall_api->setDefaultNode(restart_device);
this->firewall_api->setDefaultNode(not_found);
}
void esp32Firewall::json_generic_response(HTTPResponse *response, String serialized, const uint16_t response_code)
@ -305,18 +287,6 @@ String esp32Firewall::construct_json_firewall()
return response;
}
void esp32Firewall::not_found_handler(HTTPRequest *request, HTTPResponse *response)
{
this->json_message_response(response, "not found", 404);
}
void esp32Firewall::restart_device_handler(HTTPRequest *request, HTTPResponse *response)
{
this->json_message_response(response, "restarting device in 2 sec", 200);
sleep(2000);
esp_restart();
}
void esp32Firewall::get_firewall_rule_handler(HTTPRequest *request, HTTPResponse *response)
{
ResourceParameters *params = request->getParams();

View file

@ -52,8 +52,6 @@ class esp32Firewall
void json_message_response(HTTPResponse *, String, const uint16_t);
String construct_json_firewall_rule(firewall_rule_t *);
String construct_json_firewall();
void not_found_handler(HTTPRequest *, HTTPResponse *);
void restart_device_handler(HTTPRequest *, HTTPResponse *);
void get_firewall_rule_handler(HTTPRequest *, HTTPResponse *);
void get_firewall_rules_handler(HTTPRequest *, HTTPResponse *);
bool request_has_firewall_parameter(ResourceParameters *);
@ -61,8 +59,7 @@ class esp32Firewall
void delete_firewall_handler(HTTPRequest *, HTTPResponse *);
public:
esp32Firewall(const uint16_t = 8080);
void handle_firewall_api_clients();
esp32Firewall();
};
#endif

View file

@ -0,0 +1,76 @@
#include "esp32FirewallAPI.hpp"
esp32FirewallApi::esp32FirewallApi(const uint16_t port)
{
this->setup_certificate();
this->server = new HTTPSServer(this->certificate, port, 5);
this->setup_routing();
log_i("Starting server...");
this->server->start();
if (this->server->isRunning())
{
log_i("Server ready.");
}
}
void esp32FirewallApi::handle_clients()
{
this->server->loop();
}
void esp32FirewallApi::setup_certificate()
{
log_i("Creating the certificate...");
this->certificate = new SSLCert();
int createCertResult = createSelfSignedCert(
*this->certificate,
KEYSIZE_2048,
"CN=myesp32.local,O=Firewall,C=DE",
"20220101000000",
"20320101000000");
if (createCertResult != 0)
{
log_e("Cerating certificate failed. Error Code = 0x%02X, check SSLCert.hpp for details", createCertResult);
while (true)
delay(500);
}
log_i("Creating the certificate was successful");
}
void esp32FirewallApi::setup_routing()
{
ResourceNode *restart_device = new ResourceNode("/api/v1/device/restart", "GET", std::bind(&esp32FirewallApi::restart_device_handler, this, std::placeholders::_1, std::placeholders::_2));
ResourceNode *not_found = new ResourceNode("", "GET", std::bind(&esp32FirewallApi::not_found_handler, this, std::placeholders::_1, std::placeholders::_2));
this->server->registerNode(restart_device);
this->server->setDefaultNode(not_found);
}
void esp32FirewallApi::restart_device_handler(HTTPRequest *request, HTTPResponse *response)
{
this->json_message_response(response, "restarting device in 2 sec", 200);
sleep(2000);
esp_restart();
}
void esp32FirewallApi::not_found_handler(HTTPRequest *request, HTTPResponse *response)
{
this->json_message_response(response, "not found", 404);
}
void esp32FirewallApi::json_generic_response(HTTPResponse *response, String serialized, const uint16_t response_code)
{
response->setHeader("Content-Type", "application/json");
response->setStatusCode(response_code);
response->println(serialized);
}
void esp32FirewallApi::json_message_response(HTTPResponse *response, String message, const uint16_t response_code)
{
response->setHeader("Content-Type", "application/json");
response->setStatusCode(response_code);
StaticJsonDocument<96> json;
String serialized;
json["message"] = message;
serializeJson(json, serialized);
response->println(serialized);
}

View file

@ -0,0 +1,29 @@
#ifndef ESP32_FIREWALL_API_HPP
#define ESP32_FIREWALL_API_HPP
#include "HTTPSServer.hpp"
#include "SSLCert.hpp"
#include "HTTPRequest.hpp"
#include "HTTPResponse.hpp"
#include "ArduinoJson.h"
using namespace httpsserver;
class esp32FirewallApi
{
HTTPSServer *server;
SSLCert *certificate;
void setup_certificate();
void setup_routing();
void restart_device_handler(HTTPRequest *, HTTPResponse *);
void not_found_handler(HTTPRequest *, HTTPResponse *);
public:
esp32FirewallApi(const uint16_t = 8080);
void handle_clients();
void json_generic_response(HTTPResponse *, String, const uint16_t);
void json_message_response(HTTPResponse *, String, const uint16_t);
};
#endif

View file

@ -1,8 +1,10 @@
#include "theSecrets.h"
#include "WiFi.h"
#include "esp32Firewall.hpp"
const char *esp_ip_address;
#include "esp32Firewall.hpp"
#include "esp32FirewallAPI.hpp"
esp32FirewallApi *firewall_api;
esp32Firewall *firewall;
void setup_wifi()
@ -17,17 +19,17 @@ void setup_wifi()
delay(2000);
log_d("Connecting... (%i/%i)", retries++, max_retries);
}
esp_ip_address = WiFi.localIP().toString().c_str();
log_i("Connected, IP Address: %s", esp_ip_address);
log_i("Connected, IP Address: %s", WiFi.localIP().toString());
}
void setup()
{
setup_wifi();
firewall = new esp32Firewall;
firewall_api = new esp32FirewallApi;
}
void loop()
{
firewall->handle_firewall_api_clients();
firewall_api->handle_clients();
}