use postrust_auth::JwtConfig;
use postrust_core::{AppConfig, SchemaCache};
use sqlx::PgPool;
use std::sync::Arc;
use tokio::sync::RwLock;
pub struct AppState {
pub pool: PgPool,
pub schema_cache: RwLock<SchemaCache>,
pub config: AppConfig,
pub jwt_config: JwtConfig,
}
impl AppState {
pub async fn schema_cache(&self) -> tokio::sync::RwLockReadGuard<'_, SchemaCache> {
self.schema_cache.read().await
}
pub async fn reload_schema(&self) -> Result<(), postrust_core::Error> {
let new_cache = SchemaCache::load(&self.pool, &self.config.db_schemas).await?;
let mut guard = self.schema_cache.write().await;
*guard = new_cache;
Ok(())
}
pub fn default_schema(&self) -> &str {
self.config.default_schema()
}
pub fn schemas(&self) -> &[String] {
&self.config.db_schemas
}
}