Skip to main content

tuitbot_server/
state.rs

1//! Shared application state for the tuitbot server.
2
3use std::collections::HashMap;
4use std::net::IpAddr;
5use std::path::PathBuf;
6use std::sync::Arc;
7use std::time::Instant;
8
9use tokio::sync::{broadcast, Mutex, RwLock};
10use tuitbot_core::automation::circuit_breaker::CircuitBreaker;
11use tuitbot_core::automation::Runtime;
12use tuitbot_core::content::ContentGenerator;
13use tuitbot_core::storage::DbPool;
14
15use crate::ws::WsEvent;
16
17/// Shared application state accessible by all route handlers.
18pub struct AppState {
19    /// SQLite connection pool.
20    pub db: DbPool,
21    /// Path to the configuration file.
22    pub config_path: PathBuf,
23    /// Data directory for media storage (parent of config file).
24    pub data_dir: PathBuf,
25    /// Broadcast channel sender for real-time WebSocket events.
26    pub event_tx: broadcast::Sender<WsEvent>,
27    /// Local bearer token for API authentication.
28    pub api_token: String,
29    /// Bcrypt hash of the web login passphrase (None if not configured).
30    pub passphrase_hash: RwLock<Option<String>>,
31    /// Host address the server is bound to.
32    pub bind_host: String,
33    /// Port the server is listening on.
34    pub bind_port: u16,
35    /// Per-IP login attempt tracking for rate limiting: (count, window_start).
36    pub login_attempts: Mutex<HashMap<IpAddr, (u32, Instant)>>,
37    /// Per-account automation runtimes (keyed by account_id).
38    pub runtimes: Mutex<HashMap<String, Runtime>>,
39    /// Per-account content generators for AI assist endpoints.
40    pub content_generators: Mutex<HashMap<String, Arc<ContentGenerator>>>,
41    /// Optional circuit breaker for X API rate-limit protection.
42    pub circuit_breaker: Option<Arc<CircuitBreaker>>,
43}