Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sbihel committed Sep 11, 2023
1 parent 079da64 commit c665ac6
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -37,7 +37,7 @@ features = ["sync"]
http = "0.2.8"
hyper = "0.14.19"
serde = "1.0.147"
serde_json = "1.0.93"
serde_json = "1.0.106"

[dev-dependencies.rand]
version = "0.8.5"
Expand Down
4 changes: 4 additions & 0 deletions examples/Cargo.toml
Expand Up @@ -2,3 +2,7 @@
members = ["*"]
exclude = ["target"]
resolver = "2"

[workspace.dependencies.async-session-memory-store]
git = "https://github.com/http-rs/async-session"
rev = "35cb0998f91b81b133c3314414adae4019a62741"
7 changes: 4 additions & 3 deletions examples/async-sqlx-session/Cargo.toml
Expand Up @@ -9,7 +9,9 @@ axum = "0.6.0"
axum-sessions = { path = "../../" }

[dependencies.async-sqlx-session]
version = "0.4.0"
# version = "0.4.0"
git = "https://github.com/sbihel/async-sqlx-session"
rev = "9ed73b641cb5e5c0c08b9d29dd723b3c8e57993d"
default-features = false
features = ["sqlite"]

Expand All @@ -18,8 +20,7 @@ version = "0.8.5"
features = ["min_const_gen"]

[dependencies.sqlx]
version = "0.5.13"
default-features = false
version = "0.7.1"
features = ["runtime-tokio-rustls", "sqlite"]

[dependencies.tokio]
Expand Down
9 changes: 3 additions & 6 deletions examples/async-sqlx-session/src/main.rs
Expand Up @@ -6,10 +6,7 @@

use async_sqlx_session::SqliteSessionStore;
use axum::{routing::get, Router};
use axum_sessions::{
extractors::{ReadableSession, WritableSession},
SessionLayer,
};
use axum_sessions::{extractors::Session, SessionLayer};
use rand::Rng;

#[tokio::main]
Expand All @@ -24,14 +21,14 @@ async fn main() {
let secret = rand::thread_rng().gen::<[u8; 128]>();
let session_layer = SessionLayer::new(store, &secret);

async fn increment_count_handler(mut session: WritableSession) {
async fn increment_count_handler(mut session: Session) {
let previous: usize = session.get("counter").unwrap_or_default();
session
.insert("counter", previous + 1)
.expect("Could not store counter.");
}

async fn handler(session: ReadableSession) -> String {
async fn handler(session: Session) -> String {
format!(
"Counter: {}",
session.get::<usize>("counter").unwrap_or_default()
Expand Down
1 change: 1 addition & 0 deletions examples/counter/Cargo.toml
Expand Up @@ -7,6 +7,7 @@ publish = false
[dependencies]
axum = "0.6.0"
axum-sessions = { path = "../../" }
async-session-memory-store = { workspace = true }

[dependencies.rand]
version = "0.8.5"
Expand Down
13 changes: 5 additions & 8 deletions examples/counter/src/main.rs
Expand Up @@ -4,12 +4,9 @@
//! cd examples && cargo run -p example-counter
//! ```

use async_session_memory_store::MemoryStore;
use axum::{response::IntoResponse, routing::get, Router};
use axum_sessions::{
async_session::MemoryStore,
extractors::{ReadableSession, WritableSession},
SessionLayer,
};
use axum_sessions::{extractors::Session, SessionLayer};
use rand::Rng;

#[tokio::main]
Expand All @@ -18,7 +15,7 @@ async fn main() {
let secret = rand::thread_rng().gen::<[u8; 128]>();
let session_layer = SessionLayer::new(store, &secret).with_secure(false);

async fn display_handler(session: ReadableSession) -> impl IntoResponse {
async fn display_handler(session: Session) -> impl IntoResponse {
let mut count = 0;
count = session.get("count").unwrap_or(count);
format!(
Expand All @@ -27,14 +24,14 @@ async fn main() {
)
}

async fn increment_handler(mut session: WritableSession) -> impl IntoResponse {
async fn increment_handler(mut session: Session) -> impl IntoResponse {
let mut count = 1;
count = session.get("count").map(|n: i32| n + 1).unwrap_or(count);
session.insert("count", count).unwrap();
format!("Count is: {}", count)
}

async fn reset_handler(mut session: WritableSession) -> impl IntoResponse {
async fn reset_handler(mut session: Session) -> impl IntoResponse {
session.destroy();
"Count reset"
}
Expand Down
1 change: 1 addition & 0 deletions examples/regenerate/Cargo.toml
Expand Up @@ -7,6 +7,7 @@ publish = false
[dependencies]
axum = "0.6.0"
axum-sessions = { path = "../../" }
async-session-memory-store = { workspace = true }

[dependencies.rand]
version = "0.8.5"
Expand Down
13 changes: 5 additions & 8 deletions examples/regenerate/src/main.rs
Expand Up @@ -4,12 +4,9 @@
//! cd examples && cargo run -p example-regenerate
//! ```

use async_session_memory_store::MemoryStore;
use axum::{routing::get, Router};
use axum_sessions::{
async_session::MemoryStore,
extractors::{ReadableSession, WritableSession},
SessionLayer,
};
use axum_sessions::{extractors::Session, SessionLayer};
use rand::Rng;

#[tokio::main]
Expand All @@ -18,19 +15,19 @@ async fn main() {
let secret = rand::thread_rng().gen::<[u8; 128]>();
let session_layer = SessionLayer::new(store, &secret);

async fn regenerate_handler(mut session: WritableSession) {
async fn regenerate_handler(mut session: Session) {
// NB: This DOES NOT update the store, meaning that both sessions will still be
// found.
session.regenerate();
}

async fn insert_handler(mut session: WritableSession) {
async fn insert_handler(mut session: Session) {
session
.insert("foo", 42)
.expect("Could not store the answer.");
}

async fn handler(session: ReadableSession) -> String {
async fn handler(session: Session) -> String {
session
.get::<usize>("foo")
.map(|answer| format!("{}", answer))
Expand Down
1 change: 1 addition & 0 deletions examples/signin/Cargo.toml
Expand Up @@ -7,6 +7,7 @@ publish = false
[dependencies]
axum = "0.6.0"
axum-sessions = { path = "../../" }
async-session-memory-store = { workspace = true }

[dependencies.rand]
version = "0.8.5"
Expand Down
13 changes: 5 additions & 8 deletions examples/signin/src/main.rs
Expand Up @@ -4,12 +4,9 @@
//! cd examples && cargo run -p example-signin
//! ```

use async_session_memory_store::MemoryStore;
use axum::{routing::get, Router};
use axum_sessions::{
async_session::MemoryStore,
extractors::{ReadableSession, WritableSession},
SessionLayer,
};
use axum_sessions::{extractors::Session, SessionLayer};
use rand::Rng;

#[tokio::main]
Expand All @@ -18,17 +15,17 @@ async fn main() {
let secret = rand::thread_rng().gen::<[u8; 128]>();
let session_layer = SessionLayer::new(store, &secret);

async fn signin_handler(mut session: WritableSession) {
async fn signin_handler(mut session: Session) {
session
.insert("signed_in", true)
.expect("Could not sign in.");
}

async fn signout_handler(mut session: WritableSession) {
async fn signout_handler(mut session: Session) {
session.destroy();
}

async fn protected_handler(session: ReadableSession) -> &'static str {
async fn protected_handler(session: Session) -> &'static str {
if session.get::<bool>("signed_in").unwrap_or(false) {
"Shh, it's secret!"
} else {
Expand Down

0 comments on commit c665ac6

Please sign in to comment.