Skip to content

Commit

Permalink
Merge pull request #16 from EleisonC/revert-15-Add-redis
Browse files Browse the repository at this point in the history
Revert "Implement Redis"
  • Loading branch information
EleisonC authored Jun 16, 2024
2 parents 8260348 + ac8a9e6 commit 543b6a4
Show file tree
Hide file tree
Showing 11 changed files with 10 additions and 232 deletions.
42 changes: 0 additions & 42 deletions auth-service/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion auth-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ url = "2"
rand = "0.8.5"
sqlx = { version = "0.7", features = [ "runtime-tokio-rustls", "postgres", "migrate"] }
argon2 = { version = "0.5.3", features = ["std"] }
redis = { version = "0.25.2", features = ["tokio-comp"] }

[dev-dependencies]
fake = "=2.3.0"
Expand Down
1 change: 0 additions & 1 deletion auth-service/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ FROM debian:buster-slim AS runtime
WORKDIR /app
COPY --from=builder /app/target/release/auth-service /usr/local/bin
COPY --from=builder /app/assets /app/assets
ENV REDIS_HOST_NAME=redis
ENTRYPOINT ["/usr/local/bin/auth-service"]
6 changes: 0 additions & 6 deletions auth-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use axum::{
http::{Method, StatusCode},
Json
};
use redis::{Client, RedisResult};
use sqlx::{postgres::PgPoolOptions, PgPool};
use tower_http::{services::ServeDir, cors::CorsLayer};
use app_state::AppState;
Expand Down Expand Up @@ -98,8 +97,3 @@ impl Application {
pub async fn get_postgres_pool(url: &str) -> Result<PgPool, sqlx::Error> {
PgPoolOptions::new().max_connections(5).connect(url).await
}

pub fn get_redis_client(redis_hostname: String) -> RedisResult<Client> {
let redis_url = format!("redis://{}/", redis_hostname);
redis::Client::open(redis_url)
}
14 changes: 3 additions & 11 deletions auth-service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ use std::sync::Arc;
use sqlx::PgPool;
use tokio::sync::RwLock;

use auth_service::{app_state::AppState, get_postgres_pool, get_redis_client, services, utils::constants::{prod, DATABASE_URL, REDIS_HOST_NAME}, Application};
use auth_service::{app_state::AppState, get_postgres_pool, services, utils::constants::{prod, DATABASE_URL}, Application};

#[tokio::main]
async fn main() {
let pg_pool = configure_postgresql().await;
let redis_client = Arc::new(RwLock::new(configure_redis()));

let user_store = Arc::new(RwLock::new(services::PostgresUserStore::new(pg_pool)));
let banned_token_store = Arc::new(RwLock::new(services::RedisBannedTokenStore::new(redis_client.clone())));
let two_fa_code_store = Arc::new(RwLock::new(services::RedisTwoFACodeStore::new(redis_client)));
let banned_token_store = Arc::new(RwLock::new(services::HashsetBannedTokenStore::default()));
let two_fa_code_store = Arc::new(RwLock::new(services::HashmapTwoFACodeStore::default()));
let email_client = Arc::new(RwLock::new(services::MockEmailClient::default()));

let app_state = AppState::new(
Expand All @@ -34,10 +33,3 @@ async fn configure_postgresql() -> PgPool {
.expect("Failed to run migrations");
pg_pool
}

fn configure_redis() -> redis::Connection {
get_redis_client(REDIS_HOST_NAME.to_owned())
.expect("Failed to get Redis client")
.get_connection()
.expect("Failed to get Redis client")
}
6 changes: 1 addition & 5 deletions auth-service/src/services/data_stores/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ pub mod hashset_banned_token;
pub mod hashmap_two_fa_code_store;
pub mod mock_email_client;
pub mod postgres_user_store;
pub mod redis_banned_token_store;
pub mod redis_two_fa_code_store;


pub use hashmap_user_store::*;
pub use hashset_banned_token::*;
pub use hashmap_two_fa_code_store::*;
pub use mock_email_client::*;
pub use postgres_user_store::*;
pub use redis_banned_token_store::*;
pub use redis_two_fa_code_store::*;
pub use postgres_user_store::*;
55 changes: 0 additions & 55 deletions auth-service/src/services/data_stores/redis_banned_token_store.rs

This file was deleted.

81 changes: 0 additions & 81 deletions auth-service/src/services/data_stores/redis_two_fa_code_store.rs

This file was deleted.

8 changes: 0 additions & 8 deletions auth-service/src/utils/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::env as std_env;
lazy_static! {
pub static ref JWT_SECRET: String = set_token();
pub static ref DATABASE_URL: String = set_database_url();
pub static ref REDIS_HOST_NAME: String = set_redis_host();
}

fn set_token() -> String {
Expand All @@ -32,19 +31,12 @@ fn set_database_url() -> String {
database_url
}

fn set_redis_host() -> String {
dotenv().ok();
std_env::var(env::REDIS_HOST_NAME_ENV_VAR)
.unwrap_or(DEFAULT_REDIS_HOSTNAME.to_owned())
}
pub mod env {
pub const JWT_SECRET_ENV_VAR: &str = "JWT_SECRET";
pub const DATABASE_URL_ENV_VAR: &str = "DATABASE_URL";
pub const REDIS_HOST_NAME_ENV_VAR: &str = "REDIS_HOST_NAME";
}

pub const JWT_COOKIE_NAME: &str = "jwt";
pub const DEFAULT_REDIS_HOSTNAME: &str = "127.0.0.1";

pub mod prod {
pub const APP_ADDRESS: &str = "0.0.0.0:3000";
Expand Down
18 changes: 4 additions & 14 deletions auth-service/tests/api/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use auth_service::{app_state::AppState, get_postgres_pool, get_redis_client, services::{self, RedisTwoFACodeStore}, utils::constants::{test, DATABASE_URL, DEFAULT_REDIS_HOSTNAME}, Application};
use auth_service::{app_state::AppState, get_postgres_pool, services::{self, HashmapTwoFACodeStore}, utils::constants::{test, DATABASE_URL}, Application};
use sqlx::{postgres::{PgConnectOptions, PgPoolOptions}, Connection, Executor, PgConnection, PgPool};
use uuid::Uuid;
use std::{str::FromStr, sync::Arc};
Expand All @@ -11,19 +11,18 @@ pub struct TestApp {
pub http_client: reqwest::Client,
pub db_name: String,
pub cookie_jar: Arc<Jar>,
pub two_fa_code_store: Arc<RwLock<RedisTwoFACodeStore>>,
pub two_fa_code_store: Arc<RwLock<HashmapTwoFACodeStore>>,
clean_up_called: bool
}

impl TestApp {
pub async fn new() -> Self {
let db_name = Uuid::new_v4().to_string();
let pg_pool = configure_postgresql(&db_name).await;
let redis_client = Arc::new(RwLock::new(configure_redis()));

let test_user_store = Arc::new(RwLock::new(services::PostgresUserStore::new(pg_pool)));
let test_banned_token_store = Arc::new(RwLock::new(services::RedisBannedTokenStore::new(redis_client.clone())));
let two_fa_code_store = Arc::new(RwLock::new(services::RedisTwoFACodeStore::new(redis_client)));
let test_banned_token_store = Arc::new(RwLock::new(services::HashsetBannedTokenStore::default()));
let two_fa_code_store = Arc::new(RwLock::new(services::HashmapTwoFACodeStore::default()));
let email_client = Arc::new(RwLock::new(services::MockEmailClient::default()));

let test_app_state = AppState::new(test_user_store, test_banned_token_store, two_fa_code_store.clone(), email_client);
Expand Down Expand Up @@ -206,12 +205,3 @@ async fn delete_database(db_name: &str) {
.await
.expect("Failed to drop the database.");
}

fn configure_redis() -> redis::Connection {
let redis_hostname = DEFAULT_REDIS_HOSTNAME.to_owned();

get_redis_client(redis_hostname)
.expect("Failed to get Redis client")
.get_connection()
.expect("Failed to get Redis connection")
}
10 changes: 2 additions & 8 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
version: "3.9"
services:
app-service:
# TODO: change "letsgetrusty" to your Docker Hub username
image: redwallet212/app-service # specify name of image on Docker Hub
restart: "always" # automatically restart container when server crashes
container_name: app-service
environment: # set up environment variables
AUTH_SERVICE_IP: ${AUTH_SERVICE_IP}
ports:
Expand All @@ -12,6 +12,7 @@ services:
auth-service:
condition: service_started
auth-service:
# TODO: change "letsgetrusty" to your Docker Hub username
image: redwallet212/auth-service
restart: always # automatically restart container when server crashes
container_name: auth-service
Expand All @@ -22,7 +23,6 @@ services:
- "3000:3000" # expose port 3000 so that applications outside the container can connect to it
depends_on:
- db
- redis
db:
image: postgres:15.2-alpine
restart: always
Expand All @@ -33,12 +33,6 @@ services:
- "5432:5432"
volumes:
- db_data:/var/lib/postgresql/data
redis:
image: redis:7.0-alpine
restart: always
container_name: redis
ports:
- "6379:6379"

volumes:
db_data:
Expand Down

0 comments on commit 543b6a4

Please sign in to comment.