From b5fb81f34d073c4680d679a480a7cf227939b4ac Mon Sep 17 00:00:00 2001 From: ato Date: Thu, 23 Mar 2023 18:10:59 +0100 Subject: [PATCH] wip --- Cargo.lock | 97 ++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + examples/ws.lua | 13 +++++ src/cli/main.rs | 2 +- src/internal/core/routers.rs | 28 +++++++---- src/internal/core/runtime.rs | 4 +- src/internal/lua/app.rs | 8 +++ 7 files changed, 140 insertions(+), 13 deletions(-) create mode 100644 examples/ws.lua diff --git a/Cargo.lock b/Cargo.lock index a799b1c..ae811ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,12 +8,27 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "bstr" version = "0.2.17" @@ -51,6 +66,35 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "erased-serde" version = "0.3.24" @@ -173,6 +217,16 @@ dependencies = [ "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]] name = "getrandom" version = "0.2.8" @@ -209,6 +263,31 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "hermit-abi" version = "0.2.6" @@ -624,6 +703,17 @@ dependencies = [ "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]] name = "signal-hook-registry" version = "1.4.0" @@ -674,6 +764,7 @@ name = "tchoutchou" version = "0.1.0" dependencies = [ "futures", + "headers", "hyper", "matchit", "mime", @@ -799,6 +890,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "typenum" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" + [[package]] name = "unicase" version = "2.6.0" diff --git a/Cargo.toml b/Cargo.toml index 9756a8c..27d3f9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ panic = "abort" [dependencies] futures = { version = "0.3" } +headers = { version = "0.3" } hyper = { version = "0.14", features = ["full"] } matchit = { version = "0.7" } mlua = { version = "0.8", features = [ diff --git a/examples/ws.lua b/examples/ws.lua new file mode 100644 index 0000000..2f164ab --- /dev/null +++ b/examples/ws.lua @@ -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) diff --git a/src/cli/main.rs b/src/cli/main.rs index aaf6136..4afbc8c 100644 --- a/src/cli/main.rs +++ b/src/cli/main.rs @@ -21,7 +21,7 @@ async fn main() -> Result<(), Box> { } }; - let runtime = match Runtime::from_script(&lua_script) { + let runtime = match Runtime::from_script(&lua_script).await { Ok(runtime) => runtime, Err(error) => { eprintln!("{}", error); diff --git a/src/internal/core/routers.rs b/src/internal/core/routers.rs index d73c1bf..f6daec2 100644 --- a/src/internal/core/routers.rs +++ b/src/internal/core/routers.rs @@ -1,5 +1,6 @@ use hyper::Method; use matchit::Router; +use mlua::{Function, Lua, Table}; use std::collections::HashMap; #[derive(Clone)] @@ -14,22 +15,29 @@ pub struct Routers { } impl Routers { - pub fn from(apps: HashMap)>) -> Self { + pub async fn from( + apps: HashMap)>, + lua: &Lua, + ) -> Self { Self { routers: { let mut routers: HashMap> = HashMap::new(); for (port, (_id, lua_paths)) in apps { for (method, path, handler_identifier) in lua_paths.iter() { - let lua_handle = LuaHandle { - method: method.to_string(), - handler_identifier: handler_identifier.to_owned(), - }; - if let Some(router) = routers.get_mut(&port) { - router.insert(path, lua_handle).unwrap(); + if method.as_str() == "CONNECT" { + // TODO: CONTINUE HERE } else { - let mut new_router = Router::new(); - new_router.insert(path, lua_handle).unwrap(); - routers.insert(port, new_router); + let lua_handle = LuaHandle { + method: method.to_string(), + handler_identifier: handler_identifier.to_owned(), + }; + if let Some(router) = routers.get_mut(&port) { + router.insert(path, lua_handle).unwrap(); + } else { + let mut new_router = Router::new(); + new_router.insert(path, lua_handle).unwrap(); + routers.insert(port, new_router); + } } } } diff --git a/src/internal/core/runtime.rs b/src/internal/core/runtime.rs index 7b87508..54ce182 100644 --- a/src/internal/core/runtime.rs +++ b/src/internal/core/runtime.rs @@ -18,7 +18,7 @@ pub struct Runtime { } impl Runtime { - pub fn from_script(lua_script: &Vec) -> Result { + pub async fn from_script(lua_script: &Vec) -> Result { let lua = Rc::new(Lua::new()); lua.globals().set("internal_app_counter", 0).unwrap(); @@ -48,7 +48,7 @@ impl Runtime { Ok(Self { lua: lua.to_owned(), - routers: Routers::from(apps), + routers: Routers::from(apps, &lua).await, }) } diff --git a/src/internal/lua/app.rs b/src/internal/lua/app.rs index 36abefa..714a744 100644 --- a/src/internal/lua/app.rs +++ b/src/internal/lua/app.rs @@ -25,6 +25,14 @@ impl UserData for App { 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)| { for (method, path, handler_identifier) in app.handlers { let sub_path = if path == "/" {