2020-05-05 09:42:02 +00:00
|
|
|
#ifndef CORE_ROUTER_H
|
|
|
|
#define CORE_ROUTER_H
|
|
|
|
|
|
|
|
#include <mongoose.h>
|
|
|
|
|
2020-05-13 23:37:08 +00:00
|
|
|
#define ENDPOINTS_MAX_COUNT 128
|
2020-05-05 09:42:02 +00:00
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
ENDPOINT_ARG_TYPE_INT,
|
|
|
|
ENDPOINT_ARG_TYPE_STR
|
|
|
|
} endpoint_arg_type_e;
|
|
|
|
|
2020-05-13 23:37:08 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
HTTP_METHOD_GET = (1 << 0),
|
|
|
|
HTTP_METHOD_POST = (1 << 1),
|
|
|
|
HTTP_METHOD_PUT = (1 << 2),
|
|
|
|
HTTP_METHOD_DELETE = (1 << 3),
|
|
|
|
HTTP_METHOD_OPTIONS = (1 << 4)
|
|
|
|
} http_method_e;
|
|
|
|
|
2020-05-05 09:42:02 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
endpoint_arg_type_e type;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
int v_int;
|
2020-05-06 08:53:42 +00:00
|
|
|
const char *v_str;
|
2020-05-05 09:42:02 +00:00
|
|
|
} value;
|
|
|
|
} endpoint_args_t;
|
|
|
|
|
2020-05-20 23:33:18 +00:00
|
|
|
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);
|
2020-05-05 09:42:02 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2020-05-13 23:37:08 +00:00
|
|
|
const char *full_route;
|
2020-05-05 09:42:02 +00:00
|
|
|
char **route;
|
|
|
|
char *route_keeper;
|
2020-05-13 23:37:08 +00:00
|
|
|
int method;
|
|
|
|
int options;
|
2020-05-05 09:42:02 +00:00
|
|
|
endpoint_func_f func;
|
2020-05-13 23:37:08 +00:00
|
|
|
int trailing_slash;
|
2020-05-05 09:42:02 +00:00
|
|
|
|
|
|
|
int args_count;
|
|
|
|
endpoint_args_t *args;
|
|
|
|
|
|
|
|
int possible_route;
|
|
|
|
int args_found;
|
|
|
|
} endpoint_t;
|
|
|
|
|
|
|
|
void
|
|
|
|
router_init();
|
|
|
|
|
2020-05-13 23:37:08 +00:00
|
|
|
endpoint_t*
|
|
|
|
router_register_endpoint(const char *route, int method, endpoint_func_f func);
|
2020-05-05 09:42:02 +00:00
|
|
|
|
|
|
|
endpoint_t*
|
2020-05-13 23:37:08 +00:00
|
|
|
router_find_endpoint(const char *uri_str, size_t uri_len, struct mg_str *method_str);
|
2020-05-05 09:42:02 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
router_free();
|
|
|
|
|
|
|
|
#endif /* CORE_ROUTER_H */
|