feat(lander): initial lnm integration test
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
Jef Roosens 2023-12-02 20:22:05 +01:00
parent 799821d9fc
commit 8ec667af3b
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
15 changed files with 187 additions and 44 deletions

View file

@ -1,10 +1,12 @@
#include <stdio.h>
#include <string.h>
#include "lnm/common.h"
#include "lsm/store.h"
#include "http/types.h"
#include "http_loop.h"
#include "lander.h"
#include "lsm/store.h"
const char lander_key_charset[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
@ -60,7 +62,17 @@ http_route lander_routes[] = {
void *lander_gctx_init() { return calloc(1, sizeof(lander_gctx)); }
void *lander_ctx_init() { return calloc(1, sizeof(lander_ctx)); }
lnm_err lander_ctx_init(void **c_ctx, void *gctx) {
lander_ctx *ctx = calloc(1, sizeof(lander_ctx));
if (ctx == NULL) {
return lnm_err_failed_alloc;
}
*c_ctx = ctx;
return lnm_err_ok;
}
void lander_ctx_reset(lander_ctx *ctx) {
if (ctx->entry != NULL) {

View file

@ -1,5 +1,7 @@
#include <stdio.h>
#include "lnm/loop.h"
#include "event_loop.h"
#include "http/res.h"
#include "http/types.h"
@ -17,14 +19,18 @@ static const char index_page[] =
" </body>\n"
"</html>\n";
bool lander_get_index(event_loop_conn *conn) {
http_loop_ctx *ctx = conn->ctx;
lnm_http_step_err lander_get_index(lnm_http_conn *conn) {
lnm_http_loop_ctx *ctx = conn->ctx;
http_res_set_body_buf(&ctx->res, index_page, sizeof(index_page) - 1, false);
http_res_set_mime_type(&ctx->res, http_mime_html);
lnm_http_res_body_set_buf(&ctx->res, (char *)index_page,
sizeof(index_page) - 1, false);
conn->state = event_loop_conn_state_res;
return true;
/* http_res_set_body_buf(&ctx->res, index_page, sizeof(index_page) - 1,
* false); */
/* http_res_set_mime_type(&ctx->res, http_mime_html); */
/* conn->state = event_loop_conn_state_res; */
return lnm_http_step_err_done;
}
void lander_get_redirect(event_loop_conn *conn) {

View file

@ -2,9 +2,26 @@
#include <stdlib.h>
#include <time.h>
#include "lnm/http/loop.h"
#include "lander.h"
#include "log.h"
lnm_http_loop *loop_init(lander_gctx *gctx) {
lnm_http_loop *hl;
lnm_http_step *step = NULL;
lnm_http_route *route;
lnm_http_loop_init(&hl, gctx, lander_ctx_init,
(lnm_http_ctx_reset_fn)lander_ctx_reset,
(lnm_http_ctx_free_fn)lander_ctx_free);
lnm_http_step_init(&step, lander_get_index);
lnm_http_route_init_literal(&route, lnm_http_method_get, "/", step);
lnm_http_loop_route_add(hl, route);
return hl;
}
#define ENV(var, env_var) \
const char *var = getenv(env_var); \
if (var == NULL) { \
@ -44,12 +61,15 @@ int main() {
}
info("Store loaded containing %lu entries", lsm_store_size(c_gctx->store));
lnm_http_loop *hl = loop_init(c_gctx);
lnm_http_loop_run(hl, port);
http_loop *hl = http_loop_init(
lander_routes, sizeof(lander_routes) / sizeof(lander_routes[0]), c_gctx,
lander_ctx_init, (void (*)(void *))lander_ctx_reset,
(void (*)(void *))lander_ctx_free);
http_loop_set_api_key(hl, api_key);
/* http_loop *hl = http_loop_init( */
/* lander_routes, sizeof(lander_routes) / sizeof(lander_routes[0]),
* c_gctx, */
/* lander_ctx_init, (void (*)(void *))lander_ctx_reset, */
/* (void (*)(void *))lander_ctx_free); */
/* http_loop_set_api_key(hl, api_key); */
http_loop_run(hl, port);
/* http_loop_run(hl, port); */
}