Skip to content
This repository was archived by the owner on Nov 2, 2024. It is now read-only.

Commit 37f2f73

Browse files
committed
Bug fix & add pastebin example
1 parent 52a6846 commit 37f2f73

5 files changed

Lines changed: 69 additions & 4 deletions

File tree

cli/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ fn main() -> anyhow::Result<()> {
6767
info!("Starting abel-server v{ver} (dev mode)");
6868

6969
let abel_path = tempdir()?;
70+
info!("Abel working path: {}", abel_path.path().display().underline());
7071
let server_args = ServerArgs {
7172
config,
7273
abel_path: abel_path.path().into(),

core/src/lua/bootstrap.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function HttpError(obj)
99
detail = detail,
1010
}
1111
local result_mt = {
12-
__call = function(detail)
12+
__call = function(self, detail)
1313
return {
1414
status = status,
1515
error = error,

core/src/lua/fs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,13 @@ impl UserData for LuaFile {
250250

251251
methods.add_async_function("write", |lua, mut args: MultiValue| async move {
252252
let mut this = check_self_mut_async(lua, args.pop_front())?;
253-
for (i, x) in args.iter().cloned().enumerate().skip(1) {
253+
for (i, x) in args.iter().cloned().enumerate() {
254254
let type_name = x.type_name();
255255
let x = lua
256256
.coerce_string(x)
257257
.ok()
258258
.flatten()
259-
.ok_or_else(|| tag_error(lua, i, "string", type_name, 1))?;
259+
.ok_or_else(|| tag_error(lua, i + 1, "string", type_name, 1))?;
260260
if let Err(error) = this
261261
.with_borrowed_mut(|t| t.0.write_all(x.as_bytes()))
262262
.await

core/src/task/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl TaskContext {
4444
_ => continue,
4545
};
4646
if let Some(close) = close.transpose()? {
47-
close.call(v)?;
47+
let _ = close.call::<_, ()>(v);
4848
}
4949
}
5050
}

examples/pastebin.lua

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
-- A simple one-time pastebin.
2+
--
3+
-- This example will keep updating with upcoming new features of Abel.
4+
5+
local crypto = require "crypto"
6+
local fs = require "fs"
7+
8+
local method_not_allowed = HttpError {
9+
status = 405,
10+
error = "method not allowed",
11+
}
12+
13+
local function gen_uid()
14+
local template = "xxxxxxxx"
15+
return (string.gsub(template, "x", function(c)
16+
local v = crypto.thread_rng:gen_range(0, 0xf)
17+
return string.format('%x', v)
18+
end))
19+
end
20+
21+
function abel.start()
22+
fs.mkdir "files"
23+
end
24+
25+
abel.listen("/", function(req)
26+
if req.method ~= "POST" then
27+
error(method_not_allowed {
28+
allowed = { "POST" },
29+
got = req.method,
30+
})
31+
end
32+
33+
local content = req.body:to_string()
34+
local uid = gen_uid()
35+
36+
local file <close> = assert(io.open("files/" .. uid, "w"))
37+
assert(file:write(content))
38+
39+
return { uid = uid }
40+
end)
41+
42+
abel.listen("/:uid", function(req)
43+
if req.method ~= "GET" then
44+
error(method_not_allowed {
45+
allowed = { "GET" },
46+
got = req.method,
47+
})
48+
end
49+
50+
local uid = req.params.uid
51+
if #uid ~= 8 then
52+
error { status = 400, error = "invalid UID" }
53+
end
54+
55+
local path = "files/" .. uid
56+
local file = io.open(path)
57+
if not file then
58+
error { status = 404, error = "file not found" }
59+
end
60+
61+
-- This works on POSIX systems, but not Windows
62+
assert(os.remove(path))
63+
return file:into_stream()
64+
end)

0 commit comments

Comments
 (0)