aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorspl3g <spleefer6@yandex.ru>2025-03-22 21:41:16 +0300
committerspl3g <spleefer6@yandex.ru>2025-03-22 21:41:16 +0300
commit62454373db4cd2add82ded0cf7b21f7d69ade597 (patch)
tree1627133fe4567e9a3abb707af5e7289353410d5f /examples
parentef5f52fb739c9f0d71c579f08363ef0dfd5c227d (diff)
Reorganize the lib
Diffstat (limited to 'examples')
-rw-r--r--examples/main.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/examples/main.c b/examples/main.c
new file mode 100644
index 0000000..c9cffb6
--- /dev/null
+++ b/examples/main.c
@@ -0,0 +1,34 @@
+#include <chttp/const_strings.h>
+#include <chttp/http.h>
+
+void success(http_request req) {
+ req.resp->code = OK;
+ req.resp->body = CS("Hello world!\n");
+ http_send(req);
+}
+
+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 whatever(http_middleware *self, http_request req) {
+ http_log(HTTP_INFO, "Whatever\n");
+ http_run_next(self, req);
+}
+
+int main() {
+ arena arena = {0};
+ http_server serv = {0};
+ if (init_server(&arena, &serv, "127.0.0.1", "6969") != 0) {
+ return 1;
+ }
+
+ http_register_global_middleware(&serv, logging_func);
+ http_handler *success_handler = http_handle_path(&serv, "GET", "/", success);
+ http_register_handler_middleware(&arena, success_handler, whatever);
+
+ listen_and_serve(&serv);
+
+ return 0;
+}