- add: logout route to api service
This commit is contained in:
@@ -8,6 +8,7 @@ use std::sync::Arc;
|
||||
use tonic::transport::Channel;
|
||||
use tower_http::cors::{Any, CorsLayer};
|
||||
|
||||
use crate::axum_gateway::auth::LogoutRequest;
|
||||
use auth::auth_service_client::AuthServiceClient;
|
||||
use auth::{LoginRequest, RegisterRequest};
|
||||
use log::{error, info};
|
||||
@@ -32,6 +33,16 @@ struct RestLoginResponse {
|
||||
session_id: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct RestLogoutRequest {
|
||||
session_id: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct RestLogoutResponse {
|
||||
message: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct RestRegisterRequest {
|
||||
username: String,
|
||||
@@ -52,8 +63,6 @@ async fn login_handler(
|
||||
) -> Result<Json<RestLoginResponse>, axum::http::StatusCode> {
|
||||
let ip_address = addr.ip().to_string();
|
||||
|
||||
info!("Client IP Address: {}", ip_address);
|
||||
|
||||
let request = tonic::Request::new(LoginRequest {
|
||||
username: payload.username.clone(),
|
||||
password: payload.password.clone(),
|
||||
@@ -75,6 +84,28 @@ async fn login_handler(
|
||||
}
|
||||
}
|
||||
|
||||
async fn logout_handler(
|
||||
ConnectInfo(addr): ConnectInfo<SocketAddr>,
|
||||
State(grpc_client): State<Arc<Mutex<AuthServiceClient<Channel>>>>,
|
||||
Json(payload): Json<RestLogoutRequest>,
|
||||
) -> Result<axum::http::StatusCode, axum::http::StatusCode> {
|
||||
let ip_address = addr.ip().to_string();
|
||||
|
||||
let request = tonic::Request::new(LogoutRequest {
|
||||
session_id: payload.session_id
|
||||
});
|
||||
|
||||
let mut client = grpc_client.lock().await; // Lock the mutex to get mutable access
|
||||
match client.logout(request).await {
|
||||
Ok(_response) => {
|
||||
Ok(axum::http::StatusCode::OK)
|
||||
}
|
||||
Err(_e) => {
|
||||
Err(axum::http::StatusCode::INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn register_handler(
|
||||
ConnectInfo(addr): ConnectInfo<SocketAddr>,
|
||||
State(grpc_client): State<Arc<Mutex<AuthServiceClient<Channel>>>>,
|
||||
@@ -110,6 +141,7 @@ pub async fn serve_rest_api(
|
||||
|
||||
let app = Router::new()
|
||||
.route("/api/login", post(login_handler))
|
||||
.route("/api/logout", post(logout_handler))
|
||||
.route("/api/register", post(register_handler))
|
||||
.with_state(grpc_client)
|
||||
.layer(cors);
|
||||
@@ -123,8 +155,8 @@ pub async fn serve_rest_api(
|
||||
listener,
|
||||
app.into_make_service_with_connect_info::<SocketAddr>(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user