-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is based on the tower-sessions crate, but provides a slightly simpler API (that should be extended in the following commits).
- Loading branch information
Showing
10 changed files
with
174 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "example-sessions" | ||
version = "0.1.0" | ||
publish = false | ||
description = "Sessions - Flareon example." | ||
edition = "2021" | ||
|
||
[dependencies] | ||
askama = "0.12.1" | ||
flareon = { path = "../../flareon" } | ||
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use askama::Template; | ||
use flareon::forms::Form; | ||
use flareon::middleware::SessionMiddleware; | ||
use flareon::request::{Request, RequestExt}; | ||
use flareon::response::{Response, ResponseExt}; | ||
use flareon::router::Route; | ||
use flareon::{reverse, Body, Error, FlareonApp, FlareonProject, StatusCode}; | ||
|
||
#[derive(Debug, Template)] | ||
#[template(path = "index.html")] | ||
struct IndexTemplate<'a> { | ||
request: &'a Request, | ||
name: String, | ||
} | ||
|
||
#[derive(Debug, Template)] | ||
#[template(path = "name.html")] | ||
struct NameTemplate<'a> { | ||
request: &'a Request, | ||
} | ||
|
||
#[derive(Debug, Form)] | ||
struct NameForm { | ||
#[form(opt(max_length = 100))] | ||
name: String, | ||
} | ||
|
||
async fn hello(request: Request) -> Result<Response, Error> { | ||
let name: String = request | ||
.session() | ||
.get("user_name") | ||
.await | ||
.expect("Invalid session value") | ||
.unwrap_or_default(); | ||
if name.is_empty() { | ||
return Ok(reverse!(request, "name")); | ||
} | ||
|
||
let template = IndexTemplate { | ||
request: &request, | ||
name, | ||
}; | ||
|
||
Ok(Response::new_html( | ||
StatusCode::OK, | ||
Body::fixed(template.render()?), | ||
)) | ||
} | ||
|
||
async fn name(mut request: Request) -> Result<Response, Error> { | ||
if request.method() == flareon::Method::POST { | ||
let name_form = NameForm::from_request(&mut request).await.unwrap(); | ||
request | ||
.session_mut() | ||
.insert("user_name", name_form.name) | ||
.await | ||
.unwrap(); | ||
|
||
return Ok(reverse!(request, "index")); | ||
} | ||
|
||
let template = NameTemplate { request: &request }; | ||
|
||
Ok(Response::new_html( | ||
StatusCode::OK, | ||
Body::fixed(template.render()?), | ||
)) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let hello_app = FlareonApp::builder() | ||
.urls([ | ||
Route::with_handler_and_name("/", hello, "index"), | ||
Route::with_handler_and_name("/name", name, "name"), | ||
]) | ||
.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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{% 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>Hello!</h1> | ||
<p>Your name is: {{ name }}</p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>Hello!</h1> | ||
<form id="todo-form" action="{{ flareon::reverse_str!(request, "name") }}" method="post"> | ||
<input type="text" id="name" name="name" placeholder="Please enter your name" required> | ||
<button type="submit">Submit</button> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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) | ||
} | ||
} | ||
|
||
// TODO: add Flareon ORM-based session store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters