multiberry-backend/src/route/handlers.rs

52 lines
1.9 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.

// src/route/handlers.rs
// Почему это здесь?
// - Это всё, что обрабатывает HTTP запросы
// - Каждый handler'а — это async функция, которая обрабатывает запрос и возвращает ответ
use axum::{
extract::State,
http::StatusCode,
response::IntoResponse,
Json,
};
use serde_json::json;
use crate::state::AppState;
/// Health-check handler
///
/// Что он делает:
/// 1. Принимает AppState через extract::State (это параметр, который Axum инжектирует)
/// 2. Пытается выполнить простой SELECT 1 в БД (проверка подключения)
/// 3. Возвращает 200 OK если всё хорошо, или 500 если БД недоступна
pub async fn health_handler(
State(state): State<AppState>,
) -> impl IntoResponse {
// Пытаемся выполнить простой запрос к БД
let result = sqlx::query("SELECT 1")
.execute(&state.db_pool)
.await;
// Обрабатываем результат
match result {
Ok(_) => (
StatusCode::OK,
Json(json!({ "status": "Database connection is successful." })),
).into_response(),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({ "status": format!("Database connection failed: {}", e) })),
).into_response(),
}
}
pub async fn test_token_handler(
State(state): State<AppState>,
) -> impl IntoResponse {
match state.banking_clients.vbank.get_token().await {
Ok(token) => (StatusCode::OK, Json(json!({ "vbank_token": token }))),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({ "error": e.to_string() })),
),
}
}