diff options
| author | Patrick | 2023-08-19 16:42:12 +0200 |
|---|---|---|
| committer | Patrick | 2023-08-19 16:42:12 +0200 |
| commit | 5baef6aec75ac5ca18222150a538a5c8c6c01931 (patch) | |
| tree | 86362676426c111ddaab5b578903ce0f533b8575 | |
| download | chirp-5baef6aec75ac5ca18222150a538a5c8c6c01931.tar.gz chirp-5baef6aec75ac5ca18222150a538a5c8c6c01931.zip | |
Initial commit
| -rw-r--r-- | build.sh | 1 | ||||
| -rw-r--r-- | src/main.c | 59 |
2 files changed, 60 insertions, 0 deletions
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,
+ "<!DOCTYPE html>"
+ "<html>"
+ "<body>"
+ "<form action=\"/user/%.*s/post\" method=\"post\">"
+ " <input type=\"text\" id=\"content\" name=\"content\"><br>"
+ " <input type=\"submit\" value=\"Chirp!\">"
+ "</form>"
+ "</body>"
+ "</html>",
+ 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;
+}
|
