core-legacy/endpoint.c

88 lines
2.3 KiB
C
Raw Normal View History

2020-05-30 22:23:57 +00:00
#include <logger.h>
#include <cJSON.h>
#include <mongoose.h>
#include <macros.h>
#include <endpoint.h>
void
2020-05-31 00:07:25 +00:00
endpoint_func_index(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
2020-05-30 22:23:57 +00:00
{
(void)args;
(void)hm;
2020-05-31 00:07:25 +00:00
(void)nc;
2020-05-30 22:23:57 +00:00
static const char content[] = "Emgauwa";
response->status_code = 200;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
2020-05-31 00:07:25 +00:00
mg_serve_http(nc, hm, global_config.http_server_opts);
2020-05-30 22:23:57 +00:00
}
void
2020-05-31 00:07:25 +00:00
endpoint_func_not_found(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
2020-05-30 22:23:57 +00:00
{
(void)args;
(void)hm;
2020-05-31 00:07:25 +00:00
(void)nc;
2020-05-30 22:23:57 +00:00
static const char content[] = "404 - NOT FOUND";
response->status_code = 404;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
2020-05-31 00:07:25 +00:00
mg_serve_http(nc, hm, global_config.http_server_opts);
2020-05-30 22:23:57 +00:00
}
void
endpoint_response_text(endpoint_response_t *response, int status_code, const char *content, int content_length)
{
if(content == NULL)
{
content = "";
content_length = 0;
}
response->status_code = status_code;
response->content_type = "text/plain";
if(content_length >= 0)
{
response->content_length = content_length;
response->alloced_content = false;
}
else
{
response->content_length = strlen(content);
response->alloced_content = true;
}
response->content = content;
}
void
endpoint_response_json(endpoint_response_t *response, int status_code, const cJSON *json_root)
{
if(json_root != NULL)
{
char *json_str = cJSON_Print(json_root);
if (json_str != NULL)
{
response->status_code = status_code;
response->content_type = "application/json";
response->content_length = strlen(json_str);
response->content = json_str;
response->alloced_content = true;
return;
}
}
LOG_ERROR("failed to print schedule json\n");
static const char content[] = "failed to print json";
endpoint_response_text(response, status_code, content, STRLEN(content));
}