#[non_exhaustive]pub struct AppState {
pub pool: PgPool,
pub schema_cache: RwLock<SchemaCache>,
pub config: AppConfig,
pub jwt_config: JwtConfig,
pub jwt_cache: Option<JwtCache>,
}Expand description
Shared application state.
#[non_exhaustive] from 3.0.0: build one with AppState::new, then
assign any field that should differ from what the configuration says.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.pool: PgPoolDatabase connection pool
schema_cache: RwLock<SchemaCache>Cached schema metadata
config: AppConfigApplication configuration
jwt_config: JwtConfigJWT configuration
jwt_cache: Option<JwtCache>Cache of validated tokens, when jwt_cache_enabled asks for one.
Implementations§
Source§impl AppState
impl AppState
Sourcepub fn new(
pool: PgPool,
schema_cache: SchemaCache,
config: AppConfig,
) -> Result<Self, JwtKeyError>
pub fn new( pool: PgPool, schema_cache: SchemaCache, config: AppConfig, ) -> Result<Self, JwtKeyError>
State for config, serving schema_cache through pool.
The JWT configuration and token cache are derived from config, as
build derives them. Fails when the configured JWT
secret does not parse; see postrust_engine::jwt_config. With
PGRST_JWT_JWKS_URL set the keys are fetched when first needed, by
the request that needs them; build also fetches them
before serving, and keeps them fresh in the background.
Sourcepub async fn schema_cache(&self) -> RwLockReadGuard<'_, SchemaCache>
pub async fn schema_cache(&self) -> RwLockReadGuard<'_, SchemaCache>
Get a read lock on the schema cache.
Sourcepub async fn reload_schema(&self) -> Result<(), Error>
pub async fn reload_schema(&self) -> Result<(), Error>
Reload the schema cache. Used by the db_channel_enabled listener.
Sourcepub fn default_schema(&self) -> &str
pub fn default_schema(&self) -> &str
Get the default schema.
Sourcepub async fn begin_as(
&self,
user: &User,
) -> Result<Transaction<'static, Postgres>, Error>
pub async fn begin_as( &self, user: &User, ) -> Result<Transaction<'static, Postgres>, Error>
A transaction on the pool that runs as user, as a REST request
would: read-write, at the isolation level the configuration gives the
role, with db_pre_request run. See AppState::begin_as_with.
Sourcepub async fn begin_as_with(
&self,
user: &User,
options: TxOptions,
) -> Result<Transaction<'static, Postgres>, Error>
pub async fn begin_as_with( &self, user: &User, options: TxOptions, ) -> Result<Transaction<'static, Postgres>, Error>
A transaction on the pool that runs as user.
The pool is logged in as the authenticator, and a query sent on it
runs as the authenticator: with every grant it has, and with no
request.jwt.claims for a row-level security policy to read, so the
policies written for the API’s roles do not apply. This makes the
transaction the caller’s before you send anything on it. The same
statements REST sends for a request, worked out by the same
Session: SET TRANSACTION where one is
asked for, the role, the claims, the facts about the request user
carries, the search_path and app.settings.*, and db_pre_request.
Every one of them is local to the transaction. Commit it or drop it – a dropped one is rolled back – and the connection goes back to the pool as the authenticator, with nothing set.
What this does not do is make your handler untrusted code. It keeps
what a request sends inside the policy: every statement you run with
the caller’s values in it runs as the caller. But the handler runs as
the authenticator, which may always switch back – RESET ROLE on the
transaction is enough – and it holds the pool. Treat it as you would a
SECURITY DEFINER function: code you wrote and reviewed, not something
the client controls.
Fails as REST fails: a connection that cannot be had from the pool in
time is logged at error and answered 500 (PGRST500), and a session the database
refuses – a role that does not exist, a pre-request function that
raises – is answered as the database’s error.
Trait Implementations§
Source§impl State for AppState
impl State for AppState
Source§type SchemaCache<'a> = RwLockReadGuard<'a, SchemaCache>
type SchemaCache<'a> = RwLockReadGuard<'a, SchemaCache>
Source§async fn schema_cache(&self) -> RwLockReadGuard<'_, SchemaCache>
async fn schema_cache(&self) -> RwLockReadGuard<'_, SchemaCache>
Source§fn jwt_config(&self) -> &JwtConfig
fn jwt_config(&self) -> &JwtConfig
Source§fn jwt_cache(&self) -> Option<&JwtCache>
fn jwt_cache(&self) -> Option<&JwtCache>
Source§fn default_schema(&self) -> &str
fn default_schema(&self) -> &str
Auto Trait Implementations§
impl !Freeze for AppState
impl !RefUnwindSafe for AppState
impl !UnwindSafe for AppState
impl Send for AppState
impl Sync for AppState
impl Unpin for AppState
impl UnsafeUnpin for AppState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more