Skip to main content

webdav_server/app/
state.rs

1use std::path::Path;
2
3use moka::future::Cache;
4use sqlx::SqlitePool;
5
6use crate::{model::session::TokenHash, storage};
7
8pub type UserId = i64;
9
10/// The shared application state, injected into every route handler by Axum.
11///
12/// `State` is cloned cheaply on each request — all fields are either reference-counted
13/// (`SqlitePool`, `Cache`) or backed by an `Arc` internally, so cloning is just
14/// incrementing a reference count.
15///
16/// Access it in handlers via `State(state): State<AppState>`.
17#[derive(Clone)]
18pub struct State {
19    /// The storage service managing the on-disk CAS vault.
20    pub storage: storage::Service,
21    /// The `SQLite` connection pool for all database queries.
22    pub db: SqlitePool,
23    /// In-memory session cache. Checked before every database lookup in `auth_guard`
24    /// to avoid hitting the disk on every authenticated request.
25    pub session_cache: Cache<TokenHash, UserId>,
26}
27
28impl State {
29    /// Returns a reference to the vault directory path.
30    ///
31    /// This is a convenience accessor that delegates to the storage service.
32    #[must_use]
33    pub fn vault_path(&self) -> &Path {
34        &self.storage.vault_path
35    }
36}