Compare commits

...

2 Commits

Author SHA1 Message Date
ato
1c917722a6 style: cleanup
All checks were successful
continuous-integration/drone/push Build is passing
2023-02-18 23:32:50 +01:00
ato
a7e7972033 docs: add curl examples 2023-02-18 23:31:58 +01:00
10 changed files with 81 additions and 52 deletions

View File

@ -1,3 +1,13 @@
print([[
===============================================================================
examples:
===============================================================================
curl localhost:3000
=> Hello, World!
===============================================================================
]])
local app = create_app() local app = create_app()
app:get("/", function(req, res) app:get("/", function(req, res)

View File

@ -1,3 +1,10 @@
print([[
===============================================================================
Open your browser and go to http://localhost:3000
===============================================================================
]])
local app = create_app() local app = create_app()
app:get("/", function(req, res) app:get("/", function(req, res)

View File

@ -1,12 +1,21 @@
print([[
===============================================================================
examples:
===============================================================================
curl -F "test=@README.md" localhost:3000/submit
===============================================================================
]])
local app = create_app() local app = create_app()
app:post("/", function(req,res) app:post("/", function(req, res)
if req.parts.myfile == nil then if req.parts.test == nil then
res.status = 400 res.status = 400
res.body = "Missing myfile field" res.body = "Missing myfile field"
return res return res
end end
fs:write("yololo.txt", req.parts.myfile.content) fs:write("yololo.txt", req.parts.test.content)
return res return res
end) end)

View File

@ -1,8 +1,18 @@
print([[
===============================================================================
examples:
===============================================================================
curl localhost:3000
===============================================================================
]])
local lib = require("require_lib") local lib = require("require_lib")
local app = create_app() local app = create_app()
app:get("/", function(req,res) app:get("/", function(req, res)
return lib.identity(res) return lib.identity(res)
end) end)

View File

@ -1,3 +1,25 @@
print([[
===============================================================================
examples:
===============================================================================
curl localhost:3000
=> Not Found
curl localhost:3000/42
=> {"data":"42"}
curl -d "test=hello&test2=world" localhost:3000/submit
=> hello world
curl localhost:3000/admin
=> Admin
curl localhost:3001
=> Admin
===============================================================================
]])
--[[ --[[
Admin Section Admin Section
]] ]]
@ -20,9 +42,7 @@ app:get("/:segment", function(req, res)
end) end)
app:post("/submit", function(req, res) app:post("/submit", function(req, res)
for k,v in pairs(req.form) do res.body = req.form.test .. " " .. req.form.test2
print(k,v)
end
return res return res
end) end)

View File

@ -1,4 +1,5 @@
use mlua::UserData; use mlua::UserData;
use tokio::fs;
#[derive(Clone)] #[derive(Clone)]
pub struct FS {} pub struct FS {}
@ -8,8 +9,7 @@ impl UserData for FS {
methods.add_async_method( methods.add_async_method(
"write", "write",
|_l, _this, (filename, contents): (String, String)| async move { |_l, _this, (filename, contents): (String, String)| async move {
tokio::fs::write(filename, contents).await.unwrap(); Ok(fs::write(filename, contents).await.unwrap())
Ok(())
}, },
) )
} }

View File

@ -2,14 +2,14 @@ pub mod fs;
use mlua::Lua; use mlua::Lua;
use std::time::Duration; use std::time::Duration;
use tokio::time::sleep; use tokio::time;
use self::fs::FS; use self::fs::FS;
pub fn load_builtins(lua: &Lua) { pub fn load_builtins(lua: &Lua) {
let sleep = lua let sleep = lua
.create_async_function(|_l, n: u64| async move { .create_async_function(|_l, n: u64| async move {
sleep(Duration::from_secs(n)).await; time::sleep(Duration::from_secs(n)).await;
Ok(()) Ok(())
}) })
.unwrap(); .unwrap();

View File

@ -31,6 +31,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
runtime.load(&load_builtins); runtime.load(&load_builtins);
runtime.load(&load_apis); runtime.load(&load_apis);
runtime.start().await; runtime.start().await;
Ok(()) Ok(())

View File

@ -1,6 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use mlua::{LuaSerdeExt, UserData, UserDataFields}; use mlua::{LuaSerdeExt, UserData, UserDataFields};
use serde_json::{from_str, Value};
#[derive(Clone)] #[derive(Clone)]
pub struct Part { pub struct Part {
@ -9,16 +10,10 @@ pub struct Part {
} }
impl UserData for Part { impl UserData for Part {
fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) { fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_set("filename", |_, this, value| { fields.add_field_method_set("filename", |_, this, value| Ok(this.filename = value));
this.filename = value;
Ok(())
});
fields.add_field_method_get("filename", |_, this| Ok(this.filename.to_owned())); fields.add_field_method_get("filename", |_, this| Ok(this.filename.to_owned()));
fields.add_field_method_set("content", |_, this, value| { fields.add_field_method_set("content", |_, this, value| Ok(this.content = value));
this.content = value;
Ok(())
});
fields.add_field_method_get("content", |_, this| Ok(this.content.to_owned())); fields.add_field_method_get("content", |_, this| Ok(this.content.to_owned()));
} }
} }
@ -36,45 +31,25 @@ pub struct Req {
impl UserData for Req { impl UserData for Req {
fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) { fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_set("body", |_, this, value| { fields.add_field_method_set("body", |_, this, value| Ok(this.body = value));
this.body = value; fields.add_field_method_get("body", |l, this| match from_str::<Value>(&this.body) {
Ok(()) Ok(value) => Ok(l.to_value(&value)?),
}); Err(_) => Ok(l.to_value(&this.body)?),
fields.add_field_method_get("body", |l, this| {
match serde_json::from_str::<serde_json::Value>(&this.body) {
Ok(value) => Ok(l.to_value(&value)?),
Err(_) => Ok(l.to_value(&this.body)?),
}
}); });
fields.add_field_method_set("form", |_, this, value| { fields.add_field_method_set("form", |_, this, value| Ok(this.form = value));
this.form = value;
Ok(())
});
fields.add_field_method_get("form", |_, this| Ok(this.form.to_owned())); fields.add_field_method_get("form", |_, this| Ok(this.form.to_owned()));
fields.add_field_method_set("headers", |_, this, value| { fields.add_field_method_set("headers", |_, this, value| Ok(this.headers = value));
this.headers = value;
Ok(())
});
fields.add_field_method_get("headers", |_, this| Ok(this.headers.to_owned())); fields.add_field_method_get("headers", |_, this| Ok(this.headers.to_owned()));
fields.add_field_method_set("method", |_, this, value| { fields.add_field_method_set("method", |_, this, value| Ok(this.method = value));
this.method = value;
Ok(())
});
fields.add_field_method_get("method", |_, this| Ok(this.method.to_owned())); fields.add_field_method_get("method", |_, this| Ok(this.method.to_owned()));
fields.add_field_method_set("params", |_, this, value| { fields.add_field_method_set("params", |_, this, value| Ok(this.params = value));
this.params = value;
Ok(())
});
fields.add_field_method_get("params", |_, this| Ok(this.params.to_owned())); fields.add_field_method_get("params", |_, this| Ok(this.params.to_owned()));
fields.add_field_method_set("parts", |_, this, value| { fields.add_field_method_set("parts", |_, this, value| Ok(this.parts = value));
this.parts = value;
Ok(())
});
fields.add_field_method_get("parts", |_, this| Ok(this.parts.to_owned())); fields.add_field_method_get("parts", |_, this| Ok(this.parts.to_owned()));
} }
} }

View File

@ -25,10 +25,7 @@ pub struct Res {
impl UserData for Res { impl UserData for Res {
fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) { fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_set("body", |_, this, value| { fields.add_field_method_set("body", |_, this, value| Ok(this.body = value));
this.body = value;
Ok(())
});
fields.add_field_method_get("body", |_, this| Ok(this.body.to_owned())); fields.add_field_method_get("body", |_, this| Ok(this.body.to_owned()));
fields.add_field_method_set("json", |_, this, value| { fields.add_field_method_set("json", |_, this, value| {