feat(ltm): PoC paste pages

ltm
Jef Roosens 2023-12-17 17:29:20 +01:00
parent 11421dca74
commit 8c2a7a640d
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
6 changed files with 60 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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 {

View File

@ -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); }

View File

@ -9,6 +9,7 @@
#include "lsm/store.h"
#include "lander.h"
#include "ltm/template.h"
static const char index_page[] =
"<!DOCTYPE html>\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;

View File

@ -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 =
"<!doctype html>\n"
"<head><link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/styles/stackoverflow-dark.min.css\">\n"
"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js\"></script>\n"
"<script>hljs.highlightAll();</script></head>\n"
"<body><h1>{{ header }}</h1><pre><code>{{ paste }}</code></pre></body>";
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);