fix: 🐛 http get with body unrecommended, we transfered request to url

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-05-26 16:14:04 +03:00
parent ff5512fe26
commit 62cf6e6232

View file

@ -1,11 +1,11 @@
use axum::{ use axum::{
extract::State, extract::{Path, State},
http::StatusCode, http::StatusCode,
response::IntoResponse, response::IntoResponse,
routing::{get, post}, routing::{get, post},
Json, Router, Json, Router,
}; };
use serde::{Deserialize, Serialize}; use serde::Deserialize;
use tower_http::cors::CorsLayer; use tower_http::cors::CorsLayer;
use crate::{ use crate::{
@ -19,16 +19,12 @@ struct ReceivedMessage {
username: String, username: String,
message: String, message: String,
} }
#[derive(Debug, Serialize, Deserialize)]
struct SendRequest {
room_id: String,
}
pub async fn routing(State(state): State<AppState>) -> Router { pub async fn routing(State(state): State<AppState>) -> Router {
Router::new() Router::new()
.route("/", get(alive)) .route("/", get(alive))
.route("/send", post(receive_message)) .route("/send", post(receive_message))
.route("/receive", get(send_message)) .route("/receive/:room_id", get(send_message))
.layer(CorsLayer::permissive()) .layer(CorsLayer::permissive())
.with_state(state) .with_state(state)
} }
@ -63,10 +59,10 @@ async fn receive_message(
} }
async fn send_message( async fn send_message(
Path(room_id): Path<String>,
State(mut state): State<AppState>, State(mut state): State<AppState>,
Json(send_request): Json<SendRequest>,
) -> impl IntoResponse { ) -> impl IntoResponse {
match state.is_chat_exists(&send_request.room_id).await { match state.is_chat_exists(&room_id).await {
Some(index) => { Some(index) => {
let chats = state.chats.lock().await; let chats = state.chats.lock().await;
( (