about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2024-06-05 22:32:40 -0400
committerelioat <elioat@tilde.institute>2024-06-05 22:32:40 -0400
commitc5b5258160e96a16ba11d48d306c5c26c13ee0e7 (patch)
tree200b79644b079574f59d464ba05b582db2705770
parent969f1d142a5d5589f0336086777b69dcc75b9018 (diff)
downloadtour-c5b5258160e96a16ba11d48d306c5c26c13ee0e7.tar.gz
Refactoring and cleaning up
-rw-r--r--lua/sandborb/.gitignore1
-rw-r--r--lua/sandborb/README.md0
-rwxr-xr-xlua/sandborb/build.sh4
-rw-r--r--lua/sandborb/giblang.lua34
-rw-r--r--lua/sandborb/route_handler.lua65
-rw-r--r--lua/sandborb/src/app.c (renamed from lua/sandborb/app.c)0
-rw-r--r--lua/sandborb/src/sandbird.c (renamed from lua/sandborb/sandbird.c)0
-rw-r--r--lua/sandborb/src/sandbird.h (renamed from lua/sandborb/sandbird.h)0
8 files changed, 83 insertions, 21 deletions
diff --git a/lua/sandborb/.gitignore b/lua/sandborb/.gitignore
index 92e5c23..d284b97 100644
--- a/lua/sandborb/.gitignore
+++ b/lua/sandborb/.gitignore
@@ -1,3 +1,4 @@
+/sandborb
 /a.out
 /.vscode
 *.gch
\ No newline at end of file
diff --git a/lua/sandborb/README.md b/lua/sandborb/README.md
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lua/sandborb/README.md
diff --git a/lua/sandborb/build.sh b/lua/sandborb/build.sh
index 28f1724..dc90a60 100755
--- a/lua/sandborb/build.sh
+++ b/lua/sandborb/build.sh
@@ -1,10 +1,10 @@
 #!/usr/bin/env sh
 
 if [ -f a.out ]; then
-    rm a.out
+    rm sandborb
 fi
 
-cc app.c sandbird.c -I /opt/homebrew/include/lua5.4 sandbird.h -std=c99 -pedantic -Wall -Wextra -llua
+cc ./src/app.c ./src/sandbird.c -I /opt/homebrew/include/lua5.4 ./src/sandbird.h -std=c99 -pedantic -Wall -Wextra -llua
 
 if [ -f a.out ]; then
     ./a.out
diff --git a/lua/sandborb/giblang.lua b/lua/sandborb/giblang.lua
new file mode 100644
index 0000000..d1ab3a2
--- /dev/null
+++ b/lua/sandborb/giblang.lua
@@ -0,0 +1,34 @@
+local giblang = {_VERSION = "2024.07"}
+local cons = {"b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "v", "w", "z", "ch", "sh", "zh", "th"}
+local vow = {"a", "e", "i", "o", "u", "y", "ee", "ai", "ae", "au"}
+
+math.randomseed(os.time())
+
+function giblang.rpick(t)
+    return t[math.floor(math.random() * #t) + 1]
+end
+
+function giblang.syl()
+    return giblang.rpick(cons) .. giblang.rpick(vow)
+end
+
+function giblang.word()
+    local str = ""
+    for i=1, math.floor(math.random() * 3) + 1 do
+        str = str .. giblang.syl()
+    end
+    if math.random() > 0.2 then
+        str = str .. giblang.rpick(cons)
+    end
+    return str
+end
+
+function giblang.speak(number_of_words)
+    local words = ""
+    for i=1,number_of_words do
+        words = words .. giblang.word() .. " "
+    end
+    return words
+end
+
+return giblang
diff --git a/lua/sandborb/route_handler.lua b/lua/sandborb/route_handler.lua
index ae69875..7819d3d 100644
--- a/lua/sandborb/route_handler.lua
+++ b/lua/sandborb/route_handler.lua
@@ -1,37 +1,64 @@
 local json = require "json"
+local giblang = require "giblang"
 
 local function brackets(e)
-  sb_send_status(e, 200, "OK")
-  sb_send_header(e, "Content-Type", "application/json")
-  return json.encode({ 1, 2, 3, { x = 10 } })
+  return {
+    content_type = "application/json",
+    body = json.encode({ 1, 2, 3, { x = 10 } })
+  }
 end
 
 local function banana(e)
-  sb_send_status(e, 200, "OK")
-  sb_send_header(e, "Content-Type", "text/html, charset=utf-8")
-  return "<h1>Bananas are delicious!</h1>"
+  return {
+    content_type = "text/html, charset=utf-8",
+    body = "<h1>Bananas are delicious!</h1>"
+  }
 end
 
-local function error_404(e,p)
-  sb_send_status(e, 404, "Not Found")
-  sb_send_header(e, "Content-Type", "text/html, charset=utf-8")
-  return "<h1>404 - Page not found: " .. p .. "</h1>"
+local function gib(e,n)
+  return {
+    content_type = "text/html, charset=utf-8",
+    body = "<h1>" .. giblang.speak(n) .. "</h1>"
+  }
+end
+
+local function error_404(e, p)
+  return {
+    code = 404,
+    status = "Not Found",
+    content_type = "text/html, charset=utf-8",
+    body = "<h1>404 - Page not found: " .. p .. "</h1>"
+  }
 end
 
 function HANDLE_ROUTE(e, path)
+  local response = {
+    content_type = "text/html, charset=utf-8",
+    body = ""
+  }
+
   if path == "/" then
-    sb_send_status(e, 200, "OK")
-    sb_send_header(e, "Content-Type", "text/html, charset=utf-8")  
-    return "Hello, you are at the root!"
+    response.body = "Hello, you are at the root!"
   elseif path == "/about" then
-    sb_send_status(e, 200, "OK")
-    sb_send_header(e, "Content-Type", "text/plain")  
-    return "This is the about page!"
+    response.body = "This is the about page!"
   elseif path == "/banana" then
-    return banana(e)
+    response = banana(e)
   elseif path == "/json" then
-    return brackets(e)
+    response = brackets(e)
+  elseif path == "/giblang" then
+    math.randomseed(os.time())
+    local number_of_words = math.random(2, 10)
+    response = gib(e, number_of_words)
   else
-    return error_404(e, path)
+    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/app.c b/lua/sandborb/src/app.c
index 1cfa6d0..1cfa6d0 100644
--- a/lua/sandborb/app.c
+++ b/lua/sandborb/src/app.c
diff --git a/lua/sandborb/sandbird.c b/lua/sandborb/src/sandbird.c
index e08c287..e08c287 100644
--- a/lua/sandborb/sandbird.c
+++ b/lua/sandborb/src/sandbird.c
diff --git a/lua/sandborb/sandbird.h b/lua/sandborb/src/sandbird.h
index e1c0e7c..e1c0e7c 100644
--- a/lua/sandborb/sandbird.h
+++ b/lua/sandborb/src/sandbird.h