postrust_engine/state.rs
1//! What a request runs against: a database, a schema cache, configuration.
2
3use postrust_auth::{JwtCache, JwtConfig};
4use postrust_core::db::{Db, MaybeSend, MaybeSync};
5use postrust_core::{AppConfig, SchemaCache};
6use std::future::Future;
7use std::ops::Deref;
8
9/// Everything the pipeline needs from its host.
10///
11/// The server's `AppState` is one, over a sqlx pool. A host with no sqlx -- a
12/// Cloudflare Worker -- brings its own, over its own [`Db`]. The pipeline is
13/// generic over this, so nothing on the native path goes through a vtable.
14pub trait State: MaybeSync {
15 /// The database requests run on.
16 type Db: Db;
17
18 /// A borrow of the schema cache, held for as long as a request needs it.
19 type SchemaCache<'a>: Deref<Target = SchemaCache> + MaybeSend + MaybeSync
20 where
21 Self: 'a;
22
23 /// The database requests run on.
24 fn db(&self) -> &Self::Db;
25
26 /// The schema cache.
27 fn schema_cache(&self) -> impl Future<Output = Self::SchemaCache<'_>> + MaybeSend;
28
29 /// Configuration.
30 fn config(&self) -> &AppConfig;
31
32 /// How tokens are verified.
33 fn jwt_config(&self) -> &JwtConfig;
34
35 /// The cache of verified tokens, where one is configured.
36 fn jwt_cache(&self) -> Option<&JwtCache>;
37
38 /// The schema a request names when it names none.
39 fn default_schema(&self) -> &str {
40 self.config().default_schema()
41 }
42
43 /// The schemas the API exposes.
44 fn schemas(&self) -> &[String] {
45 &self.config().db_schemas
46 }
47}