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;
|
||||
|
|
Reference in a new issue