add: relay endpoint
fix: discovery invalid read
This commit is contained in:
parent
1171ef22be
commit
a127a68e31
5 changed files with 61 additions and 2 deletions
endpoints
40
endpoints/api_v1_relays.c
Normal file
40
endpoints/api_v1_relays.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include <cJSON.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_relays.h>
|
||||
#include <logger.h>
|
||||
#include <models/junction_tag.h>
|
||||
#include <models/relay.h>
|
||||
#include <models/tag.h>
|
||||
|
||||
void
|
||||
api_v1_relays_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
{
|
||||
(void)args;
|
||||
(void)hm;
|
||||
relay_t** all_relays = relay_get_all();
|
||||
|
||||
cJSON *json = cJSON_CreateArray();
|
||||
|
||||
for(int i = 0; all_relays[i] != NULL; ++i)
|
||||
{
|
||||
cJSON *json_relay = relay_to_json(all_relays[i]);
|
||||
|
||||
cJSON_AddItemToArray(json, json_relay);
|
||||
}
|
||||
|
||||
char *json_str = cJSON_Print(json);
|
||||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print relays json\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
relay_free_list(all_relays);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue