refactor(lander): partial migration to updated lnm framework
parent
7b195c75b0
commit
44ba4b053e
|
@ -0,0 +1,3 @@
|
||||||
|
[submodule "lnm"]
|
||||||
|
path = lnm
|
||||||
|
url = https://git.rustybever.be/Chewing_Bever/lnm
|
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 6eab5d616c2772a625b521e19d50020156c72ca2
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include "lnm/http/consts.h"
|
#include "lnm/http/consts.h"
|
||||||
#include "lnm/http/loop.h"
|
#include "lnm/http/loop.h"
|
||||||
|
#include "lnm/http/req.h"
|
||||||
#include "lnm/log.h"
|
#include "lnm/log.h"
|
||||||
#include "lnm/loop.h"
|
#include "lnm/loop.h"
|
||||||
#include "lsm/store.h"
|
#include "lsm/store.h"
|
||||||
|
@ -102,9 +103,8 @@ lnm_http_step_err lander_get_entry(lnm_http_conn *conn) {
|
||||||
lnm_http_loop_gctx *gctx = ctx->g;
|
lnm_http_loop_gctx *gctx = ctx->g;
|
||||||
lander_gctx *c_gctx = gctx->c;
|
lander_gctx *c_gctx = gctx->c;
|
||||||
|
|
||||||
const char *key_s =
|
const char *key_s;
|
||||||
&ctx->req.buf.s[ctx->req.path.o + ctx->req.path.groups[1].rm_so];
|
size_t key_len = lnm_http_req_route_segment(&key_s, &ctx->req, "key");
|
||||||
int key_len = ctx->req.path.groups[1].rm_eo - ctx->req.path.groups[1].rm_so;
|
|
||||||
|
|
||||||
lsm_str *key;
|
lsm_str *key;
|
||||||
lsm_str_init_copy_n(&key, (char *)key_s, key_len);
|
lsm_str_init_copy_n(&key, (char *)key_s, key_len);
|
||||||
|
@ -138,6 +138,9 @@ lnm_http_step_err lander_get_entry(lnm_http_conn *conn) {
|
||||||
case lander_entry_type_file:
|
case lander_entry_type_file:
|
||||||
res = lander_get_file(conn);
|
res = lander_get_file(conn);
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
ctx->res.status = lnm_http_status_internal_server_error;
|
||||||
|
res = lnm_http_step_err_res;
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "lnm/loop.h"
|
#include "lnm/loop.h"
|
||||||
|
#include "lnm/http/req.h"
|
||||||
#include "lsm/store.h"
|
#include "lsm/store.h"
|
||||||
|
|
||||||
#include "lander.h"
|
#include "lander.h"
|
||||||
|
@ -20,28 +21,24 @@ static void randomize_key(char *key, int len) {
|
||||||
*
|
*
|
||||||
* @return true on success, false otherwise
|
* @return true on success, false otherwise
|
||||||
*/
|
*/
|
||||||
bool lander_insert_entry(lnm_http_loop_ctx *ctx) {
|
bool lander_insert_entry(lnm_http_loop_ctx *ctx, bool secure) {
|
||||||
lnm_http_loop_gctx *gctx = ctx->g;
|
lnm_http_loop_gctx *gctx = ctx->g;
|
||||||
lander_gctx *c_gctx = gctx->c;
|
lander_gctx *c_gctx = gctx->c;
|
||||||
lander_ctx *c_ctx = ctx->c;
|
lander_ctx *c_ctx = ctx->c;
|
||||||
|
|
||||||
lsm_str *key;
|
const char *key_s;
|
||||||
int key_len;
|
size_t key_len = lnm_http_req_route_segment(&key_s, &ctx->req, "key");
|
||||||
|
|
||||||
if (ctx->req.path.groups[2].rm_eo == ctx->req.path.groups[2].rm_so) {
|
lsm_str *key;
|
||||||
|
|
||||||
|
if (key_len == 0) {
|
||||||
// Generate a random key to insert
|
// Generate a random key to insert
|
||||||
bool secure =
|
|
||||||
(ctx->req.path.groups[1].rm_eo - ctx->req.path.groups[1].rm_so) == 1;
|
|
||||||
key_len = secure ? 16 : 4;
|
key_len = secure ? 16 : 4;
|
||||||
char *key_s = malloc((key_len + 1) * sizeof(char));
|
char *key_s = malloc((key_len + 1) * sizeof(char));
|
||||||
|
|
||||||
randomize_key(key_s, key_len);
|
randomize_key(key_s, key_len);
|
||||||
lsm_str_init(&key, key_s);
|
lsm_str_init(&key, key_s);
|
||||||
} else {
|
} else {
|
||||||
const char *key_s =
|
|
||||||
&ctx->req.buf.s[ctx->req.path.o + ctx->req.path.groups[2].rm_so];
|
|
||||||
key_len = ctx->req.path.groups[2].rm_eo - ctx->req.path.groups[2].rm_so;
|
|
||||||
|
|
||||||
lsm_str_init_copy_n(&key, key_s, key_len);
|
lsm_str_init_copy_n(&key, key_s, key_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +70,7 @@ lnm_http_step_err lander_post_redirect(lnm_http_conn *conn) {
|
||||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||||
lander_ctx *c_ctx = ctx->c;
|
lander_ctx *c_ctx = ctx->c;
|
||||||
|
|
||||||
if (!lander_insert_entry(ctx)) {
|
if (!lander_insert_entry(ctx, false)) {
|
||||||
return lnm_http_step_err_res;
|
return lnm_http_step_err_res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,7 +95,7 @@ lnm_http_step_err lander_post_paste(lnm_http_conn *conn) {
|
||||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||||
lander_ctx *c_ctx = ctx->c;
|
lander_ctx *c_ctx = ctx->c;
|
||||||
|
|
||||||
if (!lander_insert_entry(ctx)) {
|
if (!lander_insert_entry(ctx, false)) {
|
||||||
return lnm_http_step_err_res;
|
return lnm_http_step_err_res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,7 +110,7 @@ lnm_http_step_err lander_post_file(lnm_http_conn *conn) {
|
||||||
lnm_http_loop_ctx *ctx = conn->ctx;
|
lnm_http_loop_ctx *ctx = conn->ctx;
|
||||||
lander_ctx *c_ctx = ctx->c;
|
lander_ctx *c_ctx = ctx->c;
|
||||||
|
|
||||||
if (!lander_insert_entry(ctx)) {
|
if (!lander_insert_entry(ctx, false)) {
|
||||||
return lnm_http_step_err_res;
|
return lnm_http_step_err_res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
84
src/main.c
84
src/main.c
|
@ -12,49 +12,67 @@ const char *lander_server = "lander/" LANDER_VERSION;
|
||||||
|
|
||||||
lnm_http_loop *loop_init(lander_gctx *gctx, const char *api_key) {
|
lnm_http_loop *loop_init(lander_gctx *gctx, const char *api_key) {
|
||||||
lnm_http_loop *hl;
|
lnm_http_loop *hl;
|
||||||
lnm_http_step *step = NULL;
|
|
||||||
lnm_http_route *route;
|
|
||||||
lnm_http_loop_init(&hl, gctx, lander_ctx_init,
|
lnm_http_loop_init(&hl, gctx, lander_ctx_init,
|
||||||
(lnm_http_ctx_reset_fn)lander_ctx_reset,
|
(lnm_http_ctx_reset_fn)lander_ctx_reset,
|
||||||
(lnm_http_ctx_free_fn)lander_ctx_free);
|
(lnm_http_ctx_free_fn)lander_ctx_free);
|
||||||
lnm_http_loop_set_api_key(hl, api_key);
|
lnm_http_loop_set_api_key(hl, api_key);
|
||||||
lnm_http_loop_set_server(hl, lander_server);
|
lnm_http_loop_set_server(hl, lander_server);
|
||||||
|
|
||||||
lnm_http_step_init(&step, lander_get_index);
|
lnm_http_router *router;
|
||||||
lnm_http_route_init_literal(&route, lnm_http_method_get, "/", step);
|
lnm_http_router_init(&router);
|
||||||
lnm_http_loop_route_add(hl, route);
|
|
||||||
|
|
||||||
lnm_http_step_init(&step, lander_get_entry);
|
lnm_http_route *route;
|
||||||
lnm_http_route_init_regex(&route, lnm_http_method_get, "^/([^/]+)$", 1, step);
|
lnm_http_router_add(&route, router, lnm_http_method_get, "/");
|
||||||
lnm_http_loop_route_add(hl, route);
|
lnm_http_route_step_append(route, lander_get_index, false);
|
||||||
|
|
||||||
lnm_http_step_init(&step, lnm_http_loop_step_auth);
|
lnm_http_router_add(&route, router, lnm_http_method_get, "/:key");
|
||||||
lnm_http_route_init_regex(&route, lnm_http_method_post, "^/s(l?)/([^/]*)$", 2,
|
lnm_http_route_step_append(route, lander_get_entry, false);
|
||||||
step);
|
|
||||||
lnm_http_step_append(&step, step, lander_post_redirect);
|
|
||||||
lnm_http_step_append(&step, step, lnm_http_loop_step_body_to_buf);
|
|
||||||
lnm_http_step_append(&step, step, lander_post_redirect_body_to_attr);
|
|
||||||
lnm_http_loop_route_add(hl, route);
|
|
||||||
|
|
||||||
lnm_http_step_init(&step, lnm_http_loop_step_auth);
|
lnm_http_router_add(&route, router, lnm_http_method_post, "/s/:key");
|
||||||
lnm_http_route_init_regex(&route, lnm_http_method_post, "^/p(l?)/([^/]*)$", 2,
|
lnm_http_route_step_append(route, lnm_http_loop_step_auth, false);
|
||||||
step);
|
lnm_http_route_step_append(route, lander_post_redirect, false);
|
||||||
lnm_http_step_append(&step, step, lander_post_paste);
|
lnm_http_route_step_append(route, lnm_http_loop_step_body_to_buf, false);
|
||||||
lnm_http_step_append(&step, step, lander_stream_body_to_entry);
|
lnm_http_route_step_append(route, lander_post_redirect_body_to_attr, false);
|
||||||
lnm_http_loop_route_add(hl, route);
|
|
||||||
|
|
||||||
lnm_http_step_init(&step, lnm_http_loop_step_auth);
|
lnm_http_router_add(&route, router, lnm_http_method_post, "/p/:key");
|
||||||
lnm_http_route_init_regex(&route, lnm_http_method_post, "^/f(l?)/([^/]*)$", 2,
|
lnm_http_route_step_append(route, lnm_http_loop_step_auth, false);
|
||||||
step);
|
lnm_http_route_step_append(route, lander_post_paste, false);
|
||||||
lnm_http_step_append(&step, step, lander_post_file);
|
lnm_http_route_step_append(route, lander_stream_body_to_entry, false);
|
||||||
lnm_http_step_append(&step, step, lander_stream_body_to_entry);
|
|
||||||
lnm_http_loop_route_add(hl, route);
|
|
||||||
|
|
||||||
lnm_http_step_init(&step, lnm_http_loop_step_auth);
|
lnm_http_router_add(&route, router, lnm_http_method_post, "/f/:key");
|
||||||
lnm_http_route_init_regex(&route, lnm_http_method_delete, "^/([^/]+)$", 1,
|
lnm_http_route_step_append(route, lnm_http_loop_step_auth, false);
|
||||||
step);
|
lnm_http_route_step_append(route, lander_post_file, false);
|
||||||
lnm_http_step_append(&step, step, lander_remove_entry);
|
lnm_http_route_step_append(route, lander_stream_body_to_entry, false);
|
||||||
lnm_http_loop_route_add(hl, route);
|
|
||||||
|
/* lnm_http_step_init(&step, lnm_http_loop_step_auth); */
|
||||||
|
/* lnm_http_route_init_regex(&route, lnm_http_method_post, "^/s(l?)/([^/]*)$", 2, */
|
||||||
|
/* step); */
|
||||||
|
/* lnm_http_step_append(&step, step, lander_post_redirect); */
|
||||||
|
/* lnm_http_step_append(&step, step, lnm_http_loop_step_body_to_buf); */
|
||||||
|
/* lnm_http_step_append(&step, step, lander_post_redirect_body_to_attr); */
|
||||||
|
/* lnm_http_loop_route_add(hl, route); */
|
||||||
|
|
||||||
|
/* lnm_http_step_init(&step, lnm_http_loop_step_auth); */
|
||||||
|
/* lnm_http_route_init_regex(&route, lnm_http_method_post, "^/p(l?)/([^/]*)$", 2, */
|
||||||
|
/* step); */
|
||||||
|
/* lnm_http_step_append(&step, step, lander_post_paste); */
|
||||||
|
/* lnm_http_step_append(&step, step, lander_stream_body_to_entry); */
|
||||||
|
/* lnm_http_loop_route_add(hl, route); */
|
||||||
|
|
||||||
|
/* lnm_http_step_init(&step, lnm_http_loop_step_auth); */
|
||||||
|
/* lnm_http_route_init_regex(&route, lnm_http_method_post, "^/f(l?)/([^/]*)$", 2, */
|
||||||
|
/* step); */
|
||||||
|
/* lnm_http_step_append(&step, step, lander_post_file); */
|
||||||
|
/* lnm_http_step_append(&step, step, lander_stream_body_to_entry); */
|
||||||
|
/* lnm_http_loop_route_add(hl, route); */
|
||||||
|
|
||||||
|
/* lnm_http_step_init(&step, lnm_http_loop_step_auth); */
|
||||||
|
/* lnm_http_route_init_regex(&route, lnm_http_method_delete, "^/([^/]+)$", 1, */
|
||||||
|
/* step); */
|
||||||
|
/* lnm_http_step_append(&step, step, lander_remove_entry); */
|
||||||
|
/* lnm_http_loop_route_add(hl, route); */
|
||||||
|
|
||||||
|
lnm_http_loop_router_set(hl, router);
|
||||||
|
|
||||||
return hl;
|
return hl;
|
||||||
}
|
}
|
||||||
|
@ -104,7 +122,7 @@ int main() {
|
||||||
lnm_linfo("main", "Store loaded containing %lu entries",
|
lnm_linfo("main", "Store loaded containing %lu entries",
|
||||||
lsm_store_size(c_gctx->store));
|
lsm_store_size(c_gctx->store));
|
||||||
lnm_http_loop *hl = loop_init(c_gctx, api_key);
|
lnm_http_loop *hl = loop_init(c_gctx, api_key);
|
||||||
lnm_http_loop_run(hl, port, 1);
|
lnm_http_loop_run(hl, port, 1, 0);
|
||||||
|
|
||||||
/* http_loop *hl = http_loop_init( */
|
/* http_loop *hl = http_loop_init( */
|
||||||
/* lander_routes, sizeof(lander_routes) / sizeof(lander_routes[0]),
|
/* lander_routes, sizeof(lander_routes) / sizeof(lander_routes[0]),
|
||||||
|
|
Loading…
Reference in New Issue