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("") - for time, username, amount, comment in read_log() do - print(string.format("", time, escape(username), - amount / 100, escape(comment))) - end - print("
TimeUsernameAmountComment
%d%s%.02f€%s
") - end) +local function r_user() + if path == nil then + return respond_error("no path") end - return respond(200, "Users", function() - 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([[
@@ -132,3 +123,58 @@ elseif path ~= nil then end end) end + +local function r_log() + return respond(200, "Log", function() + print("") + print("") + for time, username, amount, comment in read_log() do + print(string.format("", time, escape(username), + amount / 100, escape(comment))) + end + print("
TimeUsernameAmountComment
%d%s%.02f€%s
") + end) +end + +local function r_index() + return respond(200, "Users", function() + print([[ + + +
+ +
+ ]]) + 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")