feat: configure api key using env var

c-web-server
Jef Roosens 2023-05-31 13:29:13 +02:00
parent 3b1df332a3
commit 323fa65921
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 17 additions and 9 deletions

View File

@ -1,9 +1,9 @@
#ifndef LANDER_HTTP_TYPES #ifndef LANDER_HTTP_TYPES
#define LANDER_HTTP_TYPES #define LANDER_HTTP_TYPES
#include <sys/types.h>
#include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h>
#include <sys/types.h>
// Array mapping the http_request_method enum to strings // Array mapping the http_request_method enum to strings
extern const char *http_method_names[]; extern const char *http_method_names[];

View File

@ -2,9 +2,7 @@
#include "http/types.h" #include "http/types.h"
http_body *http_body_init() { http_body *http_body_init() { return calloc(sizeof(http_body), 1); }
return calloc(sizeof(http_body), 1);
}
void http_body_reset(http_body *body) { void http_body_reset(http_body *body) {
if (body->type == http_body_file) { if (body->type == http_body_file) {

View File

@ -76,8 +76,8 @@ bool http_loop_step_body_to_buf(event_loop_conn *conn) {
size_t bytes_to_copy = MIN(conn->rbuf_size - conn->rbuf_read, size_t bytes_to_copy = MIN(conn->rbuf_size - conn->rbuf_read,
ctx->req.body.expected_len - ctx->req.body.len); ctx->req.body.expected_len - ctx->req.body.len);
memcpy(&ctx->req.body.buf[ctx->req.body.len], memcpy(&ctx->req.body.buf[ctx->req.body.len], &conn->rbuf[conn->rbuf_read],
&conn->rbuf[conn->rbuf_read], bytes_to_copy); bytes_to_copy);
ctx->req.body.len += bytes_to_copy; ctx->req.body.len += bytes_to_copy;
conn->rbuf_read += bytes_to_copy; conn->rbuf_read += bytes_to_copy;
@ -114,7 +114,8 @@ bool http_loop_step_auth(event_loop_conn *conn) {
struct phr_header *header = &ctx->req.headers[i]; struct phr_header *header = &ctx->req.headers[i];
if ((strncmp("X-Api-Key", header->name, header->name_len) == 0) && if ((strncmp("X-Api-Key", header->name, header->name_len) == 0) &&
(strncmp(header->value, ctx->g->api_key, header->value_len) == 0)) { (strncmp(header->value, ctx->g->api_key, header->value_len) == 0) &&
strlen(ctx->g->api_key) == header->value_len) {
return true; return true;
} }
} }

View File

@ -3,9 +3,18 @@
#include "lander.h" #include "lander.h"
#include "log.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);
int main() { int main() {
setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0);
ENV(api_key, "LANDER_API_KEY");
info("Initializing trie"); info("Initializing trie");
Trie *trie; Trie *trie;
@ -21,7 +30,7 @@ int main() {
gctx->trie = trie; gctx->trie = trie;
gctx->routes = lander_routes; gctx->routes = lander_routes;
gctx->route_count = sizeof(lander_routes) / sizeof(lander_routes[0]); gctx->route_count = sizeof(lander_routes) / sizeof(lander_routes[0]);
gctx->api_key = "test"; gctx->api_key = api_key;
event_loop *el = http_loop_init(gctx); event_loop *el = http_loop_init(gctx);
http_loop_run(el, 18080); http_loop_run(el, 18080);