(feat) updated to extend application
This commit is contained in:
parent
6685f73268
commit
d53dadb272
4 changed files with 522 additions and 57 deletions
83
src/main.rs
83
src/main.rs
|
|
@ -1,42 +1,46 @@
|
|||
// src/main.rs
|
||||
// Подключаем модули напрямую без mod.rs!
|
||||
mod banking {
|
||||
pub mod client;
|
||||
pub mod models;
|
||||
pub mod error;
|
||||
}
|
||||
|
||||
mod services {
|
||||
pub mod account_service;
|
||||
pub mod consent_service;
|
||||
}
|
||||
|
||||
mod handlers {
|
||||
pub mod health;
|
||||
pub mod accounts;
|
||||
}
|
||||
|
||||
mod config;
|
||||
mod db;
|
||||
mod error;
|
||||
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Json},
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use serde_json::json;
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
use sqlx::PgPool;
|
||||
use std::net::SocketAddr;
|
||||
use std::env;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
db_pool: PgPool,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
dotenvy::dotenv().ok();
|
||||
|
||||
let database_url = env::var("DATABASE_URL")
|
||||
.expect("DATABASE_URL must be set");
|
||||
let db_pool = db::init_pool().await;
|
||||
|
||||
let banking_clients = banking::client::init_all_banks().await;
|
||||
|
||||
let db_pool = PgPoolOptions::new()
|
||||
.max_connections(5)
|
||||
.connect(&database_url)
|
||||
.await
|
||||
.expect("Failed to create Postgres connection pool");
|
||||
|
||||
println!("✅ Database connection pool created successfully.");
|
||||
|
||||
let app_state = AppState { db_pool };
|
||||
let app_state = AppState {
|
||||
db_pool,
|
||||
banking_clients,
|
||||
};
|
||||
|
||||
let app = Router::new()
|
||||
.route("/api/health", get(health_checker_handler))
|
||||
.route("/api/health", get(handlers::health::health_handler))
|
||||
.route("/api/accounts/aggregated", get(handlers::accounts::get_aggregated_accounts))
|
||||
.with_state(app_state);
|
||||
|
||||
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
|
||||
|
|
@ -46,24 +50,15 @@ async fn main() {
|
|||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
async fn health_checker_handler(
|
||||
State(state): State<AppState>,
|
||||
) -> impl IntoResponse {
|
||||
let result = sqlx::query("SELECT 1")
|
||||
.execute(&state.db_pool)
|
||||
.await;
|
||||
|
||||
let (status_code, message) = match result {
|
||||
Ok(_) => (
|
||||
StatusCode::OK,
|
||||
"Database connection is successful.".to_string(),
|
||||
),
|
||||
Err(e) => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Database connection failed: {}", e),
|
||||
),
|
||||
};
|
||||
|
||||
(status_code, Json(json!({ "status": message })))
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
pub db_pool: sqlx::PgPool,
|
||||
pub banking_clients: BankingClients,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct BankingClients {
|
||||
pub vbank: banking::client::BankClient,
|
||||
pub abank: banking::client::BankClient,
|
||||
pub sbank: banking::client::BankClient,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue