Files
GiteaTest/server.lua
Christian van Dijk 64ee67d10c 🎉 initial commit
2026-02-19 17:14:08 +01:00

48 lines
1.4 KiB
Lua

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