From 8c2a7a640d515cb1b882dc3d63004cf108ab7656 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sun, 17 Dec 2023 17:29:20 +0100 Subject: [PATCH] feat(ltm): PoC paste pages --- Makefile | 6 +++++- config.mk | 8 ++++---- include/lander.h | 5 +++++ src/lander/lander.c | 7 +++++++ src/lander/lander_get.c | 34 ++++++++++++++++++++++++++++++---- src/main.c | 9 +++++++++ 6 files changed, 60 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 9cd57fe..7c7b48e 100644 --- a/Makefile +++ b/Makefile @@ -42,7 +42,11 @@ liblsm: liblnm: $(MAKE) -C lnm -$(BIN): liblsm liblnm $(OBJS) +.PHONY: libltm +libltm: + $(MAKE) -C ltm + +$(BIN): liblsm liblnm libltm $(OBJS) $(CC) -o $@ $(OBJS) $(_LDFLAGS) $(BUILD_DIR)/$(SRC_DIR)/%.c.o: $(SRC_DIR)/%.c diff --git a/config.mk b/config.mk index d5230d7..19c0510 100644 --- a/config.mk +++ b/config.mk @@ -7,16 +7,16 @@ SRC_DIR = src TEST_DIR = test THIRDPARTY_DIR = thirdparty -INC_DIRS = include $(THIRDPARTY_DIR)/include lsm/include lnm/include -LIBS = lsm lnm -LIB_DIRS = ./lsm/build ./lnm/build +INC_DIRS = include $(THIRDPARTY_DIR)/include lsm/include lnm/include ltm/include +LIBS = lsm lnm ltm +LIB_DIRS = ./lsm/build ./lnm/build ./ltm/build # -MMD: generate a .d file for every source file. This file can be imported by # make and makes make aware that a header file has been changed, ensuring an # object file is also recompiled if only a header is changed. # -MP: generate a dummy target for every header file (according to the docs it # prevents some errors when removing header files) -CFLAGS ?= -MMD -MP -g +CFLAGS ?= -MMD -MP -g -Wall # When compiling release builds, these flags are better # CLAGS = -O3 diff --git a/include/lander.h b/include/lander.h index 7b418bc..cee1399 100644 --- a/include/lander.h +++ b/include/lander.h @@ -4,16 +4,21 @@ #include "lnm/common.h" #include "lnm/http/loop.h" #include "lsm/store.h" +#include "ltm/template.h" extern const char lander_key_charset[]; typedef struct lander_gctx { const char *data_dir; lsm_store *store; + struct { + ltm_template *paste; + } templates; } lander_gctx; typedef struct lander_ctx { lsm_entry_handle *entry; + ltm_instance *instance; } lander_ctx; typedef enum lander_attr_type : uint8_t { diff --git a/src/lander/lander.c b/src/lander/lander.c index ff44b5c..203d71e 100644 --- a/src/lander/lander.c +++ b/src/lander/lander.c @@ -3,6 +3,7 @@ #include "lnm/common.h" #include "lsm/store.h" +#include "ltm/template.h" #include "lander.h" @@ -29,6 +30,12 @@ void lander_ctx_reset(lander_ctx *ctx) { ctx->entry = NULL; } + + if (ctx->instance != NULL) { + ltm_instance_free(ctx->instance); + + ctx->instance = NULL; + } } void lander_ctx_free(lander_ctx *ctx) { free(ctx); } diff --git a/src/lander/lander_get.c b/src/lander/lander_get.c index 7d4a186..b283fcf 100644 --- a/src/lander/lander_get.c +++ b/src/lander/lander_get.c @@ -9,6 +9,7 @@ #include "lsm/store.h" #include "lander.h" +#include "ltm/template.h" static const char index_page[] = "\n" @@ -73,13 +74,38 @@ lnm_err lander_entry_data_streamer(uint64_t *written, char *buf, return lnm_err_ok; } -lnm_http_step_err lander_get_paste(lnm_http_conn *conn) { +lnm_err lander_template_streamer(size_t *written, char *buf, lnm_http_conn *conn, uint64_t offset, uint64_t len) { lnm_http_loop_ctx *ctx = conn->ctx; lander_ctx *c_ctx = ctx->c; - lnm_http_res_body_set_fn(&ctx->res, lander_entry_data_streamer, - lsm_entry_data_len(c_ctx->entry)); - lnm_http_res_add_header(&ctx->res, lnm_http_header_content_type, "text/plain", + // TODO respect offset variable + + ltm_instance_write(written, buf, len, c_ctx->instance); + + return lnm_err_ok; +} + +ltm_err lander_data_to_template(size_t *written, char *buf, size_t len, void *data) { + lsm_entry_handle *entry = data; + + lsm_entry_data_read(written, buf, entry, len); + + return ltm_err_ok; +} + +lnm_http_step_err lander_get_paste(lnm_http_conn *conn) { + lnm_http_loop_ctx *ctx = conn->ctx; + lander_gctx *c_gctx = ctx->g->c; + lander_ctx *c_ctx = ctx->c; + + ltm_template_instantiate(&c_ctx->instance, c_gctx->templates.paste); + /* ltm_instance_block_add_var(c_ctx->instance, ltm_instance_block_type_buf, lsm_str_ptr(c_ctx->entry)) */ + ltm_instance_block_add_var_fn(c_ctx->instance, "paste", lander_data_to_template, c_ctx->entry, lsm_entry_data_len(c_ctx->entry)); + + + lnm_http_res_body_set_fn(&ctx->res, lander_template_streamer, + ltm_instance_size(c_ctx->instance)); + lnm_http_res_add_header(&ctx->res, lnm_http_header_content_type, "text/html", false); return lnm_http_step_err_done; diff --git a/src/main.c b/src/main.c index f0f8e65..31dbf94 100644 --- a/src/main.c +++ b/src/main.c @@ -4,10 +4,17 @@ #include "lnm/http/loop.h" #include "lnm/log.h" +#include "ltm/template.h" #include "lander.h" const char *lander_server = "lander/" LANDER_VERSION; +const char *paste_template = + "\n" + "\n" + "\n" + "\n" + "

{{ header }}

{{ paste }}
"; lnm_http_loop *loop_init(lander_gctx *gctx, const char *api_key) { lnm_http_loop *hl; @@ -117,6 +124,8 @@ int main() { lander_gctx *c_gctx = lander_gctx_init(); c_gctx->data_dir = data_dir_s; + ltm_template_compile(&c_gctx->templates.paste, paste_template); + lsm_str *data_dir; lsm_str_init_copy(&data_dir, (char *)data_dir_s);