Installation
The session machinery is not included in the project scaffolded by pavex new
.
You need to go through a few steps to set it up.
Dependencies
The core session logic lives in a standalone crate, pavex_session
.
Support for different session storage backends is provided by separate crates, such as
pavex_session_sqlx
and pavex_session_memory_store
.
Choose a storage backend that fits your needs and then add the required dependencies to the Cargo.toml
of your application crate:
Kits
Kits bundles together all the components you need to work with a specific session setup.
Register the one provided by the storage backend you chose against your Blueprint
:
use pavex::{blueprint::Blueprint, cookie::CookieKit};
use pavex_session_sqlx::PostgresSessionKit;
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
PostgresSessionKit::new().register(&mut bp);
// Sessions are built on top of cookies,
// so you need to set those up too.
// Order is important here!
CookieKit::new().register(&mut bp);
// [...]
}
use pavex::blueprint::Blueprint;
use pavex::cookie::CookieKit;
use pavex_session_memory_store::InMemorySessionKit;
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
InMemorySessionKit::new().register(&mut bp);
// Sessions are built on top of cookies,
// so you need to set those up too.
// Order is important here!
CookieKit::new().register(&mut bp);
// [...]
}
You can customize each component inside the kit to suit your needs. Check out their respective documentation for more information.