postrust_server/
state.rs

1//! Application state.
2
3use postrust_auth::JwtConfig;
4use postrust_core::{AppConfig, SchemaCache};
5use sqlx::PgPool;
6use std::sync::Arc;
7use tokio::sync::RwLock;
8
9/// Shared application state.
10pub struct AppState {
11    /// Database connection pool
12    pub pool: PgPool,
13    /// Cached schema metadata
14    pub schema_cache: RwLock<SchemaCache>,
15    /// Application configuration
16    pub config: AppConfig,
17    /// JWT configuration
18    pub jwt_config: JwtConfig,
19}
20
21impl AppState {
22    /// Get a read lock on the schema cache.
23    pub async fn schema_cache(&self) -> tokio::sync::RwLockReadGuard<'_, SchemaCache> {
24        self.schema_cache.read().await
25    }
26
27    /// Reload the schema cache.
28    pub async fn reload_schema(&self) -> Result<(), postrust_core::Error> {
29        let new_cache = SchemaCache::load(&self.pool, &self.config.db_schemas).await?;
30        let mut guard = self.schema_cache.write().await;
31        *guard = new_cache;
32        Ok(())
33    }
34
35    /// Get the default schema.
36    pub fn default_schema(&self) -> &str {
37        self.config.default_schema()
38    }
39
40    /// Get exposed schemas.
41    pub fn schemas(&self) -> &[String] {
42        &self.config.db_schemas
43    }
44}