juiceback/
state.rs

1//! The shared state every request handler can access.
2
3use crate::config::Config;
4use crate::tus::TusMap;
5use r2d2::Pool;
6use r2d2_sqlite::SqliteConnectionManager;
7use std::sync::Arc;
8use std::time::Duration;
9
10/// A pool of SQLite connections, managed by r2d2.
11pub type DbPool = Pool<SqliteConnectionManager>;
12
13/// Shared state that every axum handler can access.
14pub struct AppState {
15    pub db: DbPool,
16    pub config: Arc<Config>,
17    pub http: reqwest::Client,
18    pub tus: TusMap,
19}
20
21impl AppState {
22    /// Build a new AppState, wrapping the pool and config in Arc.
23    pub fn new(pool: DbPool, config: Config) -> Arc<Self> {
24        Arc::new(Self {
25            db: pool,
26            config: Arc::new(config),
27            http: reqwest::Client::builder()
28                .timeout(Duration::from_secs(30))
29                .connect_timeout(Duration::from_secs(5))
30                .pool_idle_timeout(Duration::from_secs(30))
31                .build()
32                .expect("Failed to build reqwest client"),
33            tus: crate::tus::new_tus_state(),
34        })
35    }
36}