Compare commits

..

No commits in common. "1c917722a6767f56fb7ed4a261745d80c4f6bd10" and "a634528d02d435baa158f14d1d50d05e8104fee0" have entirely different histories.

10 changed files with 52 additions and 81 deletions

View File

@ -1,13 +1,3 @@
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,10 +1,3 @@
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,21 +1,12 @@
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.test == nil then if req.parts.myfile == 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.test.content) fs:write("yololo.txt", req.parts.myfile.content)
return res return res
end) end)

View File

@ -1,18 +1,8 @@
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,25 +1,3 @@
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
]] ]]
@ -42,7 +20,9 @@ app:get("/:segment", function(req, res)
end) end)
app:post("/submit", function(req, res) app:post("/submit", function(req, res)
res.body = req.form.test .. " " .. req.form.test2 for k,v in pairs(req.form) do
print(k,v)
end
return res return res
end) end)

View File

@ -1,5 +1,4 @@
use mlua::UserData; use mlua::UserData;
use tokio::fs;
#[derive(Clone)] #[derive(Clone)]
pub struct FS {} pub struct FS {}
@ -9,7 +8,8 @@ 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 {
Ok(fs::write(filename, contents).await.unwrap()) tokio::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; use tokio::time::sleep;
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 {
time::sleep(Duration::from_secs(n)).await; sleep(Duration::from_secs(n)).await;
Ok(()) Ok(())
}) })
.unwrap(); .unwrap();

View File

@ -31,7 +31,6 @@ 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,7 +1,6 @@
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 {
@ -10,10 +9,16 @@ 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| Ok(this.filename = value)); fields.add_field_method_set("filename", |_, this, 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| Ok(this.content = value)); fields.add_field_method_set("content", |_, this, 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()));
} }
} }
@ -31,25 +36,45 @@ 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| Ok(this.body = value)); fields.add_field_method_set("body", |_, this, value| {
fields.add_field_method_get("body", |l, this| match from_str::<Value>(&this.body) { 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)?), Ok(value) => Ok(l.to_value(&value)?),
Err(_) => Ok(l.to_value(&this.body)?), Err(_) => Ok(l.to_value(&this.body)?),
}
}); });
fields.add_field_method_set("form", |_, this, value| Ok(this.form = value)); fields.add_field_method_set("form", |_, this, 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| Ok(this.headers = value)); fields.add_field_method_set("headers", |_, this, 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| Ok(this.method = value)); fields.add_field_method_set("method", |_, this, 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| Ok(this.params = value)); fields.add_field_method_set("params", |_, this, 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| Ok(this.parts = value)); fields.add_field_method_set("parts", |_, this, 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,7 +25,10 @@ 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| Ok(this.body = value)); fields.add_field_method_set("body", |_, this, 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| {