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()
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()
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()
app:post("/", function(req,res)
if req.parts.myfile == nil then
app:post("/", function(req, res)
if req.parts.test == nil then
res.status = 400
res.body = "Missing myfile field"
return res
end
fs:write("yololo.txt", req.parts.myfile.content)
fs:write("yololo.txt", req.parts.test.content)
return res
end)

View File

@ -1,8 +1,18 @@
print([[
===============================================================================
examples:
===============================================================================
curl localhost:3000
===============================================================================
]])
local lib = require("require_lib")
local app = create_app()
app:get("/", function(req,res)
app:get("/", function(req, res)
return lib.identity(res)
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
]]
@ -20,9 +42,7 @@ app:get("/:segment", function(req, res)
end)
app:post("/submit", function(req, res)
for k,v in pairs(req.form) do
print(k,v)
end
res.body = req.form.test .. " " .. req.form.test2
return res
end)

View File

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

View File

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

View File

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

View File

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