add: generic response makers

This commit is contained in:
Tobias Reisinger 2020-05-31 00:23:57 +02:00
parent 2d992cfe3c
commit 6178ef3c7b
13 changed files with 200 additions and 503 deletions

62
include/endpoint.h Normal file
View file

@ -0,0 +1,62 @@
#ifndef CORE_ENDPOINT_H
#define CORE_ENDPOINT_H
#include <cJSON.h>
typedef enum
{
ENDPOINT_ARG_TYPE_INT,
ENDPOINT_ARG_TYPE_STR
} endpoint_arg_type_e;
typedef struct
{
endpoint_arg_type_e type;
union
{
int v_int;
const char *v_str;
} value;
} endpoint_args_t;
typedef struct
{
int status_code;
const char *content_type;
size_t content_length;
const char *content;
int alloced_content;
} endpoint_response_t;
typedef void (*endpoint_func_f)(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
typedef struct
{
const char *full_route;
char **route;
char *route_keeper;
int method;
int options;
endpoint_func_f func;
int trailing_slash;
int args_count;
endpoint_args_t *args;
int possible_route;
int args_found;
} endpoint_t;
void
endpoint_func_index(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
void
endpoint_func_not_found(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
void
endpoint_response_text(endpoint_response_t *response, int status_code, const char *content, int content_length);
void
endpoint_response_json(endpoint_response_t *response, int status_code, const cJSON *json_root);
#endif /* CORE_ENDPOINT_H */

View file

@ -2,14 +2,9 @@
#define CORE_ROUTER_H
#include <mongoose.h>
#include <endpoint.h>
#define ENDPOINTS_MAX_COUNT 128
typedef enum
{
ENDPOINT_ARG_TYPE_INT,
ENDPOINT_ARG_TYPE_STR
} endpoint_arg_type_e;
#define ROUTER_ENDPOINTS_MAX_COUNT 128
typedef enum
{
@ -20,44 +15,6 @@ typedef enum
HTTP_METHOD_OPTIONS = (1 << 4)
} http_method_e;
typedef struct
{
endpoint_arg_type_e type;
union
{
int v_int;
const char *v_str;
} value;
} endpoint_args_t;
typedef struct
{
int status_code;
const char *content_type;
size_t content_length;
const char *content;
int alloced_content;
} endpoint_response_t;
typedef void (*endpoint_func_f)(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
typedef struct
{
const char *full_route;
char **route;
char *route_keeper;
int method;
int options;
endpoint_func_f func;
int trailing_slash;
int args_count;
endpoint_args_t *args;
int possible_route;
int args_found;
} endpoint_t;
void
router_init();