1use postrust_auth::JwtConfig;
4use postrust_core::{AppConfig, SchemaCache};
5use sqlx::PgPool;
6use tokio::sync::RwLock;
7
8pub struct AppState {
10 pub pool: PgPool,
12 pub schema_cache: RwLock<SchemaCache>,
14 pub config: AppConfig,
16 pub jwt_config: JwtConfig,
18}
19
20impl AppState {
21 pub async fn schema_cache(&self) -> tokio::sync::RwLockReadGuard<'_, SchemaCache> {
23 self.schema_cache.read().await
24 }
25
26 #[allow(dead_code)] pub async fn reload_schema(&self) -> Result<(), postrust_core::Error> {
29 let new_cache = SchemaCache::load_with_search_path(
30 &self.pool,
31 &self.config.db_schemas,
32 &self.config.db_extra_search_path,
33 )
34 .await?;
35 let mut guard = self.schema_cache.write().await;
36 *guard = new_cache;
37 Ok(())
38 }
39
40 pub fn default_schema(&self) -> &str {
42 self.config.default_schema()
43 }
44
45 pub fn schemas(&self) -> &[String] {
47 &self.config.db_schemas
48 }
49}