2025-12-03 18:42:22 -03:00
|
|
|
//! Application state management
|
|
|
|
|
//!
|
|
|
|
|
//! This module contains the shared application state that is passed to all
|
2025-12-04 09:33:31 -03:00
|
|
|
//! route handlers and provides access to the BotServer client.
|
2025-12-04 09:20:28 -03:00
|
|
|
|
2025-12-04 09:33:31 -03:00
|
|
|
use botlib::http_client::BotServerClient;
|
2025-12-03 18:42:22 -03:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
/// Application state shared across all handlers
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct AppState {
|
2025-12-04 09:33:31 -03:00
|
|
|
/// HTTP client for communicating with BotServer
|
|
|
|
|
pub client: Arc<BotServerClient>,
|
2025-12-03 18:42:22 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AppState {
|
|
|
|
|
/// Create a new application state
|
2025-12-04 09:33:31 -03:00
|
|
|
///
|
|
|
|
|
/// Uses BOTSERVER_URL environment variable if set, otherwise defaults to localhost:8080
|
2025-12-03 18:42:22 -03:00
|
|
|
pub fn new() -> Self {
|
2025-12-04 09:33:31 -03:00
|
|
|
let url = std::env::var("BOTSERVER_URL").ok();
|
2025-12-03 18:42:22 -03:00
|
|
|
Self {
|
2025-12-04 09:33:31 -03:00
|
|
|
client: Arc::new(BotServerClient::new(url)),
|
2025-12-03 18:42:22 -03:00
|
|
|
}
|
|
|
|
|
}
|
2025-12-04 09:33:31 -03:00
|
|
|
|
|
|
|
|
/// Check if the BotServer is healthy
|
|
|
|
|
pub async fn health_check(&self) -> bool {
|
|
|
|
|
self.client.health_check().await
|
|
|
|
|
}
|
2025-12-03 18:42:22 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for AppState {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::new()
|
|
|
|
|
}
|
|
|
|
|
}
|