From 3baea0e943ef89c7c73a5de51cbd41f78f8f0f14 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 28 Dec 2020 13:20:35 +0530 Subject: add redis integration --- backend/Cargo.lock | 52 ++++++++++++++++++++++++++++++++++--------- backend/Cargo.toml | 1 + backend/src/handlers/users.rs | 12 ++++++++++ 3 files changed, 55 insertions(+), 10 deletions(-) (limited to 'backend') diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 2f6aaaa..d309a66 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -7,7 +7,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78d1833b3838dbe990df0f1f87baf640cf6146e898166afe401839d1b001e570" dependencies = [ "bitflags", - "bytes", + "bytes 0.5.6", "futures-core", "futures-sink", "log", @@ -64,7 +64,7 @@ dependencies = [ "base64", "bitflags", "brotli2", - "bytes", + "bytes 0.5.6", "cookie", "copyless", "derive_more", @@ -229,7 +229,7 @@ dependencies = [ "actix-rt", "actix-service", "bitflags", - "bytes", + "bytes 0.5.6", "either", "futures-channel", "futures-sink", @@ -258,7 +258,7 @@ dependencies = [ "actix-utils", "actix-web-codegen", "awc", - "bytes", + "bytes 0.5.6", "derive_more", "encoding_rs", "futures-channel", @@ -412,7 +412,7 @@ dependencies = [ "actix-rt", "actix-service", "base64", - "bytes", + "bytes 0.5.6", "derive_more", "futures-core", "log", @@ -566,13 +566,19 @@ version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" +[[package]] +name = "bytes" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad1f8e949d755f9d79112b5bb46938e0ef9d3804a0b16dfab13aafcaa5f0fa72" + [[package]] name = "bytestring" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc7c05fa5172da78a62d9949d662d2ac89d4cc7355d7b49adee5163f1fb3f363" dependencies = [ - "bytes", + "bytes 0.5.6", ] [[package]] @@ -610,6 +616,16 @@ dependencies = [ "bitflags", ] +[[package]] +name = "combine" +version = "4.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01199925a18b00193570e3d70cfe57dcb647eb167c29851983e76fc39e2fee39" +dependencies = [ + "bytes 1.0.0", + "memchr", +] + [[package]] name = "const_fn" version = "0.4.2" @@ -813,6 +829,7 @@ dependencies = [ "log", "pretty_env_logger", "rand", + "redis", "serde", "serde_derive", "serde_json", @@ -952,7 +969,7 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "993f9e0baeed60001cf565546b0d3dbe6a6ad23f2bd31644a133c641eccf6d53" dependencies = [ - "bytes", + "bytes 0.5.6", "fnv", "futures-core", "futures-sink", @@ -1026,7 +1043,7 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28d569972648b2c512421b5f2a405ad6ac9666547189d0c5477a3f200f3e02f9" dependencies = [ - "bytes", + "bytes 0.5.6", "fnv", "itoa", ] @@ -1546,6 +1563,21 @@ dependencies = [ "rand_core", ] +[[package]] +name = "redis" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a6ddfecac9391fed21cce10e83c65fa4abafd77df05c98b1c647c65374ce9b3" +dependencies = [ + "async-trait", + "combine", + "dtoa", + "itoa", + "percent-encoding 2.1.0", + "sha1", + "url 2.1.1", +] + [[package]] name = "redox_syscall" version = "0.1.57" @@ -1938,7 +1970,7 @@ version = "0.2.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d34ca54d84bf2b5b4d7d31e901a8464f7b60ac145a284fba25ceb801f2ddccd" dependencies = [ - "bytes", + "bytes 0.5.6", "futures-core", "iovec", "lazy_static", @@ -1958,7 +1990,7 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be8242891f2b6cbef26a2d7e8605133c2c554cd35b3e4948ea892d6d68436499" dependencies = [ - "bytes", + "bytes 0.5.6", "futures-core", "futures-sink", "log", diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 8ce728b..fad11b0 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -18,6 +18,7 @@ actix-identity = "0.3.1" actix-cors = "0.5" bigdecimal = "0.0.14" rand = "0.7" +redis = "0.19" [dependencies.diesel] version = "1.4.2" diff --git a/backend/src/handlers/users.rs b/backend/src/handlers/users.rs index 8423384..775635e 100644 --- a/backend/src/handlers/users.rs +++ b/backend/src/handlers/users.rs @@ -9,6 +9,7 @@ use actix_web::{web, HttpResponse, Responder}; use bcrypt::{hash, verify, DEFAULT_COST}; use diesel::prelude::*; use log::{error, info}; +use redis::Commands; use serde::{Deserialize, Serialize}; pub async fn new_user( @@ -74,6 +75,14 @@ pub async fn login( let hashed_pass = selected_user.password; if verify(entered_pass, &hashed_pass).unwrap() { cookie.remember(login_details.username.clone()); + let redis_client = redis::Client::open("redis://127.0.0.1/").unwrap(); + let mut redis_conn = redis_client.get_connection().unwrap(); + redis_conn + .set::( + login_details.username.clone(), + cookie.identity().unwrap(), + ) + .unwrap(); info!( "Successful login: {} {}", selected_user.username, selected_user.email_id @@ -85,6 +94,9 @@ pub async fn login( } pub async fn logout(cookie: Identity) -> impl Responder { + let redis_client = redis::Client::open("redis://127.0.0.1/").unwrap(); + let mut redis_conn = redis_client.get_connection().unwrap(); + redis_conn.del::(cookie.identity().unwrap()); cookie.forget(); HttpResponse::Ok().body("Successful logout.") } -- cgit v1.2.3