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 tokio_util::sync::CancellationToken;
11use tuitbot_core::automation::circuit_breaker::CircuitBreaker;
12use tuitbot_core::automation::Runtime;
13use tuitbot_core::config::{ContentSourcesConfig, DeploymentMode};
14use tuitbot_core::content::ContentGenerator;
15use tuitbot_core::storage::DbPool;
16
17use crate::ws::WsEvent;
18
19/// Shared application state accessible by all route handlers.
20pub struct AppState {
21    /// SQLite connection pool.
22    pub db: DbPool,
23    /// Path to the configuration file.
24    pub config_path: PathBuf,
25    /// Data directory for media storage (parent of config file).
26    pub data_dir: PathBuf,
27    /// Broadcast channel sender for real-time WebSocket events.
28    pub event_tx: broadcast::Sender<WsEvent>,
29    /// Local bearer token for API authentication.
30    pub api_token: String,
31    /// Bcrypt hash of the web login passphrase (None if not configured).
32    pub passphrase_hash: RwLock<Option<String>>,
33    /// Host address the server is bound to.
34    pub bind_host: String,
35    /// Port the server is listening on.
36    pub bind_port: u16,
37    /// Per-IP login attempt tracking for rate limiting: (count, window_start).
38    pub login_attempts: Mutex<HashMap<IpAddr, (u32, Instant)>>,
39    /// Per-account automation runtimes (keyed by account_id).
40    pub runtimes: Mutex<HashMap<String, Runtime>>,
41    /// Per-account content generators for AI assist endpoints.
42    pub content_generators: Mutex<HashMap<String, Arc<ContentGenerator>>>,
43    /// Optional circuit breaker for X API rate-limit protection.
44    pub circuit_breaker: Option<Arc<CircuitBreaker>>,
45    /// Cancellation token for the Watchtower filesystem watcher (None if not running).
46    pub watchtower_cancel: Option<CancellationToken>,
47    /// Content sources configuration for the Watchtower.
48    pub content_sources: ContentSourcesConfig,
49    /// Deployment mode (desktop, self_host, or cloud).
50    pub deployment_mode: DeploymentMode,
51}