feat: coin flip

feat:  button trigger
This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-03-09 03:35:17 +03:00
parent 75cf0be90d
commit aeafc3cf4e
3 changed files with 82 additions and 38 deletions

View file

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
axum = "0.7.4"
rand = "0.8.5"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
tokio = { version = "1.36.0", features = ["full"] }

View file

@ -1,20 +1,35 @@
use crate::AppState;
use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Json, Router};
use tower_http::cors::CorsLayer;
use rand::prelude::*;
pub async fn routing(State(state): State<AppState>) -> Router{
pub async fn routing(State(state): State<AppState>) -> Router {
Router::new()
.route("/", get(alive))
.route("/coin", get(flip_coin))
.layer(CorsLayer::permissive())
.with_state(state.clone())
}
async fn alive() -> impl IntoResponse{
async fn alive() -> impl IntoResponse {
let alive_json = serde_json::json!({
"status":"Alive",
});
println!("Alive");
(StatusCode::OK, Json(alive_json))
}
async fn flip_coin() -> impl IntoResponse {
let mut rng = rand::thread_rng();
let random:f64 = rng.gen();
let mut flip_status:String = "Tail".to_string();
if random > 0.5 {
flip_status = "Head".to_string();
}
let coin_json = serde_json::json!({
"status":flip_status,
});
println!("Coin Flip");
(StatusCode::OK, Json(coin_json))
}