73 lines
2.4 KiB
C
73 lines
2.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
|
|
#include "lander.h"
|
|
#include "log.h"
|
|
|
|
#define ENV(var, env_var) \
|
|
const char *var = getenv(env_var); \
|
|
if (var == NULL) { \
|
|
critical(1, "Missing environment variable %s", env_var); \
|
|
} \
|
|
var = strdup(var);
|
|
|
|
#define ENV_OPT(var, env_var, default) \
|
|
const char *var = getenv(env_var); \
|
|
if (var == NULL) { \
|
|
var = strdup(default); \
|
|
} else { \
|
|
var = strdup(var); \
|
|
}
|
|
|
|
int main() {
|
|
setvbuf(stdout, NULL, _IONBF, 0);
|
|
srand(time(NULL));
|
|
|
|
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) {
|
|
critical(1, "Invalid TCP port %s", port_str);
|
|
}
|
|
|
|
/* char file_path[strlen(data_dir) + 12 + 1]; */
|
|
/* sprintf(file_path, "%s/lander.data", data_dir); */
|
|
|
|
/* info("Initializing trie from file '%s'", file_path); */
|
|
|
|
/* Trie *trie; */
|
|
/* TrieExitCode res = trie_init(&trie, file_path); */
|
|
|
|
/* if (res != Ok) { */
|
|
/* critical(1, "An error occured while populating the trie."); */
|
|
/* } */
|
|
|
|
/* info("Trie initialized and populated with %i entries", trie_size(trie)); */
|
|
|
|
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);
|
|
|
|
info("Initializing store from path '%s'", data_dir_s);
|
|
|
|
if (lsm_store_load(&c_gctx->store, data_dir) != lsm_error_ok) {
|
|
critical(2, "Failed to load existing store.");
|
|
}
|
|
|
|
info("Store loaded containing %lu entries.", lsm_store_size(c_gctx->store));
|
|
|
|
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);
|
|
}
|