From 66125543fcf0a697321473441439fbcf86bd9d70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Ma=C4=87kowski?= Date: Wed, 25 Sep 2024 08:03:04 +0200 Subject: [PATCH] WIP --- Cargo.lock | 8 ++++++ Cargo.toml | 1 + examples/sessions/Cargo.toml | 10 ++++++++ examples/sessions/src/main.rs | 35 ++++++++++++++++++++++++++ examples/sessions/templates/index.html | 17 +++++++++++++ flareon/src/lib.rs | 9 ++++--- flareon/src/middleware.rs | 21 ++++++++++++++++ 7 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 examples/sessions/Cargo.toml create mode 100644 examples/sessions/src/main.rs create mode 100644 examples/sessions/templates/index.html create mode 100644 flareon/src/middleware.rs diff --git a/Cargo.lock b/Cargo.lock index 86ae62a..7f8473e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -751,6 +751,14 @@ dependencies = [ "tokio", ] +[[package]] +name = "example-sessions" +version = "0.1.0" +dependencies = [ + "flareon", + "tokio", +] + [[package]] name = "example-todo-list" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index d82e42f..9132197 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ members = [ # Examples "examples/hello-world", "examples/todo-list", + "examples/sessions", ] resolver = "2" diff --git a/examples/sessions/Cargo.toml b/examples/sessions/Cargo.toml new file mode 100644 index 0000000..f8d325d --- /dev/null +++ b/examples/sessions/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "example-sessions" +version = "0.1.0" +publish = false +description = "Sessions - Flareon example." +edition = "2021" + +[dependencies] +flareon = { path = "../../flareon" } +tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] } diff --git a/examples/sessions/src/main.rs b/examples/sessions/src/main.rs new file mode 100644 index 0000000..f96568c --- /dev/null +++ b/examples/sessions/src/main.rs @@ -0,0 +1,35 @@ +use flareon::middleware::SessionMiddleware; +use flareon::request::{Request, RequestExt}; +use flareon::response::{Response, ResponseExt}; +use flareon::router::Route; +use flareon::{Body, Error, FlareonApp, FlareonProject, StatusCode}; + +async fn return_hello(mut request: Request) -> Result { + request + .session_mut() + .insert("user_name", "XD") + .await + .unwrap(); + + Ok(Response::new_html( + StatusCode::OK, + Body::fixed("

Hello Flareon!

".as_bytes().to_vec()), + )) +} + +#[tokio::main] +async fn main() { + let hello_app = FlareonApp::builder() + .urls([Route::with_handler("/", return_hello)]) + .build() + .unwrap(); + + let flareon_project = FlareonProject::builder() + .register_app_with_views(hello_app, "") + .middleware(SessionMiddleware::new()) + .build(); + + flareon::run(flareon_project, "127.0.0.1:8000") + .await + .unwrap(); +} diff --git a/examples/sessions/templates/index.html b/examples/sessions/templates/index.html new file mode 100644 index 0000000..b25fc54 --- /dev/null +++ b/examples/sessions/templates/index.html @@ -0,0 +1,17 @@ +{% let request = request %} + + + + + + + Sessions example + + +

TODO List

+
+ + +
+ + diff --git a/flareon/src/lib.rs b/flareon/src/lib.rs index ddb5f89..5624df5 100644 --- a/flareon/src/lib.rs +++ b/flareon/src/lib.rs @@ -20,6 +20,7 @@ mod headers; #[doc(hidden)] #[path = "private.rs"] pub mod __private; +pub mod middleware; pub mod request; pub mod response; pub mod router; @@ -279,7 +280,7 @@ pub struct FlareonProject { handler: S, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct FlareonProjectBuilder { apps: Vec, urls: Vec, @@ -304,10 +305,12 @@ impl FlareonProjectBuilder { #[must_use] pub fn middleware>( - self, + &mut self, middleware: M, ) -> FlareonProjectBuilderWithMiddleware { - self.to_builder_with_middleware().middleware(middleware) + self.clone() + .to_builder_with_middleware() + .middleware(middleware) } /// Builds the Flareon project instance. diff --git a/flareon/src/middleware.rs b/flareon/src/middleware.rs new file mode 100644 index 0000000..2ebb3ef --- /dev/null +++ b/flareon/src/middleware.rs @@ -0,0 +1,21 @@ +use tower_sessions::{MemoryStore, SessionManagerLayer}; + +#[derive(Debug, Copy, Clone)] +pub struct SessionMiddleware {} + +impl SessionMiddleware { + #[must_use] + pub fn new() -> Self { + Self {} + } +} + +impl tower::Layer for SessionMiddleware { + type Service = as tower::Layer>::Service; + + fn layer(&self, inner: S) -> Self::Service { + let session_store = MemoryStore::default(); + let session_layer = SessionManagerLayer::new(session_store); + session_layer.layer(inner) + } +}