aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorspl3g <spleefer6@yandex.ru>2025-03-22 18:07:21 +0300
committerspl3g <spleefer6@yandex.ru>2025-03-22 18:07:21 +0300
commitef5f52fb739c9f0d71c579f08363ef0dfd5c227d (patch)
treea22c5cdbf4b897c070a8834a78b18dcfb18a3d88
parentad26c5cbb30bc1f5ed4f90909bc366953ae08b9f (diff)
Add a readme
-rw-r--r--README.org75
1 files changed, 75 insertions, 0 deletions
diff --git a/README.org b/README.org
new file mode 100644
index 0000000..93b3c94
--- /dev/null
+++ b/README.org
@@ -0,0 +1,75 @@
+* chttp
+This is not really a library, but a little project for my education. But if you really want to use it, you can.
+** Installation
+Just copy the lib directory and use the header files.
+** Usage
+*** Initializing the server
+#+begin_src c
+ arena arena = {0};
+ http_server serv = {0};
+ if (init_server(&arena, &serv, "127.0.0.1", "6969") != 0) {
+ return 1;
+ }
+#+end_src
+*** Adding handlers
+#+begin_src c
+ http_handler *hello_world_handler = http_handle_path(&serv, "GET", "/", hello_world);
+
+ void hello_world(http_request req) {
+ req.resp->code = OK;
+ req.resp->body = CS("Hello world!\n");
+ http_send(req);
+ }
+#+end_src
+*** Adding middleware
+#+begin_src c
+ http_register_global_middleware(&serv, logging_func);
+ // or
+ http_register_handler_middleware(&arena, hello_world_handler, logging_func);
+
+ void logging_func(http_middleware *self, http_request req) {
+ // you have to run http_run_next to run the next middleware
+ http_run_next(self, req);
+ // or you can just send the response
+ // http_send(req);
+
+ http_log(HTTP_INFO, CS_FMT" "CS_FMT": %ld\n", CS_ARG(req.method), CS_ARG(req.path), req.resp->code);
+ }
+#+end_src
+*** Listening for requests
+And to wrap things up, we need to run
+#+begin_src c
+ listen_and_serve(&serv);
+#+end_src
+*** Full example
+#+begin_src c
+ #include "lib/http.h"
+ #include "lib/const_strings.h"
+
+ void logging_func(http_middleware *self, http_request req) {
+ http_run_next(self, req);
+ http_log(HTTP_INFO, CS_FMT" "CS_FMT": %ld\n", CS_ARG(req.method), CS_ARG(req.path), req.resp->code);
+ }
+
+ void hello_world(http_request req) {
+ req.resp->code = OK;
+ req.resp->body = CS("Hello world!\n");
+ http_send(req);
+ }
+
+ int main() {
+ arena arena = {0};
+ http_server serv = {0};
+ if (init_server(&arena, &serv, "127.0.0.1", "6969") != 0) {
+ return 1;
+ }
+
+ http_handler *hello_world_handler = http_handle_path(&serv, "GET", "/", hello_world);
+ http_register_handler_middleware(&arena, hello_world_handler, logging_func);
+ http_register_global_middleware(&serv, logging_func);
+
+ listen_and_serve(&serv);
+
+ return 0;
+ }
+#+end_src