2个不稳定版本
| 0.2.0 | 2022年4月1日 | 
|---|---|
| 0.1.0 | 2022年2月5日 | 
#1167 in HTTP服务器
23KB
567 行
rocket-session-store
rocket-session-store是一个用于rocket Web框架的库。它通过使用cookies和可自定义的存储库来管理会话。
快速入门
使用此库包括以下两个步骤:
- 在构建rocket时设置会话存储公平。
- 使用会话请求保护器。
use rocket_session_store::{
	memory::MemoryStore,
	SessionStore,
	SessionResult,
	Session,
	CookieConfig,
};
use rocket::{
	Rocket,
	get,
	routes,
	launch,
};
use std::time::Duration;
// Using the `Session` request guard
#[get("/")]
async fn index(session: Session<'_, String>) -> SessionResult<String> {
	let name: Option<String> = session.get().await?;
	if let Some(name) = name {
		Ok(format!("Hello, {}!", name))
	} else {
		Ok("Hello, world!".into())
	}
}
#[launch]
fn rocket() -> _ {
	// Instance a store that fits your needs and wrap it in a Box in SessionStore.
	let memory_store: MemoryStore::<String> = MemoryStore::default();
	let store: SessionStore<String> = SessionStore {
		store: Box::new(memory_store),
		name: "token".into(),
		duration: Duration::from_secs(3600 * 24 * 3),
		// The cookie config is used to set the cookie's path and other options.
		cookie: CookieConfig::default(),
	};
	// Attach it to a rocket by calling `fairing()`
	rocket::build().attach(store.fairing()).mount("/", routes![index])
}
贡献
如果您想贡献,请阅读CONTRIBUTING.md。
依赖关系
~16–50MB
~815K SLoC