From 358f08897aca0aafeed993e91ef63edc00fd288f Mon Sep 17 00:00:00 2001 From: spl3g Date: Fri, 21 Mar 2025 13:38:30 +0300 Subject: Update the api and add middleware support --- main.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 42cfd21..0ba64c9 100644 --- a/main.c +++ b/main.c @@ -1,18 +1,15 @@ +#include "lib/const_strings.h" #include "lib/http.h" -void success(struct request req, struct response *resp) { - printf("Success handler:\n"); - cs_print("Method: %s\n", req.method); - cs_print("Path: %s\n", req.path); - cs_print("Body: %s\n", req.body); - - for (size_t i = 0; i < req.query.len; i++) { - cs_print("%s: ", req.query.data[i].key); - cs_print("%s\n", req.query.data[i].val); - }; +void success(struct request req) { + req.resp->code = OK; + req.resp->body = CS("Hello world!\n"); + http_send(req); +} - resp->code = OK; - resp->body = CS("Hello world!\n"); +void logging_func(http_middleware *self, struct request req) { + (self->next->func)(self->next, req); + http_log(HTTP_INFO, "[1]"CS_FMT" "CS_FMT": %ld\n", CS_ARG(req.method), CS_ARG(req.path), req.resp->code); } int main() { @@ -22,7 +19,9 @@ int main() { return 1; } - handle_path(&serv, CS("GET"), CS("/"), success); + handler *success_handler = handle_path(&serv, CS("GET"), CS("/"), success); + http_register_handler_middleware(&arena, success_handler, logging_func); + listen_and_serve(&serv); return 0; -- cgit v1.2.3