Skip to main content

tuitbot_server/
state.rs

1//! Shared application state for the tuitbot server.
2
3use std::collections::HashMap;
4use std::path::PathBuf;
5use std::sync::Arc;
6
7use tokio::sync::{broadcast, Mutex};
8use tuitbot_core::automation::circuit_breaker::CircuitBreaker;
9use tuitbot_core::automation::Runtime;
10use tuitbot_core::content::ContentGenerator;
11use tuitbot_core::storage::DbPool;
12
13use crate::ws::WsEvent;
14
15/// Shared application state accessible by all route handlers.
16pub struct AppState {
17    /// SQLite connection pool.
18    pub db: DbPool,
19    /// Path to the configuration file.
20    pub config_path: PathBuf,
21    /// Data directory for media storage (parent of config file).
22    pub data_dir: PathBuf,
23    /// Broadcast channel sender for real-time WebSocket events.
24    pub event_tx: broadcast::Sender<WsEvent>,
25    /// Local bearer token for API authentication.
26    pub api_token: String,
27    /// Per-account automation runtimes (keyed by account_id).
28    pub runtimes: Mutex<HashMap<String, Runtime>>,
29    /// Per-account content generators for AI assist endpoints.
30    pub content_generators: Mutex<HashMap<String, Arc<ContentGenerator>>>,
31    /// Optional circuit breaker for X API rate-limit protection.
32    pub circuit_breaker: Option<Arc<CircuitBreaker>>,
33}