feat(http): fully decouple HTTP loop functionality
This commit is contained in:
parent
fc4187e6ce
commit
6d74c8c550
9 changed files with 99 additions and 21 deletions
|
|
@ -46,18 +46,32 @@ bool http_loop_handle_request(event_loop_conn *conn) {
|
|||
return conn->state == event_loop_conn_state_req;
|
||||
}
|
||||
|
||||
event_loop *http_loop_init(http_loop_gctx *gctx) {
|
||||
event_loop *http_loop_init(http_route *routes, size_t route_count,
|
||||
void *custom_gctx, void *(*custom_ctx_init)(),
|
||||
void(custom_ctx_reset)(), void(custom_ctx_free)()) {
|
||||
event_loop *el = event_loop_init();
|
||||
|
||||
el->ctx_init = (void *(*)(void *))http_loop_ctx_init;
|
||||
el->ctx_free = (void (*)(void *))http_loop_ctx_free;
|
||||
el->handle_data = http_loop_handle_request;
|
||||
el->write_data = http_loop_write_response;
|
||||
|
||||
http_loop_gctx *gctx = http_loop_gctx_init();
|
||||
gctx->c = custom_gctx;
|
||||
gctx->routes = routes;
|
||||
gctx->route_count = route_count;
|
||||
gctx->custom_ctx_init = custom_ctx_init;
|
||||
gctx->custom_ctx_reset = custom_ctx_reset;
|
||||
gctx->custom_ctx_free = custom_ctx_free;
|
||||
el->gctx = gctx;
|
||||
|
||||
return el;
|
||||
}
|
||||
|
||||
void http_loop_set_api_key(http_loop *hl, const char *api_key) {
|
||||
((http_loop_gctx *)hl->gctx)->api_key = api_key;
|
||||
}
|
||||
|
||||
void http_loop_run(event_loop *el, int port) {
|
||||
debug("Compiling RegEx routes");
|
||||
|
||||
|
|
|
|||
|
|
@ -12,12 +12,14 @@ http_loop_gctx *http_loop_gctx_init() {
|
|||
http_loop_ctx *http_loop_ctx_init(http_loop_gctx *g) {
|
||||
http_loop_ctx *ctx = calloc(sizeof(http_loop_ctx), 1);
|
||||
ctx->g = g;
|
||||
ctx->c = g->custom_ctx_init();
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
void http_loop_ctx_free(http_loop_ctx *ctx) {
|
||||
http_loop_ctx_reset(ctx);
|
||||
ctx->g->custom_ctx_free(ctx->c);
|
||||
|
||||
free(ctx);
|
||||
}
|
||||
|
|
@ -45,4 +47,6 @@ void http_loop_ctx_reset(http_loop_ctx *ctx) {
|
|||
ctx->res.status = 0;
|
||||
ctx->res.head_len = 0;
|
||||
ctx->res.head_written = 0;
|
||||
|
||||
ctx->g->custom_ctx_reset(ctx->c);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue