pub struct AppState {
pub config: Arc<Config>,
pub db: DbPool,
pub http: Client,
pub identity: Arc<IdentityResolver>,
pub extensions: Arc<Extensions>,
}Expand description
Shared application state passed to every Axum handler.
This is the central state object that every route handler receives via
axum::extract::State<AppState>. It holds the parsed configuration,
database connection pool, and a shared HTTP client for outbound requests.
AppState is cheaply cloneable — all inner fields are either Arc-wrapped
or already use internal reference counting (e.g. sqlx pools, reqwest::Client).
Fields§
§config: Arc<Config>Parsed configuration from atrg.toml.
db: DbPoolDatabase connection pool. May be SQLite or PostgreSQL depending on
the [database] url scheme in atrg.toml (and which features are
compiled in to atrg-db).
http: ClientShared HTTP client for outbound requests.
identity: Arc<IdentityResolver>DID/handle resolver with TTL-backed in-memory cache.
extensions: Arc<Extensions>Type-erased container for app-specific state (S3 clients, SMTP config,
domain-specific services, etc.). Access via AppState::extension or
AppState::try_extension.
Implementations§
Source§impl AppState
impl AppState
Sourcepub fn extension<T: Send + Sync + 'static>(&self) -> &T
pub fn extension<T: Send + Sync + 'static>(&self) -> &T
Retrieve a reference to an app-specific extension by type.
§Panics
Panics if the extension has not been registered. Use
try_extension for a non-panicking variant.
§Examples
struct MyService { url: String }
// In a handler:
async fn my_handler(State(state): State<AppState>) -> impl IntoResponse {
let svc = state.extension::<MyService>();
Json(json!({ "url": svc.url }))
}Sourcepub fn has_extension<T: Send + Sync + 'static>(&self) -> bool
pub fn has_extension<T: Send + Sync + 'static>(&self) -> bool
Returns true if an extension of type T has been registered.
Trait Implementations§
Source§impl FromRef<AppState> for Arc<Extensions>
impl FromRef<AppState> for Arc<Extensions>
Auto Trait Implementations§
impl Freeze for AppState
impl !RefUnwindSafe for AppState
impl Send for AppState
impl Sync for AppState
impl Unpin for AppState
impl UnsafeUnpin for AppState
impl !UnwindSafe 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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