show endpoints when route not found
This commit is contained in:
parent
e012c82e16
commit
a40b81b45d
5 changed files with 96 additions and 23 deletions
|
@ -63,16 +63,49 @@ namespace fw
|
|||
this->server->getServer().setRSACert(new BearSSL::X509List(cert), new BearSSL::PrivateKey(key));
|
||||
this->server->getServer().setCache(serverCache);
|
||||
#endif
|
||||
this->server->on(UriRegex("/api/v1/firewall/([0-9]+)"), HTTP_GET, std::bind(&API::get_firewall_rule_handler, this));
|
||||
this->server->on("/api/v1/firewall", HTTP_GET, std::bind(&API::get_firewall_rules_handler, this));
|
||||
this->server->on("/api/v1/firewall", HTTP_POST, std::bind(&API::post_firewall_handler, this));
|
||||
this->server->on(UriRegex("/api/v1/firewall/([0-9]+)"), HTTP_DELETE, std::bind(&API::delete_firewall_handler, this));
|
||||
this->server->on("/firewall", HTTP_GET, std::bind(&API::get_firewall_rules_handler, this));
|
||||
add_api_endpoint("/firewall", "GET", "Get all Firewall Rules");
|
||||
|
||||
this->server->on(UriRegex("/firewall/([0-9]+)"), HTTP_GET, std::bind(&API::get_firewall_rule_handler, this));
|
||||
add_api_endpoint("/firewall/key", "GET", "Get Firewall Rule by key");
|
||||
|
||||
this->server->on("/firewall", HTTP_POST, std::bind(&API::post_firewall_handler, this));
|
||||
add_api_endpoint("/firewall", "POST", "Create Firewall Rule");
|
||||
|
||||
this->server->on(UriRegex("/firewall/([0-9]+)"), HTTP_DELETE, std::bind(&API::delete_firewall_handler, this));
|
||||
add_api_endpoint("/firewall/key", "DELETE", "Delete Firewall Rule by key");
|
||||
|
||||
this->server->onNotFound(std::bind(&API::not_found_handler, this));
|
||||
}
|
||||
|
||||
void API::add_api_endpoint(const char *uri, const char *method, const char *description)
|
||||
{
|
||||
api_endpoint_t *temp;
|
||||
|
||||
api_endpoint_t *api_ptr = (api_endpoint_t *)malloc(sizeof(api_endpoint_t));
|
||||
strncpy(api_ptr->uri, uri, sizeof(api_ptr->uri));
|
||||
strncpy(api_ptr->method, method, sizeof(api_ptr->method));
|
||||
strncpy(api_ptr->description, description, sizeof(api_ptr->description));
|
||||
|
||||
if (this->endpoint_head == NULL)
|
||||
{
|
||||
this->endpoint_head = api_ptr;
|
||||
api_ptr->next = NULL;
|
||||
return;
|
||||
}
|
||||
temp = this->endpoint_head;
|
||||
while (temp->next != NULL)
|
||||
{
|
||||
temp = temp->next;
|
||||
}
|
||||
temp->next = api_ptr;
|
||||
api_ptr->next = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
void API::not_found_handler()
|
||||
{
|
||||
this->json_message_response("not found", 404);
|
||||
this->json_generic_response(this->construct_json_endpoints(), 404);
|
||||
}
|
||||
|
||||
void API::get_firewall_rule_handler()
|
||||
|
@ -199,15 +232,43 @@ namespace fw
|
|||
|
||||
String API::construct_json_firewall()
|
||||
{
|
||||
firewall_rule_t *rule_ptr = head;
|
||||
firewall_rule_t *rule_ptr = rule_head;
|
||||
String serialized_string = "{";
|
||||
serialized_string += json_new_attribute("amount_of_rules", amount_of_rules);
|
||||
serialized_string += "\"rules\": [";
|
||||
while (rule_ptr != NULL)
|
||||
{
|
||||
serialized_string += construct_json_firewall_rule(rule_ptr);
|
||||
serialized_string += ",";
|
||||
rule_ptr = rule_ptr->next;
|
||||
if (rule_ptr != NULL)
|
||||
serialized_string += ",";
|
||||
}
|
||||
serialized_string += "]}";
|
||||
return serialized_string;
|
||||
}
|
||||
|
||||
String API::construct_json_endpoint(api_endpoint_t *api_ptr)
|
||||
{
|
||||
String serialized_string = "{";
|
||||
serialized_string += json_new_attribute("endpoint", api_ptr->uri);
|
||||
serialized_string += json_new_attribute("description", api_ptr->description);
|
||||
serialized_string += json_new_attribute("method", api_ptr->method, true);
|
||||
serialized_string += "}";
|
||||
return serialized_string;
|
||||
}
|
||||
|
||||
String API::construct_json_endpoints()
|
||||
{
|
||||
api_endpoint_t *api_ptr = this->endpoint_head;
|
||||
String serialized_string = "{";
|
||||
serialized_string += json_new_attribute("message", "route not found");
|
||||
serialized_string += "\"endpoints\": [";
|
||||
while (api_ptr != NULL)
|
||||
{
|
||||
serialized_string += construct_json_endpoint(api_ptr);
|
||||
api_ptr = api_ptr->next;
|
||||
if (api_ptr != NULL)
|
||||
serialized_string += ",";
|
||||
}
|
||||
serialized_string += "]}";
|
||||
return serialized_string;
|
||||
|
|
|
@ -14,6 +14,14 @@
|
|||
|
||||
namespace fw
|
||||
{
|
||||
typedef struct api_endpoints
|
||||
{
|
||||
char uri[20];
|
||||
char method[7];
|
||||
char description[30];
|
||||
struct api_endpoints *next;
|
||||
} api_endpoint_t;
|
||||
|
||||
class API : public Rules
|
||||
{
|
||||
private:
|
||||
|
@ -24,11 +32,13 @@ namespace fw
|
|||
BearSSL::ServerSessions *serverCache;
|
||||
#endif
|
||||
credential_t credentials;
|
||||
api_endpoint_t *endpoint_head = NULL;
|
||||
|
||||
ok_t setup_auth(const char *, const char *);
|
||||
ok_t setup_auth(const char *username, const char *password);
|
||||
auth_t check_auth();
|
||||
|
||||
void setup_routing(const char *, const char *);
|
||||
void setup_routing(const char *cert, const char *key);
|
||||
void add_api_endpoint(const char *uri, const char *method, const char *description);
|
||||
void get_firewall_rule_handler();
|
||||
void get_firewall_rules_handler();
|
||||
void post_firewall_handler();
|
||||
|
@ -42,6 +52,8 @@ namespace fw
|
|||
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_endpoint(api_endpoint_t *);
|
||||
String construct_json_endpoints();
|
||||
|
||||
protected:
|
||||
void handle_client();
|
||||
|
|
|
@ -23,13 +23,13 @@ namespace fw
|
|||
store_settings_value("amount_of_rules", this->amount_of_rules);
|
||||
store_firewall_rule(rule_ptr);
|
||||
firewall_rule_t *temp;
|
||||
if (this->head == NULL)
|
||||
if (this->rule_head == NULL)
|
||||
{
|
||||
this->head = rule_ptr;
|
||||
this->rule_head = rule_ptr;
|
||||
rule_ptr->next = NULL;
|
||||
return;
|
||||
}
|
||||
temp = this->head;
|
||||
temp = this->rule_head;
|
||||
while (temp->next != NULL)
|
||||
{
|
||||
temp = temp->next;
|
||||
|
@ -41,8 +41,8 @@ namespace fw
|
|||
|
||||
firewall_rule_t *Rules::get_rule_from_firewall(uint8_t key)
|
||||
{
|
||||
firewall_rule_t *rule_ptr = this->head;
|
||||
if (this->head == NULL)
|
||||
firewall_rule_t *rule_ptr = this->rule_head;
|
||||
if (this->rule_head == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
@ -62,9 +62,9 @@ namespace fw
|
|||
|
||||
ok_t Rules::delete_rule_from_firewall(uint8_t key)
|
||||
{
|
||||
if (this->head == NULL)
|
||||
if (this->rule_head == NULL)
|
||||
return NO_ACTION;
|
||||
firewall_rule_t *current_rule_ptr = this->head;
|
||||
firewall_rule_t *current_rule_ptr = this->rule_head;
|
||||
firewall_rule_t *previous_rule_ptr = NULL;
|
||||
firewall_rule_t *temp = NULL;
|
||||
while (current_rule_ptr->key != key)
|
||||
|
@ -77,10 +77,10 @@ namespace fw
|
|||
current_rule_ptr = current_rule_ptr->next;
|
||||
}
|
||||
}
|
||||
if (current_rule_ptr == this->head)
|
||||
if (current_rule_ptr == this->rule_head)
|
||||
{
|
||||
this->head = head->next;
|
||||
temp = this->head;
|
||||
this->rule_head = rule_head->next;
|
||||
temp = this->rule_head;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -96,7 +96,7 @@ namespace fw
|
|||
this->amount_of_rules--;
|
||||
store_settings_value("amount_of_rules", this->amount_of_rules);
|
||||
if (this->amount_of_rules != 0)
|
||||
store_all_firewall_rules(head);
|
||||
store_all_firewall_rules(rule_head);
|
||||
return SUCCESS;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ namespace fw
|
|||
{
|
||||
protected:
|
||||
uint8_t amount_of_rules = 0;
|
||||
firewall_rule_t *head = NULL;
|
||||
firewall_rule_t *rule_head = NULL;
|
||||
|
||||
void add_rule_to_firewall(firewall_rule_t *);
|
||||
firewall_rule_t *get_rule_from_firewall(uint8_t);
|
||||
|
|
|
@ -132,10 +132,10 @@ namespace fw
|
|||
return rule_ptr;
|
||||
}
|
||||
|
||||
void Storage::store_all_firewall_rules(firewall_rule_t *head)
|
||||
void Storage::store_all_firewall_rules(firewall_rule_t *rule_head)
|
||||
{
|
||||
#ifdef ESP32
|
||||
firewall_rule_t *temp = head;
|
||||
firewall_rule_t *temp = rule_head;
|
||||
while (temp != NULL)
|
||||
{
|
||||
store_firewall_rule(temp);
|
||||
|
|
Reference in a new issue