]> gitweb.ps.run Git - chirp/commitdiff
Initial commit
authorPatrick <patrick.schoenberger@posteo.de>
Sat, 19 Aug 2023 14:42:12 +0000 (16:42 +0200)
committerPatrick <patrick.schoenberger@posteo.de>
Sat, 19 Aug 2023 14:42:12 +0000 (16:42 +0200)
build.sh [new file with mode: 0644]
src/main.c [new file with mode: 0644]

diff --git a/build.sh b/build.sh
new file mode 100644 (file)
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 (file)
index 0000000..d26da8a
--- /dev/null
@@ -0,0 +1,59 @@
+#include "mongoose.h"\r
+\r
+\r
+// HTML\r
+\r
+char *\r
+html_user_post(struct mg_str user) {\r
+    static char html[1024];\r
+    snprintf(html, 1024,\r
+        "<!DOCTYPE html>"\r
+        "<html>"\r
+        "<body>"\r
+        "<form action=\"/user/%.*s/post\" method=\"post\">"\r
+        "    <input type=\"text\" id=\"content\" name=\"content\"><br>"\r
+        "    <input type=\"submit\" value=\"Chirp!\">"\r
+        "</form>"\r
+        "</body>"\r
+        "</html>",\r
+        user.len, user.ptr);\r
+    return html;\r
+}\r
+\r
+// Post\r
+\r
+\r
+\r
+\r
+// User\r
+\r
+\r
+\r
+\r
+\r
+static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {\r
+  if (ev == MG_EV_HTTP_MSG) {\r
+    struct mg_http_message *hm = (struct mg_http_message *) ev_data;\r
+    if (mg_strcmp(hm->method, mg_str("post"))) {\r
+        printf("POST: %.*s\n", hm->body.len, hm->body.ptr);\r
+    }\r
+    if (mg_http_match_uri(hm, "/user/*/post")) {\r
+        struct mg_str caps[2];\r
+        printf("uri: %.*s\n", hm->uri.len, hm->uri.ptr);\r
+        mg_match(hm->uri, mg_str("/user/*/post"), caps);\r
+        mg_http_reply(c, 200, "", html_user_post(caps[0]));\r
+    }\r
+    else {\r
+        mg_http_reply(c, 404, "", "Not found :/");\r
+    }\r
+  }\r
+}\r
+\r
+int main(int argc, char *argv[]) {\r
+  struct mg_mgr mgr;\r
+  mg_mgr_init(&mgr);                                      // Init manager\r
+  mg_http_listen(&mgr, "http://0.0.0.0:8000", fn, &mgr);  // Setup listener\r
+  for (;;) mg_mgr_poll(&mgr, 1000);                       // Event loop\r
+  mg_mgr_free(&mgr);                                      // Cleanup\r
+  return 0;\r
+}\r