Compare commits

..

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

8 changed files with 25 additions and 146 deletions

97
Cargo.lock generated
View File

@ -8,27 +8,12 @@ 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"
@ -66,35 +51,6 @@ 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"
@ -217,16 +173,6 @@ 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"
@ -263,31 +209,6 @@ 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"
@ -703,17 +624,6 @@ 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"
@ -764,7 +674,6 @@ name = "tchoutchou"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"futures", "futures",
"headers",
"hyper", "hyper",
"matchit", "matchit",
"mime", "mime",
@ -890,12 +799,6 @@ 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,7 +19,6 @@ 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 = [

View File

@ -1,13 +0,0 @@
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).await { let runtime = match Runtime::from_script(&lua_script) {
Ok(runtime) => runtime, Ok(runtime) => runtime,
Err(error) => { Err(error) => {
eprintln!("{}", error); eprintln!("{}", error);

View File

@ -1,6 +1,5 @@
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)]
@ -15,29 +14,22 @@ pub struct Routers {
} }
impl Routers { impl Routers {
pub async fn from( pub fn from(apps: HashMap<u16, (String, Vec<(Method, String, String)>)>) -> Self {
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" { let lua_handle = LuaHandle {
// TODO: CONTINUE HERE 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 { } else {
let lua_handle = LuaHandle { let mut new_router = Router::new();
method: method.to_string(), new_router.insert(path, lua_handle).unwrap();
handler_identifier: handler_identifier.to_owned(), routers.insert(port, new_router);
};
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);
}
} }
} }
} }

View File

@ -18,7 +18,7 @@ pub struct Runtime {
} }
impl Runtime { impl Runtime {
pub async fn from_script(lua_script: &Vec<u8>) -> Result<Self, &str> { pub 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, &lua).await, routers: Routers::from(apps),
}) })
} }

View File

@ -38,7 +38,8 @@ impl Service<Request<Body>> for Svc {
let addr = self.1.clone(); let addr = self.1.clone();
let router = self.2.clone(); let router = self.2.clone();
Box::pin(async move { Box::pin(async move {
let lua_handle_match = match router.at(parts.uri.path()) { let uri = parts.uri.path().to_string();
let lua_handle_match = match router.at(uri.as_str()) {
Ok(res) => res, Ok(res) => res,
Err(_) => { Err(_) => {
return Ok(Response::builder() return Ok(Response::builder()
@ -84,16 +85,21 @@ impl Service<Request<Body>> for Svc {
let mut multipart = Multipart::with_body(Cursor::new(body_bytes), boundary); let mut multipart = Multipart::with_body(Cursor::new(body_bytes), boundary);
multipart multipart
.foreach_entry(|mut arg| { .foreach_entry(|mut arg| {
let mut content = String::new(); let mut buf = String::new();
let name = arg.headers.name; let name = arg.headers.name;
arg.data.read_to_string(&mut content).unwrap(); arg.data.read_to_string(&mut buf).unwrap();
match arg.headers.filename { match arg.headers.filename {
Some(filename) => { Some(filename) => {
parts_map parts_map.insert(
.insert(name.to_string(), Part { filename, content }); name.to_string(),
Part {
filename,
content: buf,
},
);
} }
None => { None => {
form_map.insert(name.to_string(), content); form_map.insert(name.to_string(), buf);
} }
}; };
() ()

View File

@ -25,14 +25,6 @@ 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 == "/" {