This commit is contained in:
metamuffin 2024-10-30 14:15:18 +01:00
parent 498cbfe9c8
commit a7868afbcf
No known key found for this signature in database
GPG key ID: 718F9749DCDBD654
2 changed files with 18 additions and 8 deletions

View file

@ -3,9 +3,14 @@
A _simpler_ trust based ledger. A _simpler_ trust based ledger.
The entire application is contained within `strichliste.lua`. This script The entire application is contained within `strichliste.lua`. This script
implements CGI. It was tested against Lua version 5.4.7. The "database" will be implements CGI. It was tested against Lua version 5.4.7. Application data is
written to a file named `log` in the working directory. stored in a number of files in the process working directory (See below).
The repository also contains a configuration file for the The repository also contains a configuration file for the
[gnix http server](https://codeberg.org/metamuffin/gnix) (`gnix.yaml`) which is [gnix http server](https://codeberg.org/metamuffin/gnix) (`gnix.yaml`) which is
useful for development or proxyless deployments. useful for development or proxyless deployments.
## Data Files
- `log` stores the transaction log as CSV (`time,user,amount,comment`)
- `products` stores the product list as CSV (`barcode,price,name`)

View file

@ -41,7 +41,9 @@ local stylesheet = [[
.amount-neg { color: red; } .amount-neg { color: red; }
nav h2 { display: inline-block } nav h2 { display: inline-block }
.notif { padding: 0.5em; margin: 0.5em; background-color: #ddd; } .notif { padding: 0.5em; margin: 0.5em; background-color: #ddd; }
.notif p { margin: 5px } .notif p { margin: 5px; }
form.box { border: 2px solid grey; padding: 0.5em; margin: 0.5em; display: inline-block; }
form h3 { margin: 5px; }
]] ]]
local script = [[ local script = [[
@ -160,7 +162,7 @@ local function r_user()
return respond_error("failed to open log") return respond_error("failed to open log")
end end
local time = os.time() local time = os.time()
log:write(string.format("%d,%s,%s,%s\n", time, username, amount, comment)) log:write(string.format("%d,%s,%d,%s\n", time, username, amount, comment))
log:flush() log:flush()
log:close() log:close()
notif = string.format( notif = string.format(
@ -185,7 +187,8 @@ local function r_user()
print(string.format("<p>Current balance: <span class=\"amount-%s\">%.02f€</p>", balance >= 0 and "pos" or "neg", print(string.format("<p>Current balance: <span class=\"amount-%s\">%.02f€</p>", balance >= 0 and "pos" or "neg",
balance / 100)) balance / 100))
print([[ print([[
<form class="transaction" action="" method="POST"> <form class="transaction box" action="" method="POST">
<h3>Create Transaction</h3>
<label for="amount">Amount: </label> <label for="amount">Amount: </label>
<input type="number" name="amount" id="amount" /><br/> <input type="number" name="amount" id="amount" /><br/>
<label for="comment">Comment: </label> <label for="comment">Comment: </label>
@ -216,8 +219,9 @@ local function r_log()
print("<table>") print("<table>")
print("<tr><th>Time</th><th>Username</th><th>Amount</th><th>Comment</th></tr>") print("<tr><th>Time</th><th>Username</th><th>Amount</th><th>Comment</th></tr>")
for time, username, amount, comment in read_log() do 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), print(string.format("<tr><td>%d</td><td>%s</td><td class=\"amount-%s\">%.02f€</td><td>%s</td></tr>", time,
amount / 100, escape(comment))) escape(username),
amount >= 0 and "pos" or "neg", amount / 100, escape(comment)))
end end
print("</table>") print("</table>")
end) end)
@ -226,7 +230,8 @@ end
local function r_index() local function r_index()
return respond(200, "Users", function() return respond(200, "Users", function()
print([[ print([[
<form action="/" method="GET"> <form action="/" method="GET" class="box">
<h3>User Creation</h3>
<label for="username">Username: </label> <label for="username">Username: </label>
<input type="text" name="create_user" id="username" /><br/> <input type="text" name="create_user" id="username" /><br/>
<input type="submit" value="Continue" /> <input type="submit" value="Continue" />