diff --git a/requirements.sh b/requirements.sh new file mode 100755 index 0000000..d8e03df --- /dev/null +++ b/requirements.sh @@ -0,0 +1 @@ +luarocks install luasocket diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..2161e70 --- /dev/null +++ b/run.sh @@ -0,0 +1 @@ +lua server.lua diff --git a/server.lua b/server.lua new file mode 100644 index 0000000..cd2adf2 --- /dev/null +++ b/server.lua @@ -0,0 +1,47 @@ +local socket = require("socket") + +local host = "localhost" +local port = 8080 +local server = assert(socket.bind(host, port)) +print("Server started on http://" .. host .. ":" .. port .. "/hello?name=YourName") + +-- Helper function to extract query parameters +local function get_query_param(url, key) + local pattern = key .. "=([^&]+)" + return url:match(pattern) +end + +while true do + local client = server:accept() + client:settimeout(10) + + local line, err = client:receive() + if not err then + -- Extract method and URI (e.g., "GET /hello?name=Raycast HTTP/1.1") + local method, uri = line:match("^(%a+)%s+(%S+)%s+HTTP") + + local response_body = "" + local status = "404 Not Found" + + -- Basic Routing: Check if path starts with /hello + if uri and uri:find("^/hello") then + status = "200 OK" + -- Extract 'name' param or default to 'User' + local name = get_query_param(uri, "name") or "User" + -- URL decoding for spaces (%20) + name = name:gsub("%%20", " ") + response_body = "Hello " .. name .. "!" + else + response_body = "Not Found" + end + + local response = "HTTP/1.1 " .. status .. "\r\n" .. + "Content-Type: text/plain\r\n" .. + "Content-Length: " .. #response_body .. "\r\n" .. + "Connection: close\r\n\r\n" .. + response_body + + client:send(response) + end + client:close() +end