feat: env vars for configuring data dir & tcp port
All checks were successful
ci/woodpecker/push/docker Pipeline was successful
All checks were successful
ci/woodpecker/push/docker Pipeline was successful
This commit is contained in:
parent
82746c1dd0
commit
7f2a45c37a
6 changed files with 35 additions and 8 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
26
src/main.c
26
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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue