CryptoFox-Mukan-Edition/src/api/mode.rs

54 lines
1.5 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use axum::{extract::State, http::StatusCode, response::IntoResponse, Json};
use serde::{Deserialize, Serialize};
use crate::AppState;
#[derive(Serialize)]
pub struct ModeResponse {
pub mode: String,
}
#[derive(Deserialize)]
pub struct SetModeRequest {
pub mode: String,
}
pub async fn get_mode(State(state): State<AppState>) -> impl IntoResponse {
let mode = state.current_mode.lock().await.clone();
Json(ModeResponse { mode }).into_response()
}
pub async fn set_mode(
State(state): State<AppState>,
Json(req): Json<SetModeRequest>,
) -> impl IntoResponse {
if req.mode != "live" && req.mode != "testnet" {
return (StatusCode::BAD_REQUEST, "Geçersiz mod").into_response();
}
let mut current = state.current_mode.lock().await;
if *current == req.mode {
return Json(ModeResponse { mode: current.clone() }).into_response();
}
*current = req.mode.clone();
drop(current);
// DB'ye kaydet
let db = state.db.lock().await;
if let Err(e) = db.set_config("current_mode", &req.mode) {
return (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response();
}
drop(db);
// Tüm çalışan botları durdur
let mut manager = state.manager.lock().await;
let running = manager.running_ids();
for id in running {
manager.stop(&id).await;
let db = state.db.lock().await;
let _ = db.set_bot_active(&id, false);
}
drop(manager);
Json(ModeResponse { mode: req.mode }).into_response()
}