feat: attention & redis & async

This commit is contained in:
2022-03-20 22:22:08 +08:00
parent e87d8acb7c
commit 58eb7aba6f
16 changed files with 495 additions and 211 deletions

42
src/rds_conn.rs Normal file
View File

@@ -0,0 +1,42 @@
use redis::aio::MultiplexedConnection;
use rocket::request::{FromRequest, Outcome, Request};
use std::ops::{Deref, DerefMut};
use std::env;
pub struct RdsConn(pub MultiplexedConnection);
#[rocket::async_trait]
impl<'r> FromRequest<'r> for RdsConn {
type Error = ();
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
let rconn = request.rocket().state::<MultiplexedConnection>().unwrap();
Outcome::Success(RdsConn(rconn.clone()))
}
}
impl Clone for RdsConn {
fn clone(&self) -> Self {
RdsConn(self.0.clone())
}
}
impl Deref for RdsConn {
type Target = MultiplexedConnection;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for RdsConn {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
pub async fn init_rds_client() -> MultiplexedConnection {
let redis_url = env::var("REDIS_URL").expect("REDIS_URL must be set");
let client = redis::Client::open(redis_url).expect("connect to redis fail");
client.get_multiplexed_async_connection().await.unwrap()
}