aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c25
1 files changed, 12 insertions, 13 deletions
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;