feat: env vars for configuring data dir & tcp port
ci/woodpecker/push/docker Pipeline was successful Details

lsm
Jef Roosens 2023-07-27 14:09:56 +02:00
parent 82746c1dd0
commit 7f2a45c37a
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
6 changed files with 35 additions and 8 deletions

View File

@ -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

View File

@ -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)

View File

@ -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;
/**

View File

@ -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

View File

@ -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;

View File

@ -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);
}