1use postrust_auth::JwtConfig;
4use postrust_core::{AppConfig, SchemaCache};
5use sqlx::PgPool;
6use std::sync::Arc;
7use tokio::sync::RwLock;
8
9pub struct AppState {
11 pub pool: PgPool,
13 pub schema_cache: RwLock<SchemaCache>,
15 pub config: AppConfig,
17 pub jwt_config: JwtConfig,
19}
20
21impl AppState {
22 pub async fn schema_cache(&self) -> tokio::sync::RwLockReadGuard<'_, SchemaCache> {
24 self.schema_cache.read().await
25 }
26
27 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 pub fn default_schema(&self) -> &str {
37 self.config.default_schema()
38 }
39
40 pub fn schemas(&self) -> &[String] {
42 &self.config.db_schemas
43 }
44}