Skip to main content

capy_core/
lib.rs

1//! Typed configuration, shared application state, and safe application errors.
2
3mod auth_config;
4mod config;
5pub use auth_config::{AuthSettings, SameSite};
6mod error;
7
8pub use config::{
9    ApiSettings, AppConfig, AppSettings, DatabaseSettings, HttpSettings, ServerSettings,
10};
11pub use error::{ApiErrorResponse, AppError, ErrorKind, Result, ValidationErrors};
12use std::sync::Arc;
13
14/// Cheaply cloned state shared by every request.
15#[derive(Clone, Debug)]
16pub struct AppContext {
17    /// Validated, immutable application configuration.
18    pub config: Arc<AppConfig>,
19    /// SeaORM's native connection pool.
20    pub db: sea_orm::DatabaseConnection,
21}
22
23/// Extract typed application state using `Context(ctx): Context`.
24#[derive(Clone, Debug)]
25pub struct Context(pub AppContext);
26
27impl axum::extract::FromRequestParts<AppContext> for Context {
28    type Rejection = std::convert::Infallible;
29
30    async fn from_request_parts(
31        _parts: &mut axum::http::request::Parts,
32        state: &AppContext,
33    ) -> std::result::Result<Self, Self::Rejection> {
34        Ok(Self(state.clone()))
35    }
36}