feat: adds client
This commit is contained in:
parent
43ab8837ce
commit
bf3dbf8c1c
@ -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<dyn std::error::Error>> {
|
||||
@ -15,8 +15,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Port 50051 is the recommended gRPC port.
|
||||
let addr = "[::0]:50051".parse()?;
|
||||
|
||||
let users_service: Box<Mutex<dyn Users + Send + Sync + 'static>> = todo!(); // Create user service instance
|
||||
let sessions_service: Box<Mutex<dyn Sessions + Send + Sync + 'static>> = todo!(); //Create session service instance
|
||||
let users_service: Box<Mutex<dyn Users + Send + Sync + 'static>> =
|
||||
Box::new(Mutex::new(UsersImpl::new())); // Create user service instance
|
||||
let sessions_service: Box<Mutex<dyn Sessions + Send + Sync + 'static>> =
|
||||
Box::new(Mutex::new(SessionsImpl::new())); //Create session service instance
|
||||
|
||||
let auth_service = AuthService::new(users_service, sessions_service);
|
||||
|
||||
|
@ -12,6 +12,14 @@ pub struct SessionsImpl {
|
||||
uuid_to_session: HashMap<String, String>,
|
||||
}
|
||||
|
||||
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().
|
||||
|
@ -37,6 +37,15 @@ pub struct UsersImpl {
|
||||
username_to_user: HashMap<String, User>,
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -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<Commands>,
|
||||
}
|
||||
|
||||
#[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<dyn std::error::Error>> {
|
||||
// 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<Channel> =
|
||||
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<SignInRequest> = 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<SignInResponse> into SignInResponse.
|
||||
let response: SignInResponse = client.sign_in(request).await?.into_inner();
|
||||
|
||||
println!("{response:?}");
|
||||
}
|
||||
Some(Commands::SignUp { username, password }) => {
|
||||
let request: Request<SignUpRequest> = Request::new(SignUpRequest {
|
||||
username: username.to_string(),
|
||||
password: password.to_string(),
|
||||
}); // Create a new `SignUpRequest`.
|
||||
|
||||
let response: Response<SignUpResponse> = 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<SignOutRequest> = Request::new(SignOutRequest {
|
||||
session_token: session_token.to_string(),
|
||||
}); // Create a new `SignOutRequest`.
|
||||
|
||||
let response: Response<SignOutResponse> = client.sign_out(request).await?; // Make a sign out request. Propagate any errors.
|
||||
|
||||
println!("{:?}", response.into_inner());
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user