Compare commits

..

2 Commits

Author SHA1 Message Date
ato
b5fb81f34d wip 2023-03-23 18:10:59 +01:00
ato
d55c8b1164 chore: cleanup 2023-02-21 22:05:12 +01:00
8 changed files with 146 additions and 25 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"
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"

View File

@ -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 = [

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,
Err(error) => {
eprintln!("{}", error);

View File

@ -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<u16, (String, Vec<(Method, String, String)>)>) -> Self {
pub async fn from(
apps: HashMap<u16, (String, Vec<(Method, String, String)>)>,
lua: &Lua,
) -> Self {
Self {
routers: {
let mut routers: HashMap<u16, Router<LuaHandle>> = 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);
}
}
}
}

View File

@ -18,7 +18,7 @@ pub struct 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());
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,
})
}

View File

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

View File

@ -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 == "/" {