create separate folder for arduino and espidf
This commit is contained in:
parent
bc3989f111
commit
a5e0f81930
22 changed files with 0 additions and 0 deletions
|
@ -1,136 +0,0 @@
|
|||
#include "network/theWebServer.h"
|
||||
|
||||
static const char *TAG = "WebServer";
|
||||
static char *server_ip_ptr = NULL;
|
||||
static httpd_config_t *server_config_ptr = NULL;
|
||||
static struct route *head = NULL;
|
||||
static int amount_of_routes = 1;
|
||||
|
||||
struct route
|
||||
{
|
||||
const char *uri;
|
||||
const char *method;
|
||||
int key;
|
||||
struct route *next;
|
||||
};
|
||||
|
||||
static char *convert_method_to_string(httpd_method_t method)
|
||||
{
|
||||
switch (method)
|
||||
{
|
||||
case HTTP_GET:
|
||||
return "GET";
|
||||
break;
|
||||
case HTTP_POST:
|
||||
return "POST";
|
||||
break;
|
||||
case HTTP_PUT:
|
||||
return "PUT";
|
||||
break;
|
||||
case HTTP_DELETE:
|
||||
return "DELETE";
|
||||
break;
|
||||
default:
|
||||
return "not configured";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void print_routes_linked_list()
|
||||
{
|
||||
struct route *ptr = head;
|
||||
printf("\n");
|
||||
ESP_LOGW(TAG, "Available Routes:");
|
||||
while (ptr != NULL)
|
||||
{
|
||||
ESP_LOGW(TAG, "%2d: %-8s-> http://%s:%d%s", ptr->key, ptr->method, server_ip_ptr, server_config_ptr->server_port, ptr->uri);
|
||||
ptr = ptr->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void add_route_to_linked_list(const httpd_uri_t *uri_handler)
|
||||
{
|
||||
struct route *temp;
|
||||
struct route *link = (struct route *)malloc(sizeof(struct route));
|
||||
link->key = amount_of_routes;
|
||||
amount_of_routes++;
|
||||
link->uri = uri_handler->uri;
|
||||
link->method = convert_method_to_string(uri_handler->method);
|
||||
if (head == NULL)
|
||||
{
|
||||
head = link;
|
||||
link->next = NULL;
|
||||
return;
|
||||
}
|
||||
temp = head;
|
||||
while (temp->next != NULL)
|
||||
{
|
||||
temp = temp->next;
|
||||
}
|
||||
temp->next = link;
|
||||
link->next = NULL;
|
||||
}
|
||||
|
||||
void custom_request_middleware(httpd_req_t *req)
|
||||
{
|
||||
ESP_LOGI(TAG, "%s: %s", convert_method_to_string(req->method), req->uri);
|
||||
httpd_resp_set_type(req, HTTPD_TYPE_JSON);
|
||||
}
|
||||
|
||||
void custom_json_response(httpd_req_t *req, char *message, char *type)
|
||||
{
|
||||
httpd_resp_set_status(req, type);
|
||||
cJSON *json_response = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(json_response, "status", message);
|
||||
httpd_resp_sendstr(req, cJSON_Print(json_response));
|
||||
cJSON_Delete(json_response);
|
||||
}
|
||||
|
||||
static void register_request_helper(httpd_handle_t *server, const httpd_uri_t *uri_handler)
|
||||
{
|
||||
add_route_to_linked_list(uri_handler);
|
||||
httpd_register_uri_handler(server, uri_handler);
|
||||
}
|
||||
|
||||
static void register_all_request_handlers(httpd_handle_t *server)
|
||||
{
|
||||
// FIREWALL ROUTES
|
||||
httpd_uri_t firewall_get_uri = {.uri = "/api/v1/firewall", .method = HTTP_GET, .handler = firewall_get_handler};
|
||||
register_request_helper(server, &firewall_get_uri);
|
||||
httpd_uri_t firewall_post_uri = {.uri = "/api/v1/firewall", .method = HTTP_POST, .handler = firewall_post_handler};
|
||||
register_request_helper(server, &firewall_post_uri);
|
||||
httpd_uri_t firewall_put_uri = {.uri = "/api/v1/firewall", .method = HTTP_PUT, .handler = firewall_get_handler};
|
||||
register_request_helper(server, &firewall_put_uri);
|
||||
httpd_uri_t firewall_delete_uri = {.uri = "/api/v1/firewall", .method = HTTP_DELETE, .handler = firewall_get_handler};
|
||||
register_request_helper(server, &firewall_delete_uri);
|
||||
|
||||
// GPIO ROUTES
|
||||
httpd_uri_t gpio_put_uri = {.uri = "/api/v1/gpio", .method = HTTP_POST, .handler = relay_put_handler};
|
||||
register_request_helper(server, &gpio_put_uri);
|
||||
|
||||
print_routes_linked_list();
|
||||
}
|
||||
|
||||
static esp_err_t start_web_server()
|
||||
{
|
||||
httpd_handle_t server = NULL;
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.uri_match_fn = httpd_uri_match_wildcard;
|
||||
config.lru_purge_enable = true;
|
||||
config.server_port = 8080;
|
||||
server_config_ptr = &config;
|
||||
|
||||
if (httpd_start(&server, &config) == ESP_OK)
|
||||
{
|
||||
register_all_request_handlers(server);
|
||||
return ESP_OK;
|
||||
}
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
void setup_web_server(char *server_ip)
|
||||
{
|
||||
server_ip_ptr = server_ip;
|
||||
ESP_ERROR_CHECK(start_web_server());
|
||||
}
|
Reference in a new issue