abouttreesummaryrefslogcommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorPatrick2023-08-19 16:42:12 +0200
committerPatrick2023-08-19 16:42:12 +0200
commit5baef6aec75ac5ca18222150a538a5c8c6c01931 (patch)
tree86362676426c111ddaab5b578903ce0f533b8575 /src/main.c
downloadchirp-5baef6aec75ac5ca18222150a538a5c8c6c01931.tar.gz
chirp-5baef6aec75ac5ca18222150a538a5c8c6c01931.zip
Initial commit
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c59
1 files changed, 59 insertions, 0 deletions
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;
+}