diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e5c118..002254b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,4 +12,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Implemented own event & HTTP loop (used Crow framework before) * Serve redirects & pastes * Support uploading pastes & redirects - +* Env vars for configuring data dir and port diff --git a/Makefile b/Makefile index 45f72f7..06ab346 100644 --- a/Makefile +++ b/Makefile @@ -62,6 +62,12 @@ $(BUILD_DIR)/$(THIRDPARTY_DIR)/%.c.o: $(THIRDPARTY_DIR)/%.c # =====TESTING===== +.PHONY: run +run: bin + LANDER_API_KEY=test \ + LANDER_DATA_DIR=data \ + '$(BUILD_DIR)/$(BIN_FILENAME)' + .PHONY: test test: $(TARGETS_TEST) diff --git a/include/http_loop.h b/include/http_loop.h index b4b785a..b207d51 100644 --- a/include/http_loop.h +++ b/include/http_loop.h @@ -54,6 +54,7 @@ typedef struct http_loop_gctx { size_t route_count; Trie *trie; const char *api_key; + const char *data_dir; } http_loop_gctx; /** diff --git a/src/lander/lander_get.c b/src/lander/lander_get.c index 40d6db7..e4ba39b 100644 --- a/src/lander/lander_get.c +++ b/src/lander/lander_get.c @@ -37,8 +37,8 @@ bool lander_get_entry(event_loop_conn *conn) { ctx->res.status = http_moved_permanently; http_res_add_header(&ctx->res, http_header_location, entry->string, false); } else if (entry->type == Paste) { - char fname[8 + key_len]; - sprintf(fname, "pastes/%.*s", key_len, key); + char fname[strlen(ctx->g->data_dir) + 8 + key_len + 1]; + sprintf(fname, "%s/pastes/%.*s", ctx->g->data_dir, key_len, key); http_res_set_body_file(&ctx->res, fname); // TODO don't call everything a text file diff --git a/src/lander/lander_post.c b/src/lander/lander_post.c index 5d0c688..f1b686c 100644 --- a/src/lander/lander_post.c +++ b/src/lander/lander_post.c @@ -102,8 +102,8 @@ bool lander_post_paste(event_loop_conn *conn) { return true; } - char *fname = malloc(8 + key_len); - sprintf(fname, "pastes/%.*s", key_len, key); + char *fname = malloc(strlen(ctx->g->data_dir) + 8 + key_len + 1); + sprintf(fname, "%s/pastes/%.*s", ctx->g->data_dir, key_len, key); ctx->req.body.fname = fname; ctx->req.body.fname_owned = true; diff --git a/src/main.c b/src/main.c index bc956b8..fa9d95e 100644 --- a/src/main.c +++ b/src/main.c @@ -10,15 +10,34 @@ } \ 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); ENV(api_key, "LANDER_API_KEY"); + ENV_OPT(port_str, "LANDER_PORT", "18080"); + ENV_OPT(data_dir, "LANDER_DATA_DIR", "."); - info("Initializing trie"); + int port = atoi(port_str); + + if (port <= 0 || port >= 2 << 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, "lander.data"); + TrieExitCode res = trie_init(&trie, file_path); if (res != Ok) { critical(1, "An error occured while populating the trie."); @@ -31,7 +50,8 @@ int main() { gctx->routes = lander_routes; gctx->route_count = sizeof(lander_routes) / sizeof(lander_routes[0]); gctx->api_key = api_key; + gctx->data_dir = data_dir; event_loop *el = http_loop_init(gctx); - http_loop_run(el, 18080); + http_loop_run(el, port); }