botui/src/shared/state.rs

38 lines
972 B
Rust
Raw Normal View History

2025-12-03 18:42:22 -03:00
//! Application state management
//!
//! This module contains the shared application state that is passed to all
//! route handlers and provides access to the BotServer client.
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 {
/// 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
///
/// Uses BOTSERVER_URL environment variable if set, otherwise defaults to localhost:8080
2025-12-03 18:42:22 -03:00
pub fn new() -> Self {
let url = std::env::var("BOTSERVER_URL").ok();
2025-12-03 18:42:22 -03:00
Self {
client: Arc::new(BotServerClient::new(url)),
2025-12-03 18:42:22 -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()
}
}