#ifndef ESP32_API_HPP #define ESP32_API_HPP #ifdef ESP32 #include "WebServer.h" #elif defined(ESP8266) #include "ESP8266WebServerSecure.h" #endif #include "uri/UriRegex.h" #include "Rules.hpp" #include "Utils.hpp" namespace fw { typedef struct api_endpoints { char uri[40]; char method[7]; char description[30]; struct api_endpoints *next; } api_endpoint_t; class API : public Rules { private: #ifdef ESP32 WebServer *server; #elif defined(ESP8266) BearSSL::ESP8266WebServerSecure *server; BearSSL::ServerSessions *serverCache; #endif credential_t credentials; api_endpoint_t *endpoint_head = NULL; ok_t setup_auth(const char *username, const char *password); auth_t check_auth(); void setup_routing(const char *cert, const char *key); void add_api_endpoint(const String uri, const char *method, const char *description); void get_firewall_rule_handler(); void get_firewall_rules_handler(); void post_firewall_handler(); void delete_firewall_handler(); void not_found_handler(); bool request_has_firewall_parameter(); String json_new_attribute(String key, String value, bool last = false); String json_new_attribute(String key, uint8_t value, bool last = false); void json_generic_response(String serialized_string, const uint16_t response_code); void json_message_response(String message, const uint16_t response_code); String construct_json_firewall_rule(firewall_rule_t *); String construct_json_firewall(); String construct_json_api_endpoint(api_endpoint_t *); String construct_json_api(const uint16_t response_code); String construct_json_array(String message, String array_name); protected: String server_ip; uint16_t server_port; void handle_client(); public: API(const char *cert, const char *key, const char *username, const char *password, const String ip, const uint16_t port); ~API(); }; } #endif