about summary refs log tree commit diff stats
path: root/lua/sandborb
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2024-06-05 22:59:15 -0400
committerelioat <elioat@tilde.institute>2024-06-05 22:59:15 -0400
commit883eb064e89b1502816d1692c48e5e3ea3d6184c (patch)
tree664476c582ed7af599ba0b74195dbec3241de962 /lua/sandborb
parenteef5ab306a9a5d2fee1c9828980b118184f530ac (diff)
downloadtour-883eb064e89b1502816d1692c48e5e3ea3d6184c.tar.gz
Exploring options to support POST requests
Diffstat (limited to 'lua/sandborb')
-rw-r--r--lua/sandborb/README.md3
-rwxr-xr-xlua/sandborb/build.sh2
-rw-r--r--lua/sandborb/route_handler.lua25
-rw-r--r--lua/sandborb/src/app.c14
4 files changed, 37 insertions, 7 deletions
diff --git a/lua/sandborb/README.md b/lua/sandborb/README.md
index e69de29..fe81506 100644
--- a/lua/sandborb/README.md
+++ b/lua/sandborb/README.md
@@ -0,0 +1,3 @@
+# sandborb
+
+Lua web fun playground.
\ No newline at end of file
diff --git a/lua/sandborb/build.sh b/lua/sandborb/build.sh
index dc90a60..9b38b4d 100755
--- a/lua/sandborb/build.sh
+++ b/lua/sandborb/build.sh
@@ -1,7 +1,7 @@
 #!/usr/bin/env sh
 
 if [ -f a.out ]; then
-    rm sandborb
+    rm a.out
 fi
 
 cc ./src/app.c ./src/sandbird.c -I /opt/homebrew/include/lua5.4 ./src/sandbird.h -std=c99 -pedantic -Wall -Wextra -llua
diff --git a/lua/sandborb/route_handler.lua b/lua/sandborb/route_handler.lua
index 77ec7f4..3b01362 100644
--- a/lua/sandborb/route_handler.lua
+++ b/lua/sandborb/route_handler.lua
@@ -72,4 +72,29 @@ function HANDLE_ROUTE(e, path)
   sb_send_status(e, response.code, response.status)
   sb_send_header(e, "Content-Type", response.content_type)
   return response.body
+end
+
+
+function HANDLE_POST_ROUTE(e, path)
+  local response = {
+    content_type = "text/plain",
+    body = ""
+  }
+
+  if path == "/echo" then
+    response = {
+      body = "server response: " .. path
+    }
+  else
+    response = error_404(e, path)
+  end
+
+  if not response.code then
+    response.status = "OK"
+    response.code = 200
+  end
+
+  sb_send_status(e, response.code, response.status)
+  sb_send_header(e, "Content-Type", response.content_type)
+  return response.body
 end
\ No newline at end of file
diff --git a/lua/sandborb/src/app.c b/lua/sandborb/src/app.c
index 1cfa6d0..e4ca3b7 100644
--- a/lua/sandborb/src/app.c
+++ b/lua/sandborb/src/app.c
@@ -44,10 +44,15 @@ static int event_handler(sb_Event *e) {
             return SB_RES_OK;
         }
 
-        /* call the Lua function */
-        lua_getglobal(L, "HANDLE_ROUTE"); /* all caps because it is a global function */
+        /* call the Lua function based on the request method */
+        if (strcmp(e->method, "POST") == 0) {
+            lua_getglobal(L, "HANDLE_POST_ROUTE");
+        } else {
+            lua_getglobal(L, "HANDLE_ROUTE");
+        }
+
         lua_pushlightuserdata(L, e); /* Pass the event as userdata to Lua */
-        lua_pushstring(L, e->path);
+        lua_pushstring(L, e->path); /* FIXME: find a way to use lau_pushstring to pass posted data into lua */
         if (lua_pcall(L, 2, 1, 0) != LUA_OK) {
             fprintf(stderr, "Failed to call Lua function: %s\n", lua_tostring(L, -1));
             lua_close(L);
@@ -90,6 +95,3 @@ int main(void) {
     sb_close_server(server);
     return EXIT_SUCCESS;
 }
-
-
-