From 5baef6aec75ac5ca18222150a538a5c8c6c01931 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 19 Aug 2023 16:42:12 +0200 Subject: [PATCH 1/1] Initial commit --- build.sh | 1 + src/main.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 build.sh create mode 100644 src/main.c diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..a7a014d --- /dev/null +++ b/build.sh @@ -0,0 +1 @@ +cc src/main.c -o out/main.exe -I ext ext/mongoose.c -lws2_32 \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..d26da8a --- /dev/null +++ b/src/main.c @@ -0,0 +1,59 @@ +#include "mongoose.h" + + +// HTML + +char * +html_user_post(struct mg_str user) { + static char html[1024]; + snprintf(html, 1024, + "" + "" + "" + "
" + "
" + " " + "
" + "" + "", + user.len, user.ptr); + return html; +} + +// Post + + + + +// User + + + + + +static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { + if (ev == MG_EV_HTTP_MSG) { + struct mg_http_message *hm = (struct mg_http_message *) ev_data; + if (mg_strcmp(hm->method, mg_str("post"))) { + printf("POST: %.*s\n", hm->body.len, hm->body.ptr); + } + if (mg_http_match_uri(hm, "/user/*/post")) { + struct mg_str caps[2]; + printf("uri: %.*s\n", hm->uri.len, hm->uri.ptr); + mg_match(hm->uri, mg_str("/user/*/post"), caps); + mg_http_reply(c, 200, "", html_user_post(caps[0])); + } + else { + mg_http_reply(c, 404, "", "Not found :/"); + } + } +} + +int main(int argc, char *argv[]) { + struct mg_mgr mgr; + mg_mgr_init(&mgr); // Init manager + mg_http_listen(&mgr, "http://0.0.0.0:8000", fn, &mgr); // Setup listener + for (;;) mg_mgr_poll(&mgr, 1000); // Event loop + mg_mgr_free(&mgr); // Cleanup + return 0; +} -- 2.50.1