This commit is contained in:
ato 2023-03-23 18:10:59 +01:00
parent d55c8b1164
commit b5fb81f34d
7 changed files with 140 additions and 13 deletions

97
Cargo.lock generated
View File

@ -8,12 +8,27 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "base64"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "block-buffer"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cce20737498f97b993470a6e536b8523f0af7892a4f928cceb1ac5e52ebe7e"
dependencies = [
"generic-array",
]
[[package]] [[package]]
name = "bstr" name = "bstr"
version = "0.2.17" version = "0.2.17"
@ -51,6 +66,35 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cpufeatures"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28d997bd5e24a5928dd43e46dc529867e207907fe0b239c3477d924f7f2ca320"
dependencies = [
"libc",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "digest"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f"
dependencies = [
"block-buffer",
"crypto-common",
]
[[package]] [[package]]
name = "erased-serde" name = "erased-serde"
version = "0.3.24" version = "0.3.24"
@ -173,6 +217,16 @@ dependencies = [
"slab", "slab",
] ]
[[package]]
name = "generic-array"
version = "0.14.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9"
dependencies = [
"typenum",
"version_check",
]
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.8" version = "0.2.8"
@ -209,6 +263,31 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "headers"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3e372db8e5c0d213e0cd0b9be18be2aca3d44cf2fe30a9d46a65581cd454584"
dependencies = [
"base64",
"bitflags",
"bytes",
"headers-core",
"http",
"httpdate",
"mime",
"sha1",
]
[[package]]
name = "headers-core"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429"
dependencies = [
"http",
]
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.2.6" version = "0.2.6"
@ -624,6 +703,17 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "sha1"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f04293dc80c3993519f2d7f6f511707ee7094fe0c6d3406feb330cdb3540eba3"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]] [[package]]
name = "signal-hook-registry" name = "signal-hook-registry"
version = "1.4.0" version = "1.4.0"
@ -674,6 +764,7 @@ name = "tchoutchou"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"futures", "futures",
"headers",
"hyper", "hyper",
"matchit", "matchit",
"mime", "mime",
@ -799,6 +890,12 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "typenum"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba"
[[package]] [[package]]
name = "unicase" name = "unicase"
version = "2.6.0" version = "2.6.0"

View File

@ -19,6 +19,7 @@ panic = "abort"
[dependencies] [dependencies]
futures = { version = "0.3" } futures = { version = "0.3" }
headers = { version = "0.3" }
hyper = { version = "0.14", features = ["full"] } hyper = { version = "0.14", features = ["full"] }
matchit = { version = "0.7" } matchit = { version = "0.7" }
mlua = { version = "0.8", features = [ mlua = { version = "0.8", features = [

13
examples/ws.lua Normal file
View File

@ -0,0 +1,13 @@
local app = create_app()
app:ws("/ws", function(ws)
ws:on("open", function()
print(" client connected" .. ws.addr)
end)
ws:on("message", function(msg)
print("received message " .. msg)
end)
return ws
end)
app:run(3000)

View File

@ -21,7 +21,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
} }
}; };
let runtime = match Runtime::from_script(&lua_script) { let runtime = match Runtime::from_script(&lua_script).await {
Ok(runtime) => runtime, Ok(runtime) => runtime,
Err(error) => { Err(error) => {
eprintln!("{}", error); eprintln!("{}", error);

View File

@ -1,5 +1,6 @@
use hyper::Method; use hyper::Method;
use matchit::Router; use matchit::Router;
use mlua::{Function, Lua, Table};
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Clone)] #[derive(Clone)]
@ -14,12 +15,18 @@ pub struct Routers {
} }
impl Routers { impl Routers {
pub fn from(apps: HashMap<u16, (String, Vec<(Method, String, String)>)>) -> Self { pub async fn from(
apps: HashMap<u16, (String, Vec<(Method, String, String)>)>,
lua: &Lua,
) -> Self {
Self { Self {
routers: { routers: {
let mut routers: HashMap<u16, Router<LuaHandle>> = HashMap::new(); let mut routers: HashMap<u16, Router<LuaHandle>> = HashMap::new();
for (port, (_id, lua_paths)) in apps { for (port, (_id, lua_paths)) in apps {
for (method, path, handler_identifier) in lua_paths.iter() { for (method, path, handler_identifier) in lua_paths.iter() {
if method.as_str() == "CONNECT" {
// TODO: CONTINUE HERE
} else {
let lua_handle = LuaHandle { let lua_handle = LuaHandle {
method: method.to_string(), method: method.to_string(),
handler_identifier: handler_identifier.to_owned(), handler_identifier: handler_identifier.to_owned(),
@ -33,6 +40,7 @@ impl Routers {
} }
} }
} }
}
routers routers
}, },
} }

View File

@ -18,7 +18,7 @@ pub struct Runtime {
} }
impl Runtime { impl Runtime {
pub fn from_script(lua_script: &Vec<u8>) -> Result<Self, &str> { pub async fn from_script(lua_script: &Vec<u8>) -> Result<Self, &str> {
let lua = Rc::new(Lua::new()); let lua = Rc::new(Lua::new());
lua.globals().set("internal_app_counter", 0).unwrap(); lua.globals().set("internal_app_counter", 0).unwrap();
@ -48,7 +48,7 @@ impl Runtime {
Ok(Self { Ok(Self {
lua: lua.to_owned(), lua: lua.to_owned(),
routers: Routers::from(apps), routers: Routers::from(apps, &lua).await,
}) })
} }

View File

@ -25,6 +25,14 @@ impl UserData for App {
Ok(()) Ok(())
}); });
methods.add_method_mut("ws", |l, this, (path, cb): (String, Function)| {
let handler_identifier = format!("{}-ws-{}", this.id, path);
l.set_named_registry_value(&handler_identifier, cb)?;
this.handlers
.push((Method::CONNECT, path, handler_identifier));
Ok(())
});
methods.add_method_mut("use", |_l, this, (root, app): (String, App)| { methods.add_method_mut("use", |_l, this, (root, app): (String, App)| {
for (method, path, handler_identifier) in app.handlers { for (method, path, handler_identifier) in app.handlers {
let sub_path = if path == "/" { let sub_path = if path == "/" {