我正在尝试设置一个服务器后端作为练习。
我想在中存储一些数据
warp_sessions::MemoryStore
但我无法实现。我基于
warp_sessions/examples/shared_mutable_session
实例
我可以将数据写入会话。我可以在写入后立即读回(变量
_a
和
_b
在下面的代码中),但它们不会在调用之间持久存在。变量
_a_pre
和
_b_pre
在连续的处理程序调用上返回空字符串。
现在的问题是:
-
我做错了什么?
-
如果我想对不同的路线使用相同的会话,并且我通过
session_store.clone()
在过滤器中,就像在warp示例中所做的那样——会话的修改会被存储吗?目前,我不使用存储的克隆实例,但数据无论如何都会丢失。然而,我确实需要不同路线使用这些数据
-
在warp_sessions的文档中,据说MemoryStore不应在生产中使用。生产中应该使用什么?
我的路由处理程序函数如下所示:
pub async fn verify_handler(body: warp::hyper::body::Bytes, mut session_with_store:SessionWithStore<MemoryStore> ) -> Result<(Html<String>, SessionWithStore<MemoryStore>), Rejection> {
session_with_store.cookie_options = warp_sessions::CookieOptions {
cookie_name: "siwe_minimal",
cookie_value: None,
max_age:Some(3600),
domain: None,
path: None,
secure: true,
http_only: true,
same_site: Some(SameSiteCookieOption::Strict),
};
let shared_session = Arc::new(RwLock::new(session_with_store.session));
let _a_pre: String = shared_session
.read()
.unwrap()
.get("nonce")
..unwrap_or_default();
shared_session
.write()
.unwrap()
.insert("nonce", nonce )
.unwrap();
let _a: String = shared_session
.read()
.unwrap()
.get("nonce")
.unwrap();
let msgstr = message.to_string();
let _b_pre: String = shared_session
.read()
.unwrap()
.get("siwe")
.unwrap_or_default();
shared_session
.write()
.unwrap()
.insert("siwe", msgstr)
.unwrap();
let _b: String = shared_session
.read()
.unwrap()
.get("siwe")
.unwrap();
session_with_store.session = Arc::try_unwrap(shared_session)
.unwrap()
.into_inner()
.unwrap();
Ok::<_, Rejection>(
(
warp::reply::html("req".to_string()),
session_with_store
)
)
}
路线定义如下(如果重要的话):
let session_store = MemoryStore::new();
let verify_route
= warp::path("verify")
.and(warp::post())
.and(warp::body::bytes())
.and(warp_sessions::request::with_session(session_store, None))
.and_then( verify_handler)
.untuple_one()
.and_then(warp_sessions::reply::with_session)
.recover(handle_rejection)
.with(&cors);