118 lines
4.2 KiB
C
118 lines
4.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
|
|
#include "lnm/http/loop.h"
|
|
#include "lnm/log.h"
|
|
|
|
#include "lander.h"
|
|
#include "log.h"
|
|
|
|
const char *lander_server = "lander/" LANDER_VERSION;
|
|
|
|
lnm_http_loop *loop_init(lander_gctx *gctx, const char *api_key) {
|
|
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_loop_set_api_key(hl, api_key);
|
|
lnm_http_loop_set_server(hl, lander_server);
|
|
|
|
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);
|
|
|
|
lnm_http_step_init(&step, lander_get_entry);
|
|
lnm_http_route_init_regex(&route, lnm_http_method_get, "^/([^/]+)$", 1, step);
|
|
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);
|
|
|
|
return hl;
|
|
}
|
|
|
|
#define ENV(var, env_var) \
|
|
const char *var = getenv(env_var); \
|
|
if (var == NULL) { \
|
|
lnm_lcritical("main", "Missing environment variable %s", env_var); \
|
|
exit(1); \
|
|
}
|
|
|
|
#define ENV_OPT(var, env_var, default) \
|
|
const char *var = getenv(env_var); \
|
|
if (var == NULL) { \
|
|
var = default; \
|
|
}
|
|
|
|
int main() {
|
|
srand(time(NULL));
|
|
|
|
lnm_log_init_global();
|
|
lnm_log_register_stdout(lnm_log_level_debug);
|
|
|
|
ENV(api_key, "LANDER_API_KEY");
|
|
ENV_OPT(port_str, "LANDER_PORT", "18080");
|
|
ENV_OPT(data_dir_s, "LANDER_DATA_DIR", ".");
|
|
|
|
int port = atoi(port_str);
|
|
|
|
if (port <= 0 || port >= 1 << 16) {
|
|
lnm_lcritical("main", "Invalid TCP port %s", port_str);
|
|
exit(1);
|
|
}
|
|
|
|
lander_gctx *c_gctx = lander_gctx_init();
|
|
c_gctx->data_dir = data_dir_s;
|
|
|
|
lsm_str *data_dir;
|
|
lsm_str_init_copy(&data_dir, (char *)data_dir_s);
|
|
|
|
lnm_linfo("main", "Initializing store from path '%s'", data_dir_s);
|
|
|
|
if (lsm_store_load(&c_gctx->store, data_dir) != lsm_error_ok) {
|
|
lnm_lcritical("main", "%s", "Failed to load existing store.");
|
|
}
|
|
|
|
lnm_linfo("main", "Store loaded containing %lu entries",
|
|
lsm_store_size(c_gctx->store));
|
|
lnm_http_loop *hl = loop_init(c_gctx, api_key);
|
|
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_run(hl, port); */
|
|
}
|