-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.rs
102 lines (87 loc) · 2.75 KB
/
redis.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use gotham::{
handler::HandlerFuture,
helpers::http::response::create_empty_response,
middleware::{
session::{Backend, NewBackend, SessionError, SessionIdentifier},
Middleware, NewMiddleware,
},
state::{FromState, State, StateData},
};
use hyper::StatusCode;
use redis_async::client::paired::{paired_connect, PairedConnection};
use std::future::Future;
use std::pin::Pin;
#[derive(Clone)]
pub struct RedisConnection {
pub conn: PairedConnection,
}
impl StateData for RedisConnection {}
#[derive(Clone)]
pub struct RedisMiddleware;
impl NewMiddleware for RedisMiddleware {
type Instance = RedisMiddleware;
fn new_middleware(&self) -> anyhow::Result<Self::Instance> {
Ok(RedisMiddleware)
}
}
impl Middleware for RedisMiddleware {
fn call<Chain>(self, mut state: State, chain: Chain) -> Pin<Box<HandlerFuture>>
where
Chain: FnOnce(State) -> Pin<Box<HandlerFuture>> + std::marker::Send + 'static,
{
Box::pin(async {
let addr = "127.0.0.1:6379".parse().unwrap();
match paired_connect(&addr).await {
Ok(conn) => {
state.put(RedisConnection { conn });
chain(state).await
}
Err(_) => {
let res = create_empty_response(&state, StatusCode::INTERNAL_SERVER_ERROR);
Ok((state, res))
}
}
})
}
}
type SessionFuture = dyn Future<Output = Result<Option<Vec<u8>>, SessionError>> + Send;
#[derive(Clone)]
pub struct RedisBackend;
impl NewBackend for RedisBackend {
type Instance = RedisBackend;
fn new_backend(&self) -> anyhow::Result<Self::Instance> {
Ok(RedisBackend)
}
}
impl Backend for RedisBackend {
fn persist_session(
&self,
state: &State,
identifier: SessionIdentifier,
content: &[u8],
) -> Result<(), SessionError> {
info!("Persisting session {}", identifier.value);
let conn = RedisConnection::borrow_from(&state);
conn.conn.send_and_forget(resp_array!["HSET", "session", identifier.value, content]);
Ok(())
}
fn read_session(
&self,
state: &State,
identifier: SessionIdentifier,
) -> Pin<Box<SessionFuture>> {
info!("Reading session {}", identifier.value);
let conn = RedisConnection::borrow_from(&state);
let conn = conn.clone();
Box::pin(async move {
let result = conn.conn
.send(resp_array!["HGET", "session", identifier.value])
.await
.ok();
Ok(result)
})
}
fn drop_session(&self, _identifier: SessionIdentifier) -> Result<(), SessionError> {
unimplemented!()
}
}