Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
m4tx committed Sep 25, 2024
1 parent c04e7e3 commit 6612554
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
# Examples
"examples/hello-world",
"examples/todo-list",
"examples/sessions",
]
resolver = "2"

Expand Down
10 changes: 10 additions & 0 deletions examples/sessions/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"] }
35 changes: 35 additions & 0 deletions examples/sessions/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<Response, Error> {
request
.session_mut()
.insert("user_name", "XD")
.await
.unwrap();

Ok(Response::new_html(
StatusCode::OK,
Body::fixed("<h1>Hello Flareon!</h1>".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();
}
17 changes: 17 additions & 0 deletions examples/sessions/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% let request = request %}

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sessions example</title>
</head>
<body>
<h1>TODO List</h1>
<form id="todo-form" action="{{ flareon::reverse_str!(request, "add-todo") }}" method="post">
<input type="text" id="title" name="title" placeholder="Enter a new TODO" required>
<button type="submit">Add TODO</button>
</form>
</body>
</html>
9 changes: 6 additions & 3 deletions flareon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -279,7 +280,7 @@ pub struct FlareonProject<S> {
handler: S,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct FlareonProjectBuilder {
apps: Vec<FlareonApp>,
urls: Vec<Route>,
Expand All @@ -304,10 +305,12 @@ impl FlareonProjectBuilder {

#[must_use]
pub fn middleware<M: tower::Layer<RouterService>>(
self,
&mut self,
middleware: M,
) -> FlareonProjectBuilderWithMiddleware<M::Service> {
self.to_builder_with_middleware().middleware(middleware)
self.clone()
.to_builder_with_middleware()
.middleware(middleware)
}

/// Builds the Flareon project instance.
Expand Down
21 changes: 21 additions & 0 deletions flareon/src/middleware.rs
Original file line number Diff line number Diff line change
@@ -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<S> tower::Layer<S> for SessionMiddleware {
type Service = <SessionManagerLayer<MemoryStore> as tower::Layer<S>>::Service;

fn layer(&self, inner: S) -> Self::Service {
let session_store = MemoryStore::default();
let session_layer = SessionManagerLayer::new(session_store);
session_layer.layer(inner)
}
}

0 comments on commit 6612554

Please sign in to comment.