diff --git a/include/lander.h b/include/lander.h index 88bfab9..61c4297 100644 --- a/include/lander.h +++ b/include/lander.h @@ -4,7 +4,7 @@ #include "http_loop.h" #include "lsm/store.h" -extern http_route lander_routes[4]; +extern http_route lander_routes[5]; typedef struct lander_gctx { const char *data_dir; @@ -57,4 +57,6 @@ bool lander_get_entry_lsm(event_loop_conn *conn); bool lander_post_redirect_body_to_attr(event_loop_conn *conn); +bool lander_remove_entry(event_loop_conn *conn); + #endif diff --git a/lsm/src/store/lsm_store_disk_write.c b/lsm/src/store/lsm_store_disk_write.c index 243c8f5..b6906e6 100644 --- a/lsm/src/store/lsm_store_disk_write.c +++ b/lsm/src/store/lsm_store_disk_write.c @@ -151,6 +151,8 @@ lsm_error lsm_entry_disk_remove(lsm_entry_handle *handle) { return res; } + fflush(store->idx.f); + // Remove data file if present if (entry->data_len > 0) { if (handle->f != NULL) { diff --git a/src/lander/lander.c b/src/lander/lander.c index 57f5c5e..a045428 100644 --- a/src/lander/lander.c +++ b/src/lander/lander.c @@ -19,6 +19,14 @@ http_route lander_routes[] = { .steps_res = {http_loop_step_write_header, lander_stream_body_to_client, NULL}, }, + { + .type = http_route_regex, + .method = http_delete, + .path = "^/([^/]+)$", + .steps = {http_loop_step_auth, lander_remove_entry, NULL}, + .steps_res = {http_loop_step_write_header, http_loop_step_write_body, + NULL}, + }, { .type = http_route_regex, .method = http_post, diff --git a/src/lander/lander_delete.c b/src/lander/lander_delete.c new file mode 100644 index 0000000..e91b6c9 --- /dev/null +++ b/src/lander/lander_delete.c @@ -0,0 +1,29 @@ +#include "lander.h" + +bool lander_remove_entry(event_loop_conn *conn) { + http_loop_ctx *ctx = conn->ctx; + lander_ctx *c_ctx = ctx->c; + http_loop_gctx *gctx = ctx->g; + lander_gctx *c_gctx = gctx->c; + + const char *key_s = &ctx->req.path[ctx->req.regex_groups[1].rm_so]; + int key_len = ctx->req.regex_groups[1].rm_eo - ctx->req.regex_groups[1].rm_so; + + lsm_str *key; + lsm_str_init_copy_n(&key, (char *)key_s, key_len); + + switch (lsm_store_open_write(&c_ctx->entry, c_gctx->store, key)) { + case lsm_error_ok: + break; + case lsm_error_not_found: + ctx->res.status = http_not_found; + return true; + default: + ctx->res.status = http_internal_server_error; + return true; + } + + lsm_entry_remove(c_ctx->entry); + + return true; +}