-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from maxcountryman/feat/support-regenerate
initial sketch for `session.regenerate()` support
- Loading branch information
Showing
7 changed files
with
212 additions
and
33 deletions.
There are no files selected for viewing
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,4 @@ | ||
[workspace] | ||
members = ["*"] | ||
exclude = ["target"] | ||
resolver = "2" |
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 @@ | ||
[package] | ||
name = "example-regenerate" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
axum = "0.5.13" | ||
axum-sessions = { path = "../../" } | ||
|
||
[dependencies.rand] | ||
version = "0.8.5" | ||
features = ["min_const_gen"] | ||
|
||
[dependencies.tokio] | ||
version = "1.0" | ||
features = ["full"] |
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,44 @@ | ||
use axum::{routing::get, Router}; | ||
use axum_sessions::{ | ||
async_session::MemoryStore, | ||
extractors::{ReadableSession, WritableSession}, | ||
SessionLayer, | ||
}; | ||
use rand::Rng; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let store = MemoryStore::new(); | ||
let secret = rand::thread_rng().gen::<[u8; 128]>(); | ||
let session_layer = SessionLayer::new(store, &secret); | ||
|
||
async fn regenerate_handler(mut session: WritableSession) { | ||
// NB: This DOES NOT update the store, meaning that both sessions will still be | ||
// found. | ||
session.regenerate(); | ||
} | ||
|
||
async fn insert_handler(mut session: WritableSession) { | ||
session | ||
.insert("foo", 42) | ||
.expect("Could not store the answer."); | ||
} | ||
|
||
async fn handler(session: ReadableSession) -> String { | ||
session | ||
.get::<usize>("foo") | ||
.map(|answer| format!("{}", answer)) | ||
.unwrap_or_else(|| "Nothing in session yet; try /insert.".to_string()) | ||
} | ||
|
||
let app = Router::new() | ||
.route("/regenerate", get(regenerate_handler)) | ||
.route("/insert", get(insert_handler)) | ||
.route("/", get(handler)) | ||
.layer(session_layer); | ||
|
||
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) | ||
.serve(app.into_make_service()) | ||
.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,78 @@ | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
use axum::{ | ||
async_trait, | ||
extract::{FromRequest, RequestParts}, | ||
http, Extension, | ||
}; | ||
use tokio::sync::{OwnedRwLockReadGuard, OwnedRwLockWriteGuard}; | ||
|
||
use crate::SessionHandle; | ||
|
||
/// An extractor which provides a readable session. Sessions may have many | ||
/// readers. | ||
pub struct ReadableSession { | ||
session: OwnedRwLockReadGuard<async_session::Session>, | ||
} | ||
|
||
impl Deref for ReadableSession { | ||
type Target = OwnedRwLockReadGuard<async_session::Session>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.session | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<B> FromRequest<B> for ReadableSession | ||
where | ||
B: Send, | ||
{ | ||
type Rejection = std::convert::Infallible; | ||
|
||
async fn from_request(request: &mut RequestParts<B>) -> Result<Self, Self::Rejection> { | ||
let Extension(session_handle): Extension<SessionHandle> = Extension::from_request(request) | ||
.await | ||
.expect("Session extension missing. Is the session layer installed?"); | ||
let session = session_handle.read_owned().await; | ||
|
||
Ok(Self { session }) | ||
} | ||
} | ||
|
||
/// An extractor which provides a writable session. Sessions may have only one | ||
/// writer. | ||
pub struct WritableSession { | ||
session: OwnedRwLockWriteGuard<async_session::Session>, | ||
} | ||
|
||
impl Deref for WritableSession { | ||
type Target = OwnedRwLockWriteGuard<async_session::Session>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.session | ||
} | ||
} | ||
|
||
impl DerefMut for WritableSession { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.session | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<B> FromRequest<B> for WritableSession | ||
where | ||
B: Send, | ||
{ | ||
type Rejection = std::convert::Infallible; | ||
|
||
async fn from_request(request: &mut RequestParts<B>) -> Result<Self, Self::Rejection> { | ||
let Extension(session_handle): Extension<SessionHandle> = Extension::from_request(request) | ||
.await | ||
.expect("Session extension missing. Is the session layer installed?"); | ||
let session = session_handle.write_owned().await; | ||
|
||
Ok(Self { session }) | ||
} | ||
} |
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