diff options
| author | KunoiSayami <[email protected]> | 2021-05-07 02:09:12 +0800 |
|---|---|---|
| committer | KunoiSayami <[email protected]> | 2021-05-07 02:09:12 +0800 |
| commit | 1bd3bfc91fda539ed236c5bb701441b7436d4ead (patch) | |
| tree | 7e9319240168f0d519afc2915cc614aa856c85c8 | |
| parent | e13d73f9c501d7340712feb7b666775fc55b75b3 (diff) | |
feat: Use async connect to redis
| -rw-r--r-- | src/main.rs | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/src/main.rs b/src/main.rs index 9e03850..1925d1e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ use serde::{Serialize}; use handlebars::Handlebars; use sqlx::Connection; use crate::datastructures::{Config, FormData}; -use redis::Commands; +use redis::AsyncCommands; const COOKIE_LENGTH: usize = 45; @@ -105,8 +105,9 @@ async fn cmd_authenticate_cookie( return Ok(false) } - // TODO: use async connection to redis - let mut conn = redis::Client::open("redis://127.0.0.1/")?; + let redis_conn = redis::Client::open("redis://127.0.0.1/")?; + let mut conn = redis_conn.get_async_connection().await?; + for cookie in cookies.split(';').map(|x| x.trim()) { let (key, value) = cookie.split_once('=').unwrap(); @@ -126,7 +127,7 @@ async fn cmd_authenticate_cookie( break } - if let Ok(r) = conn.get::<_, String>(format!("cgit_auth_{}", key)) { + if let Ok(r) = conn.get::<_, String>(format!("cgit_auth_{}", key)).await { if r == value { return Ok(true) } @@ -174,8 +175,9 @@ async fn cmd_authenticate_post( let value = rand_str(COOKIE_LENGTH); // TODO: same here - let mut conn = redis::Client::open("redis://127.0.0.1/")?; - conn.set_ex::<_, _, i32>(format!("cgit_auth_{}", key), &value, cfg.cookie_ttl as usize)?; + let redis_conn = redis::Client::open("redis://127.0.0.1/")?; + let mut conn = redis_conn.get_async_connection().await?; + conn.set_ex::<_, _, i32>(format!("cgit_auth_{}", key), &value, cfg.cookie_ttl as usize).await?; let cookie_value = base64::encode(format!("{};{}", key, value)); |
