1pub mod auth;
14pub mod routes;
15
16use std::collections::HashMap;
17use std::sync::Arc;
18
19use axum::Router;
20use haima_core::bureau::{PaymentHistory, TrustContext};
21use haima_core::credit::CreditScore;
22use haima_core::lending::CreditLine;
23use haima_lago::FinancialState;
24use haima_x402::FacilitatorStatsCounter;
25use tokio::sync::RwLock;
26
27use crate::auth::AuthConfig;
28
29#[derive(Clone)]
31pub struct AppState {
32 pub financial_state: Arc<RwLock<FinancialState>>,
33 pub auth_config: Arc<AuthConfig>,
34 pub facilitator_stats: Arc<FacilitatorStatsCounter>,
36 pub facilitator_fee_bps: u32,
38 pub credit_scores: Arc<RwLock<HashMap<String, CreditScore>>>,
40 pub credit_lines: Arc<RwLock<HashMap<String, CreditLine>>>,
42 pub trust_contexts: Arc<RwLock<HashMap<String, TrustContext>>>,
44 pub payment_histories: Arc<RwLock<HashMap<String, PaymentHistory>>>,
46}
47
48impl AppState {
49 pub fn new(auth_config: AuthConfig) -> Self {
51 Self {
52 financial_state: Arc::new(RwLock::new(FinancialState::default())),
53 auth_config: Arc::new(auth_config),
54 facilitator_stats: Arc::new(FacilitatorStatsCounter::new()),
55 facilitator_fee_bps: haima_x402::DEFAULT_FEE_BPS,
56 credit_scores: Arc::new(RwLock::new(HashMap::new())),
57 credit_lines: Arc::new(RwLock::new(HashMap::new())),
58 trust_contexts: Arc::new(RwLock::new(HashMap::new())),
59 payment_histories: Arc::new(RwLock::new(HashMap::new())),
60 }
61 }
62}
63
64impl Default for AppState {
65 fn default() -> Self {
66 Self {
67 financial_state: Arc::new(RwLock::new(FinancialState::default())),
68 auth_config: Arc::new(AuthConfig { jwt_secret: None }),
69 facilitator_stats: Arc::new(FacilitatorStatsCounter::new()),
70 facilitator_fee_bps: haima_x402::DEFAULT_FEE_BPS,
71 credit_scores: Arc::new(RwLock::new(HashMap::new())),
72 credit_lines: Arc::new(RwLock::new(HashMap::new())),
73 trust_contexts: Arc::new(RwLock::new(HashMap::new())),
74 payment_histories: Arc::new(RwLock::new(HashMap::new())),
75 }
76 }
77}
78
79pub fn router(state: AppState) -> Router {
81 routes::routes(state)
82}