add: endpoint responses

This commit is contained in:
Tobias Reisinger 2020-05-21 01:33:18 +02:00
parent 5a19e99627
commit 5e64f5963c
18 changed files with 587 additions and 235 deletions

View file

@ -1,4 +1,5 @@
#include <cJSON.h>
#include <macros.h>
#include <constants.h>
#include <endpoints/api_v1_controllers.h>
#include <models/controller.h>
@ -104,15 +105,20 @@ send_udp_broadcast(const char *addr, uint16_t port, void *message, size_t length
}
void
api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
api_v1_controllers_discover_POST(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
int discover_server_socket = bind_tcp_server("0.0.0.0", "0", 20);
int discover_server_port = get_server_port(discover_server_socket);
if(discover_server_port == -1)
{
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_printf(c, "[]");
LOG_ERROR("failed to get server port for discovery\n");
static const char content[] = "";
response->status_code = 500;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
return;
}
@ -121,8 +127,13 @@ api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args,
if(send_udp_broadcast("255.255.255.255", global_config.discovery_port, payload, sizeof(payload)) < 0)
{
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_printf(c, "[]");
LOG_ERROR("failed to send UDP broadcast\n");
static const char content[] = "";
response->status_code = 500;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
return;
}
@ -292,5 +303,5 @@ api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args,
}
controller_free_list(known_controllers);
api_v1_controllers_GET(c, args, hm);
api_v1_controllers_GET(hm, args, response);
}