From 980cc2d42dc8e16b152671d3a7258e32e3f912f1 Mon Sep 17 00:00:00 2001 From: Milkman337 Date: Tue, 20 Aug 2024 21:43:09 +0200 Subject: [PATCH 1/9] Remove the default add function --- src/lib.rs | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c1d2959..372c495 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,18 +1,3 @@ pub mod router; pub mod entities; pub mod utilities; - -pub fn add(left: usize, right: usize) -> usize { - left + right -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } -} -- 2.45.2 From 97d794655415f74b8b2832d87872b1af3e496843 Mon Sep 17 00:00:00 2001 From: Milkman337 Date: Tue, 20 Aug 2024 21:45:36 +0200 Subject: [PATCH 2/9] Update version --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dfa672a..8b0fd2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,7 +22,7 @@ checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" [[package]] name = "loadstar" -version = "0.0.6" +version = "0.0.7" dependencies = [ "tiny_http", ] diff --git a/Cargo.toml b/Cargo.toml index 633a256..3b909c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "loadstar" description = "A simple web framework for rust" repository = "https://git.kjan.de/jank/loadstar" -version = "0.0.6" +version = "0.0.7" license = "GPL-3.0-only" edition = "2021" -- 2.45.2 From 55fb3a196e3e89cb5b0261a990ea551dcb1ce1f4 Mon Sep 17 00:00:00 2001 From: Milkman337 Date: Tue, 20 Aug 2024 21:58:14 +0200 Subject: [PATCH 3/9] Add function to add a route --- src/router.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/router.rs b/src/router.rs index 941c9dd..9999ab5 100644 --- a/src/router.rs +++ b/src/router.rs @@ -22,5 +22,34 @@ impl Router { None => respond_not_found(request), } } + + pub fn add_route(&mut self, route: &str, route_function: fn(Request)) { + self.routes.insert(route.to_string(), route_function); + } +} + +#[cfg(test)] +mod tests { + use std::collections::HashMap; + + use tiny_http::Request; + + use super::Router; + + #[test] + fn test_add_route() { + let mut router = Router::new(); + + fn test_route(_request: Request) { + + } + + router.add_route("/test", test_route as fn(Request)); + + let mut expected = HashMap::new(); + expected.insert("/test".to_string(), test_route as fn(Request)); + + assert_eq!(router.routes, expected); + } } -- 2.45.2 From aaa062db31c0705d046a30adff38ca1d8b0ba2d8 Mon Sep 17 00:00:00 2001 From: Milkman337 Date: Tue, 20 Aug 2024 22:11:40 +0200 Subject: [PATCH 4/9] Add some more Response types --- Cargo.lock | 94 ++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/utilities/responses.rs | 53 ++++++++++++++++++++- 3 files changed, 148 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 8b0fd2d..653c8e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,10 +20,18 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + [[package]] name = "loadstar" version = "0.0.7" dependencies = [ + "ascii", + "maud", "tiny_http", ] @@ -33,6 +41,80 @@ version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +[[package]] +name = "maud" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df518b75016b4289cdddffa1b01f2122f4a49802c93191f3133f6dc2472ebcaa" +dependencies = [ + "itoa", + "maud_macros", +] + +[[package]] +name = "maud_macros" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa453238ec218da0af6b11fc5978d3b5c3a45ed97b722391a2a11f3306274e18" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6af063034fc1935ede7be0122941bafa9bacb949334d090b77ca98b5817c7d9" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "tiny_http" version = "0.12.0" @@ -44,3 +126,15 @@ dependencies = [ "httpdate", "log", ] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" diff --git a/Cargo.toml b/Cargo.toml index 3b909c8..0194bf1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,5 @@ edition = "2021" [dependencies] tiny_http = "0.12.0" +maud = "0.26.0" +ascii = "1.1.0" diff --git a/src/utilities/responses.rs b/src/utilities/responses.rs index b828ee7..ee7837a 100644 --- a/src/utilities/responses.rs +++ b/src/utilities/responses.rs @@ -1,7 +1,58 @@ -use tiny_http::{Request, Response}; +use ascii::AsciiString; +use maud::Markup; +use tiny_http::{Header, Request, Response}; +/// Returns a 404 response pub fn respond_not_found(request: Request) { let response = Response::from_string("Not Found").with_status_code(404); let _ = request.respond(response); } + +/// Renders html +pub fn respond_html(request: Request, content: Markup) { + let response = Response::from_string(content); + let response = response.with_header(Header { + field: "Content-Type".parse().unwrap(), + value: AsciiString::from_ascii("text/html; charset=utf8").unwrap(), + }); + + let _ = request.respond(response); +} + +/// Redirects the user to another route +pub fn respond_redirect(request: Request, new_route: &str) { + let response = Response::from_string("Redirect"); + let response = response.with_header(Header { + field: "Content-Type".parse().unwrap(), + value: AsciiString::from_ascii("text/html").unwrap(), + }); + let response = response.with_header(Header { + field: "Location".parse().unwrap(), + value: AsciiString::from_ascii(new_route).unwrap(), + }); + let response = response.with_status_code(302); + + let _ = request.respond(response); +} + +/// Redirects the user to another route while also adding a cookie +pub fn respond_redirect_cookie(request: Request, new_route: &str, cookie_name: &str, cookie_value: &str) { + let response = Response::from_string("Redirect") + .with_header(Header { + field: "Content-Type".parse().unwrap(), + value: AsciiString::from_ascii("text/html").unwrap(), + }) + .with_header(Header { + field: "Location".parse().unwrap(), + value: AsciiString::from_ascii(new_route).unwrap(), + }) + .with_header(Header { + field: "Set-Cookie".parse().unwrap(), + value: AsciiString::from_ascii(format!("{}={}", cookie_name, cookie_value)).unwrap(), + }) + .with_status_code(302); + + + let _ = request.respond(response); +} -- 2.45.2 From a392734d595b5dbb616b15819f6f3ef5974a1a03 Mon Sep 17 00:00:00 2001 From: Milkman337 Date: Tue, 20 Aug 2024 22:40:04 +0200 Subject: [PATCH 5/9] Add ability to render templates --- src/lib.rs | 1 + src/template.rs | 5 +++++ src/utilities/responses.rs | 13 +++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 src/template.rs diff --git a/src/lib.rs b/src/lib.rs index 372c495..530c2ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ pub mod router; pub mod entities; pub mod utilities; +pub mod template; diff --git a/src/template.rs b/src/template.rs new file mode 100644 index 0000000..d79428e --- /dev/null +++ b/src/template.rs @@ -0,0 +1,5 @@ +use maud::Markup; + +pub trait Template { + fn render(&self) -> Markup; +} diff --git a/src/utilities/responses.rs b/src/utilities/responses.rs index ee7837a..227e388 100644 --- a/src/utilities/responses.rs +++ b/src/utilities/responses.rs @@ -2,6 +2,8 @@ use ascii::AsciiString; use maud::Markup; use tiny_http::{Header, Request, Response}; +use crate::template::Template; + /// Returns a 404 response pub fn respond_not_found(request: Request) { let response = Response::from_string("Not Found").with_status_code(404); @@ -9,6 +11,17 @@ pub fn respond_not_found(request: Request) { let _ = request.respond(response); } +/// Renders a template +pub fn respond_template(request: Request, template: impl Template) { + let response = Response::from_string(template.render()) + .with_header(Header { + field: "Content-Type".parse().unwrap(), + value: AsciiString::from_ascii("text/html; charset=utf8").unwrap(), + }); + + let _ = request.respond(response); +} + /// Renders html pub fn respond_html(request: Request, content: Markup) { let response = Response::from_string(content); -- 2.45.2 From 132917c61682deeb3e0bbc7d61758d87cd83cf6a Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 21 Aug 2024 08:12:12 +0200 Subject: [PATCH 6/9] Add controllers --- src/controller.rs | 7 +++++++ src/lib.rs | 1 + src/router.rs | 10 +++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/controller.rs diff --git a/src/controller.rs b/src/controller.rs new file mode 100644 index 0000000..9caebea --- /dev/null +++ b/src/controller.rs @@ -0,0 +1,7 @@ +use std::collections::HashMap; + +use tiny_http::Request; + +pub trait Controller { + fn register_routes(&self) -> HashMap; +} diff --git a/src/lib.rs b/src/lib.rs index 530c2ac..d4c151f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ pub mod router; +pub mod controller; pub mod entities; pub mod utilities; pub mod template; diff --git a/src/router.rs b/src/router.rs index 9999ab5..6449880 100644 --- a/src/router.rs +++ b/src/router.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use tiny_http::Request; -use crate::{entities::request::Url, utilities::responses::respond_not_found}; +use crate::{controller::Controller, entities::request::Url, utilities::responses::respond_not_found}; pub struct Router { @@ -23,6 +23,14 @@ impl Router { } } + pub fn add_controller(&mut self, controller: impl Controller) { + let new_routes = controller.register_routes(); + let mut final_routes = self.routes.clone(); + final_routes.extend(new_routes); + + self.routes = final_routes; + } + pub fn add_route(&mut self, route: &str, route_function: fn(Request)) { self.routes.insert(route.to_string(), route_function); } -- 2.45.2 From 036c6f873e2d6fcbbbc483e927b75e957a558900 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 21 Aug 2024 12:42:52 +0200 Subject: [PATCH 7/9] Change logic of the router --- src/router.rs | 65 +++++---------------------------------------------- 1 file changed, 6 insertions(+), 59 deletions(-) diff --git a/src/router.rs b/src/router.rs index 6449880..f7a7c86 100644 --- a/src/router.rs +++ b/src/router.rs @@ -1,63 +1,10 @@ -use std::collections::HashMap; - use tiny_http::Request; -use crate::{controller::Controller, entities::request::Url, utilities::responses::respond_not_found}; +use crate::controller::Controller; - -pub struct Router { - routes: HashMap, +pub trait Router { + fn new() -> Self; + fn route(&self, request: Request); + fn add_controller(&mut self, controller: impl Controller); + fn add_route(&mut self, route: &str, route_function: fn(Request)); } - -impl Router { - pub fn new() -> Self { - let routes = HashMap::new(); - - Router { routes } - } - - pub async fn route(&self, request: Request) { - match self.routes.get(&request.get_url_without_parameters()) { - Some(handler) => handler(request), - None => respond_not_found(request), - } - } - - pub fn add_controller(&mut self, controller: impl Controller) { - let new_routes = controller.register_routes(); - let mut final_routes = self.routes.clone(); - final_routes.extend(new_routes); - - self.routes = final_routes; - } - - pub fn add_route(&mut self, route: &str, route_function: fn(Request)) { - self.routes.insert(route.to_string(), route_function); - } -} - -#[cfg(test)] -mod tests { - use std::collections::HashMap; - - use tiny_http::Request; - - use super::Router; - - #[test] - fn test_add_route() { - let mut router = Router::new(); - - fn test_route(_request: Request) { - - } - - router.add_route("/test", test_route as fn(Request)); - - let mut expected = HashMap::new(); - expected.insert("/test".to_string(), test_route as fn(Request)); - - assert_eq!(router.routes, expected); - } -} - -- 2.45.2 From 91cc658d862035816d5e1ae42a5828e07600be3f Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 21 Aug 2024 14:55:30 +0200 Subject: [PATCH 8/9] Add example and change some code --- Cargo.lock | 317 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + examples/hello_world.rs | 85 +++++++++++ src/controller.rs | 6 +- src/router.rs | 7 +- 5 files changed, 411 insertions(+), 5 deletions(-) create mode 100644 examples/hello_world.rs diff --git a/Cargo.lock b/Cargo.lock index 653c8e6..a3e3ffb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,18 +2,93 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "ascii" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + +[[package]] +name = "backtrace" +version = "0.3.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "bytes" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" + +[[package]] +name = "cc" +version = "1.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72db2f7947ecee9b03b510377e8bb9077afa27176fdbff55c51027e976fdcc48" +dependencies = [ + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "chunked_transfer" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e4de3bc4ea267985becf712dc6d9eed8b04c953b3fcfb339ebc87acd9804901" +[[package]] +name = "gimli" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" + +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + [[package]] name = "httpdate" version = "1.0.3" @@ -26,6 +101,12 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +[[package]] +name = "libc" +version = "0.2.158" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" + [[package]] name = "loadstar" version = "0.0.7" @@ -33,6 +114,17 @@ dependencies = [ "ascii", "maud", "tiny_http", + "tokio", +] + +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", ] [[package]] @@ -63,6 +155,71 @@ dependencies = [ "syn", ] +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "miniz_oxide" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" +dependencies = [ + "adler", +] + +[[package]] +name = "mio" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" +dependencies = [ + "hermit-abi", + "libc", + "wasi", + "windows-sys", +] + +[[package]] +name = "object" +version = "0.36.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27b64972346851a39438c60b341ebc01bba47464ae329e55cf343eb93964efd9" +dependencies = [ + "memchr", +] + +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -104,6 +261,58 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_syscall" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4" +dependencies = [ + "bitflags", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + +[[package]] +name = "socket2" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" +dependencies = [ + "libc", + "windows-sys", +] + [[package]] name = "syn" version = "2.0.75" @@ -127,6 +336,35 @@ dependencies = [ "log", ] +[[package]] +name = "tokio" +version = "1.39.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9babc99b9923bfa4804bd74722ff02c0381021eafa4db9949217e3be8e84fff5" +dependencies = [ + "backtrace", + "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys", +] + +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "unicode-ident" version = "1.0.12" @@ -138,3 +376,82 @@ name = "version_check" version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index 0194bf1..0f8eb9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ edition = "2021" tiny_http = "0.12.0" maud = "0.26.0" ascii = "1.1.0" +tokio = { version = "1.39.3", features = [ "full" ] } diff --git a/examples/hello_world.rs b/examples/hello_world.rs new file mode 100644 index 0000000..8227d0c --- /dev/null +++ b/examples/hello_world.rs @@ -0,0 +1,85 @@ +use std::{collections::HashMap, sync::Arc}; + +use loadstar::{controller::Controller, entities::request::Url, router::Router, template::Template, utilities::responses::{respond_not_found, respond_template}}; +use maud::html; +use tiny_http::Request; + + +struct MainRouter { + routes: HashMap +} + +impl Router for MainRouter { + fn new() -> Self { + MainRouter { + routes: HashMap::new(), + } + } + + fn route(&self, request: Request) { + match self.routes.get(&request.get_url_without_parameters()) { + Some(handler) => handler(request), + None => respond_not_found(request), + } + } + + fn add_route(&mut self, route: &str, route_function: fn(Request)) { + self.routes.insert(route.to_string(), route_function as fn(Request)); + } +} + +struct MainTemplate; + +impl Template for MainTemplate { + fn render(&self) -> maud::Markup { + html! { + h1 {"Hello World"} + } + } +} + +struct MainController; + +impl MainController { + fn index_route(request: Request) { + respond_template(request, MainTemplate {}) + } +} + +impl Controller for MainController { + fn register_routes(&self, router: &mut impl Router) { + router.add_route("/", MainController::index_route) + } +} + + +#[tokio::main] +async fn main() { + let server = tiny_http::Server::http("0.0.0.0:8000").unwrap(); + + let mut main_router = MainRouter::new(); + + + main_router.add_controller(MainController {}); + + + let router = Arc::new(main_router); + + loop { + let request = match server.recv() { + Ok(rq) => rq, + Err(e) => { + println!("error: {}", e); + break; + } + }; + + let router_clone = Arc::clone(&router); + + tokio::spawn(async move { + router_clone.route(request); + }); + } +} + + diff --git a/src/controller.rs b/src/controller.rs index 9caebea..8b5eb2a 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -1,7 +1,5 @@ -use std::collections::HashMap; - -use tiny_http::Request; +use crate::router::Router; pub trait Controller { - fn register_routes(&self) -> HashMap; + fn register_routes(&self, router: &mut impl Router); } diff --git a/src/router.rs b/src/router.rs index f7a7c86..486cbc1 100644 --- a/src/router.rs +++ b/src/router.rs @@ -5,6 +5,11 @@ use crate::controller::Controller; pub trait Router { fn new() -> Self; fn route(&self, request: Request); - fn add_controller(&mut self, controller: impl Controller); + fn add_controller(&mut self, controller: impl Controller) + where + Self: Sized, + { + controller.register_routes(self); + } fn add_route(&mut self, route: &str, route_function: fn(Request)); } -- 2.45.2 From 69d71d65885e1e31efca17930cd4058e07ec5456 Mon Sep 17 00:00:00 2001 From: jank Date: Wed, 21 Aug 2024 15:55:25 +0200 Subject: [PATCH 9/9] Add ability to run async functions in blocking mode --- examples/hello_world.rs | 8 ++++++-- src/utilities/async_block.rs | 12 ++++++++++++ src/utilities/mod.rs | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 src/utilities/async_block.rs diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 8227d0c..6ae4643 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -1,9 +1,12 @@ -use std::{collections::HashMap, sync::Arc}; +use std::{collections::HashMap, sync::Arc, thread::sleep, time::Duration}; -use loadstar::{controller::Controller, entities::request::Url, router::Router, template::Template, utilities::responses::{respond_not_found, respond_template}}; +use loadstar::{controller::Controller, entities::request::Url, router::Router, template::Template, utilities::{async_block::run_async, responses::{respond_not_found, respond_template}}}; use maud::html; use tiny_http::Request; +async fn example_async() { + sleep(Duration::from_secs(1)); +} struct MainRouter { routes: HashMap @@ -42,6 +45,7 @@ struct MainController; impl MainController { fn index_route(request: Request) { + run_async(example_async()); respond_template(request, MainTemplate {}) } } diff --git a/src/utilities/async_block.rs b/src/utilities/async_block.rs new file mode 100644 index 0000000..1c6b305 --- /dev/null +++ b/src/utilities/async_block.rs @@ -0,0 +1,12 @@ +use std::future::Future; + + +pub fn run_async(async_operation: impl Future) -> T { + // Use `block_in_place` to ensure runtime is dropped in a blocking context + tokio::task::block_in_place(|| { + // Get the current Tokio runtime handle + let handle = tokio::runtime::Handle::current(); + // Run the async operation using the handle and wait for it to complete + handle.block_on(async_operation) + }) +} diff --git a/src/utilities/mod.rs b/src/utilities/mod.rs index b396b03..ac00999 100644 --- a/src/utilities/mod.rs +++ b/src/utilities/mod.rs @@ -1 +1,2 @@ pub mod responses; +pub mod async_block; -- 2.45.2