From bf3dbf8c1c5447fc934dacd6a876d9d7d9b0e9db Mon Sep 17 00:00:00 2001 From: itsscb Date: Tue, 13 Aug 2024 08:47:50 +0200 Subject: [PATCH] feat: adds client --- src/auth-service/main.rs | 10 +++-- src/auth-service/sessions.rs | 8 ++++ src/auth-service/users.rs | 9 ++++ src/client/main.rs | 87 +++++++++++++++++++++++++++++++++++- 4 files changed, 108 insertions(+), 6 deletions(-) diff --git a/src/auth-service/main.rs b/src/auth-service/main.rs index e2f362a..7fea689 100644 --- a/src/auth-service/main.rs +++ b/src/auth-service/main.rs @@ -5,8 +5,8 @@ mod sessions; mod users; use auth::*; -use sessions::{SessionsImpl, Sessions}; -use users::{UsersImpl, Users}; +use sessions::{Sessions, SessionsImpl}; +use users::{Users, UsersImpl}; #[tokio::main] async fn main() -> Result<(), Box> { @@ -15,8 +15,10 @@ async fn main() -> Result<(), Box> { // Port 50051 is the recommended gRPC port. let addr = "[::0]:50051".parse()?; - let users_service: Box> = todo!(); // Create user service instance - let sessions_service: Box> = todo!(); //Create session service instance + let users_service: Box> = + Box::new(Mutex::new(UsersImpl::new())); // Create user service instance + let sessions_service: Box> = + Box::new(Mutex::new(SessionsImpl::new())); //Create session service instance let auth_service = AuthService::new(users_service, sessions_service); diff --git a/src/auth-service/sessions.rs b/src/auth-service/sessions.rs index 699d99f..6bd9889 100644 --- a/src/auth-service/sessions.rs +++ b/src/auth-service/sessions.rs @@ -12,6 +12,14 @@ pub struct SessionsImpl { uuid_to_session: HashMap, } +impl SessionsImpl { + pub fn new() -> Self { + Self { + uuid_to_session: HashMap::new(), + } + } +} + impl Sessions for SessionsImpl { fn create_session(&mut self, user_uuid: &str) -> String { let session: String = Uuid::new_v4().to_string(); // Create a new session using Uuid::new_v4(). diff --git a/src/auth-service/users.rs b/src/auth-service/users.rs index 7e2c09c..a21ca8a 100644 --- a/src/auth-service/users.rs +++ b/src/auth-service/users.rs @@ -37,6 +37,15 @@ pub struct UsersImpl { username_to_user: HashMap, } +impl UsersImpl { + pub fn new() -> Self { + Self { + uuid_to_user: HashMap::new(), + username_to_user: HashMap::new(), + } + } +} + impl Users for UsersImpl { fn create_user(&mut self, username: String, password: String) -> Result<(), String> { if self.username_to_user.contains_key(&username) { diff --git a/src/client/main.rs b/src/client/main.rs index 6efd247..fc3d813 100644 --- a/src/client/main.rs +++ b/src/client/main.rs @@ -1,3 +1,86 @@ -fn main() { - println!("client"); +use clap::{Parser, Subcommand}; +use std::env; + +use authentication::auth_client::AuthClient; +use authentication::{SignInRequest, SignOutRequest, SignUpRequest}; +use tonic::transport::Channel; +use tonic::{Request, Response}; + +use crate::authentication::{SignInResponse, SignOutResponse, SignUpResponse}; + +pub mod authentication { + tonic::include_proto!("authentication"); +} + +#[derive(Parser)] +#[command(version, about, long_about = None)] +struct Cli { + #[command(subcommand)] + command: Option, +} + +#[derive(Subcommand)] +enum Commands { + SignIn { + #[arg(short, long)] + username: String, + #[arg(short, long)] + password: String, + }, + SignUp { + #[arg(short, long)] + username: String, + #[arg(short, long)] + password: String, + }, + SignOut { + #[arg(short, long)] + session_token: String, + }, +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + // AUTH_SERVICE_IP can be set to your droplet's ip address once your app is deployed + let auth_ip = env::var("AUTH_SERVICE_IP").unwrap_or("[::0]".to_owned()); + let mut client: AuthClient = + AuthClient::connect(format!("http://{auth_ip}:50051")).await?; // Create new `AuthClient` instance. Propagate any errors. + + let cli = Cli::parse(); + + match &cli.command { + Some(Commands::SignIn { username, password }) => { + let request: Request = Request::new(SignInRequest { + username: username.to_string(), + password: password.to_string(), + }); // Create a new `SignInRequest`. + + // Make a sign in request. Propagate any errors. Convert Response into SignInResponse. + let response: SignInResponse = client.sign_in(request).await?.into_inner(); + + println!("{response:?}"); + } + Some(Commands::SignUp { username, password }) => { + let request: Request = Request::new(SignUpRequest { + username: username.to_string(), + password: password.to_string(), + }); // Create a new `SignUpRequest`. + + let response: Response = client.sign_up(request).await?; // Make a sign up request. Propagate any errors. + + println!("{:?}", response.into_inner()); + } + Some(Commands::SignOut { session_token }) => { + let request: Request = Request::new(SignOutRequest { + session_token: session_token.to_string(), + }); // Create a new `SignOutRequest`. + + let response: Response = client.sign_out(request).await?; // Make a sign out request. Propagate any errors. + + println!("{:?}", response.into_inner()); + } + None => {} + } + + Ok(()) }