feat(routing): integrate new router into framework
This commit is contained in:
parent
d739157fb1
commit
e29e02ff85
9 changed files with 201 additions and 364 deletions
|
|
@ -7,6 +7,8 @@
|
|||
#include "lnm/http/req.h"
|
||||
#include "lnm/http/res.h"
|
||||
|
||||
#define LNM_HTTP_MAX_KEY_SEGMENTS 4
|
||||
|
||||
typedef enum lnm_http_step_err {
|
||||
lnm_http_step_err_done = 0,
|
||||
lnm_http_step_err_io_needed,
|
||||
|
|
@ -22,6 +24,55 @@ typedef void (*lnm_http_ctx_reset_fn)(void *c_ctx);
|
|||
|
||||
typedef void (*lnm_http_ctx_free_fn)(void *c_ctx);
|
||||
|
||||
typedef struct lnm_http_route lnm_http_route;
|
||||
|
||||
typedef struct lnm_http_route_match_segment {
|
||||
size_t start;
|
||||
size_t len;
|
||||
} lnm_http_route_match_segment;
|
||||
|
||||
typedef struct lnm_http_route_match {
|
||||
const lnm_http_route *route;
|
||||
lnm_http_method method;
|
||||
lnm_http_route_match_segment key_segments[LNM_HTTP_MAX_KEY_SEGMENTS];
|
||||
} lnm_http_route_match;
|
||||
|
||||
typedef struct lnm_http_router lnm_http_router;
|
||||
|
||||
typedef enum lnm_http_route_err {
|
||||
lnm_http_route_err_match = 0,
|
||||
lnm_http_route_err_unknown_route = 1,
|
||||
lnm_http_route_err_unknown_method = 2,
|
||||
} lnm_http_route_err;
|
||||
|
||||
/**
|
||||
* Allocate and initialize a new http_router.
|
||||
*/
|
||||
lnm_err lnm_http_router_init(lnm_http_router **out);
|
||||
|
||||
void lnm_http_router_free(lnm_http_router *router);
|
||||
|
||||
lnm_err lnm_http_router_add(lnm_http_route **out, lnm_http_router *http_router,
|
||||
lnm_http_method method, const char *path);
|
||||
|
||||
/**
|
||||
* Add all of the child router's routes to the parent router, under the given
|
||||
* route prefix.
|
||||
*/
|
||||
lnm_err lnm_http_router_nest(lnm_http_router *parent,
|
||||
const lnm_http_router *child, const char *prefix);
|
||||
|
||||
lnm_http_route_err lnm_http_router_route(lnm_http_route_match *out,
|
||||
const lnm_http_router *router,
|
||||
lnm_http_method method,
|
||||
const char *path);
|
||||
|
||||
const lnm_http_route_match_segment *
|
||||
lnm_http_route_match_get(lnm_http_route_match *match, const char *key);
|
||||
|
||||
lnm_err lnm_http_route_step_append(lnm_http_route *route, lnm_http_step_fn fn,
|
||||
bool blocking);
|
||||
|
||||
/**
|
||||
* Initialize a new `lnm_http_loop`.
|
||||
*
|
||||
|
|
@ -32,47 +83,7 @@ lnm_err lnm_http_loop_init(lnm_http_loop **out, void *c_gctx,
|
|||
lnm_http_ctx_reset_fn ctx_reset,
|
||||
lnm_http_ctx_free_fn ctx_free);
|
||||
|
||||
/**
|
||||
* Append the given step fn to the step.
|
||||
*
|
||||
* @param out both the previous step to append the new step to, and the output
|
||||
* variable to which the new step is appended
|
||||
* @param fn step function
|
||||
* @param blocking whether the step is blocking or not
|
||||
*/
|
||||
lnm_err lnm_http_step_append(lnm_http_step **out, lnm_http_step_fn fn,
|
||||
bool blocking);
|
||||
|
||||
/**
|
||||
* Initialize a new route of type literal.
|
||||
*
|
||||
* @param out where to store pointer to new `lnm_http_route`
|
||||
* @param path literal path to match
|
||||
* @param step step to process request with
|
||||
*/
|
||||
lnm_err lnm_http_route_init_literal(lnm_http_route **out,
|
||||
lnm_http_method method, const char *path,
|
||||
lnm_http_step *step);
|
||||
|
||||
/**
|
||||
* Initialize a new route of type regex.
|
||||
*
|
||||
* @param out where to store pointer to new `lnm_http_route`
|
||||
* @param pattern regex pattern
|
||||
* @param regex_group_count how many regex groups are contained in the pattern
|
||||
* @param step step to process request with
|
||||
*/
|
||||
lnm_err lnm_http_route_init_regex(lnm_http_route **out, lnm_http_method method,
|
||||
const char *pattern, int regex_group_count,
|
||||
lnm_http_step *step);
|
||||
|
||||
/**
|
||||
* Add a new route to the HTTP route.
|
||||
*
|
||||
* @param hl HTTP loop to modify
|
||||
* @param route route to add
|
||||
*/
|
||||
lnm_err lnm_http_loop_route_add(lnm_http_loop *hl, lnm_http_route *route);
|
||||
void lnm_http_loop_router_set(lnm_http_loop *hl, lnm_http_router *router);
|
||||
|
||||
lnm_err lnm_http_loop_run(lnm_http_loop *hl, uint16_t port,
|
||||
size_t epoll_threads, size_t worker_threads);
|
||||
|
|
@ -106,10 +117,7 @@ typedef enum lnm_http_loop_state {
|
|||
} lnm_http_loop_state;
|
||||
|
||||
typedef struct lnm_http_loop_gctx {
|
||||
struct {
|
||||
lnm_http_route **arr;
|
||||
size_t len;
|
||||
} routes;
|
||||
lnm_http_router *router;
|
||||
lnm_http_ctx_init_fn ctx_init;
|
||||
lnm_http_ctx_reset_fn ctx_reset;
|
||||
lnm_http_ctx_free_fn ctx_free;
|
||||
|
|
@ -122,7 +130,7 @@ typedef struct lnm_http_loop_ctx {
|
|||
lnm_http_loop_state state;
|
||||
lnm_http_req req;
|
||||
lnm_http_res res;
|
||||
lnm_http_route *route;
|
||||
const lnm_http_route *route;
|
||||
lnm_http_step *cur_step;
|
||||
lnm_http_loop_gctx *g;
|
||||
void *c;
|
||||
|
|
|
|||
|
|
@ -1,55 +0,0 @@
|
|||
#ifndef LNM_HTTP_ROUTER
|
||||
#define LNM_HTTP_ROUTER
|
||||
|
||||
#define LNM_HTTP_MAX_KEY_SEGMENTS 4
|
||||
|
||||
#include "lnm/common.h"
|
||||
#include "lnm/http/consts.h"
|
||||
|
||||
typedef struct lnm_http_route lnm_http_route;
|
||||
|
||||
typedef struct lnm_http_route_match_segment {
|
||||
size_t start;
|
||||
size_t len;
|
||||
} lnm_http_route_match_segment;
|
||||
|
||||
typedef struct lnm_http_route_match {
|
||||
const lnm_http_route *route;
|
||||
lnm_http_method method;
|
||||
lnm_http_route_match_segment key_segments[LNM_HTTP_MAX_KEY_SEGMENTS];
|
||||
} lnm_http_route_match;
|
||||
|
||||
typedef struct lnm_http_router lnm_http_router;
|
||||
|
||||
typedef enum lnm_http_route_err {
|
||||
lnm_http_route_err_match = 0,
|
||||
lnm_http_route_err_unknown_route = 1,
|
||||
lnm_http_route_err_unknown_method = 2,
|
||||
} lnm_http_route_err;
|
||||
|
||||
/**
|
||||
* Allocate and initialize a new http_router.
|
||||
*/
|
||||
lnm_err lnm_http_router_init(lnm_http_router **out);
|
||||
|
||||
void lnm_http_router_free(lnm_http_router *router);
|
||||
|
||||
lnm_err lnm_http_router_add(lnm_http_route **out, lnm_http_router *http_router,
|
||||
lnm_http_method method, const char *path);
|
||||
|
||||
/**
|
||||
* Add all of the child router's routes to the parent router, under the given
|
||||
* route prefix.
|
||||
*/
|
||||
lnm_err lnm_http_router_nest(lnm_http_router *parent,
|
||||
const lnm_http_router *child, const char *prefix);
|
||||
|
||||
lnm_http_route_err lnm_http_router_route(lnm_http_route_match *out,
|
||||
const lnm_http_router *router,
|
||||
lnm_http_method method,
|
||||
const char *path);
|
||||
|
||||
const lnm_http_route_match_segment *
|
||||
lnm_http_route_match_get(lnm_http_route_match *match, const char *key);
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue