forked from lpereira/lwan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.lua
44 lines (38 loc) · 1.02 KB
/
test.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function handle_get_hello(req)
local name = req:query_param[[name]]
if name then
req:set_response("Hello, " .. name .. "!")
else
req:set_response("Hello, World!")
end
end
function handle_get_sse(req)
for i = 0, 10 do
req:send_event("counter-changed", "event" .. i)
end
end
function handle_get_chunked(req)
for i = 0, 10 do
req:say("Chunk #" .. i .. "\n")
end
end
function handle_get_random(req)
req:set_response("Random number: " .. math.random())
end
function string.starts(String, Start)
-- From http://lua-users.org/wiki/StringRecipes
return string.sub(String, 1, string.len(Start)) == Start
end
function is_get_handler(s, func)
if string.starts(s, "handle_get_") then
return type(func) == "function"
end
return false
end
function handle_get_root(req)
for key, value in pairs(_G) do
if is_get_handler(key, value) then
req:say("<li><a href=" .. string.sub(key, 12) .. ">" .. key .. "</a></li>\n")
end
end
end