feat(http): fully decouple HTTP loop functionality

This commit is contained in:
Jef Roosens 2023-10-30 21:14:06 +01:00
parent fc4187e6ce
commit 6d74c8c550
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
9 changed files with 99 additions and 21 deletions

View file

@ -52,9 +52,12 @@ typedef struct http_route {
typedef struct http_loop_gctx {
http_route *routes;
size_t route_count;
Trie *trie;
void *(*custom_ctx_init)();
void (*custom_ctx_reset)(void *);
void (*custom_ctx_free)(void *);
const char *api_key;
const char *data_dir;
// Custom global context
void *c;
} http_loop_gctx;
/**
@ -73,6 +76,7 @@ typedef struct http_loop_ctx {
http_route *route;
size_t current_step;
http_loop_gctx *g;
void *c;
} http_loop_ctx;
/**
@ -98,6 +102,11 @@ void http_loop_ctx_reset(http_loop_ctx *ctx);
*/
void http_loop_ctx_free(http_loop_ctx *ctx);
/**
* Represents an HTTP loop
*/
typedef struct event_loop http_loop;
/**
* Process incoming data as an HTTP request. This is the "handle_data" function
* for the event loop.
@ -174,10 +183,27 @@ bool http_loop_step_switch_res(event_loop_conn *conn);
/**
* Initialize a new http loop.
*
* @param gctx global context for the event loop
* @param routes array of routes that should be served
* @parma route_count how many elements are in `routes`
* @param custom_gctx the application's custom global context; can be NULL
* @param custom_ctx_init function to initialize a new custom context
* @param custom_ctx_reset function to reset a custom context
* @param custom_ctx_free function to free a custom context; will always be run
* after a reset
* @return pointer to the newly allocated object
*/
event_loop *http_loop_init(http_loop_gctx *gctx);
http_loop *http_loop_init(http_route *routes, size_t route_count,
void *custom_gctx, void *(*custom_ctx_init)(),
void(custom_ctx_reset)(void *),
void(custom_ctx_free)(void *));
/**
* Set the API key the authentication steps should use.
*
* @param hl HTTP loop to set key in
* @param api_key API key to use
*/
void http_loop_set_api_key(http_loop *hl, const char *api_key);
/**
* Run the HTTP loop. This function never returns.
@ -185,6 +211,6 @@ event_loop *http_loop_init(http_loop_gctx *gctx);
* @param el the event loop
* @param port on what port to listen
*/
void http_loop_run(event_loop *el, int port);
void http_loop_run(http_loop *hl, int port);
#endif

View file

@ -5,6 +5,23 @@
extern http_route lander_routes[4];
typedef struct lander_gctx {
const char *data_dir;
Trie *trie;
} lander_gctx;
typedef struct lander_ctx {
char *key;
} lander_ctx;
void *lander_gctx_init();
void *lander_ctx_init();
void lander_ctx_reset(lander_ctx *ctx);
void lander_ctx_free(lander_ctx *ctx);
bool lander_get_index(event_loop_conn *conn);
bool lander_get_entry(event_loop_conn *conn);