diff --git a/strichliste.lua b/strichliste.lua
index 959cc87..f7bef9c 100755
--- a/strichliste.lua
+++ b/strichliste.lua
@@ -28,6 +28,12 @@ local function respond_error(message)
end)
end
+local function redirect(path)
+ print("Status: 307")
+ print(string.format("Location: %s", path))
+ print()
+end
+
local function form_data()
local data = {}
for pair in string.gmatch(io.read(), "([^&]+)") do
@@ -62,27 +68,10 @@ local function balances()
return users
end
-if path == "/" then
- if query == "?log" then
- return respond(200, "Log", function()
- print("
")
- print("Time | Username | Amount | Comment |
")
- for time, username, amount, comment in read_log() do
- print(string.format("%d | %s | %.02f€ | %s |
", time, escape(username),
- amount / 100, escape(comment)))
- end
- print("
")
- end)
+local function r_user()
+ if path == nil then
+ return respond_error("no path")
end
- return respond(200, "Users", function()
- print("")
- for username, balance in balances() do
- print(string.format("- %s: %.02f€
", escape(username), escape(username),
- balance / 100))
- end
- print("
")
- end)
-elseif path ~= nil then
local username = path:sub(2)
if username:match("^[%w_-]+$") == nil then
return respond_error("username invalid")
@@ -108,8 +97,10 @@ elseif path ~= nil then
log:close()
end
- respond(200, username, function()
+ return respond(200, username, function()
print(string.format("%s
", username))
+ local balance = balances()[username] or 0
+ print(string.format("Current balance: %.02f", balance / 100))
print([[
+ ]])
+ print("")
+ for username, balance in pairs(balances()) do
+ print(string.format("- %s: %.02f€
", escape(username), escape(username),
+ balance / 100))
+ end
+ print("
")
+ 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")