create user form

This commit is contained in:
metamuffin 2024-10-30 12:20:03 +01:00
parent 585dd5ac36
commit a2c773b04c
No known key found for this signature in database
GPG key ID: 718F9749DCDBD654

View file

@ -28,6 +28,12 @@ local function respond_error(message)
end) end)
end end
local function redirect(path)
print("Status: 307")
print(string.format("Location: %s", path))
print()
end
local function form_data() local function form_data()
local data = {} local data = {}
for pair in string.gmatch(io.read(), "([^&]+)") do for pair in string.gmatch(io.read(), "([^&]+)") do
@ -62,27 +68,10 @@ local function balances()
return users return users
end end
if path == "/" then local function r_user()
if query == "?log" then if path == nil then
return respond(200, "Log", function() return respond_error("no path")
print("<table>")
print("<tr><th>Time</th><th>Username</th><th>Amount</th><th>Comment</th></tr>")
for time, username, amount, comment in read_log() do
print(string.format("<tr><td>%d</td><td>%s</td><td>%.02f€</td><td>%s</td></tr>", time, escape(username),
amount / 100, escape(comment)))
end
print("</table>")
end)
end end
return respond(200, "Users", function()
print("<ul>")
for username, balance in balances() do
print(string.format("<li><a href=\"/%s\">%s</a>: %.02f€</li>", escape(username), escape(username),
balance / 100))
end
print("</ul>")
end)
elseif path ~= nil then
local username = path:sub(2) local username = path:sub(2)
if username:match("^[%w_-]+$") == nil then if username:match("^[%w_-]+$") == nil then
return respond_error("username invalid") return respond_error("username invalid")
@ -108,8 +97,10 @@ elseif path ~= nil then
log:close() log:close()
end end
respond(200, username, function() return respond(200, username, function()
print(string.format("<h1>%s</h1>", username)) print(string.format("<h1>%s</h1>", username))
local balance = balances()[username] or 0
print(string.format("Current balance: %.02f", balance / 100))
print([[ print([[
<form action="" method="POST"> <form action="" method="POST">
<label for="amount">Amount: </label> <label for="amount">Amount: </label>
@ -132,3 +123,58 @@ elseif path ~= nil then
end end
end) end)
end end
local function r_log()
return respond(200, "Log", function()
print("<table>")
print("<tr><th>Time</th><th>Username</th><th>Amount</th><th>Comment</th></tr>")
for time, username, amount, comment in read_log() do
print(string.format("<tr><td>%d</td><td>%s</td><td>%.02f€</td><td>%s</td></tr>", time, escape(username),
amount / 100, escape(comment)))
end
print("</table>")
end)
end
local function r_index()
return respond(200, "Users", function()
print([[
<form action="/" method="GET">
<label for="username">Username: </label>
<input type="text" name="create_user" id="username" /><br/>
<input type="submit" value="Create" />
</form>
]])
print("<ul>")
for username, balance in pairs(balances()) do
print(string.format("<li><a href=\"/%s\">%s</a>: %.02f€</li>", escape(username), escape(username),
balance / 100))
end
print("</ul>")
end)
end
local function r_create_user()
if query == nil then
return respond_error("no query")
end
local username = query:match("^\\?create_user=([%w_-]+)$")
if username == nil then
return respond_error("invalid username")
end
return redirect(string.format("/%s", username))
end
if path == "/" then
if query == "?log" then
return r_log()
elseif query ~= nil and query:match("^\\?create_user=") then
return r_create_user()
else
return r_index()
end
else
r_user()
end
return respond_error("unknown route")