Skip to main content

fraiseql_server/server/
mod.rs

1//! HTTP server implementation.
2
3use std::sync::Arc;
4
5#[cfg(feature = "arrow")]
6use fraiseql_arrow::FraiseQLFlightService;
7use fraiseql_core::{
8    db::traits::DatabaseAdapter,
9    runtime::{Executor, SubscriptionManager},
10    security::{AuthMiddleware, OidcValidator},
11};
12#[cfg(feature = "observers")]
13use {
14    crate::observers::{ObserverRuntime, ObserverRuntimeConfig},
15    tokio::sync::RwLock,
16};
17
18#[cfg(feature = "auth")]
19use crate::routes::{AuthMeState, AuthPkceState, auth_callback, auth_me, auth_start};
20use crate::{
21    Result, ServerError,
22    middleware::{
23        BearerAuthState, OidcAuthState, RateLimiter, bearer_auth_middleware, cors_layer_restricted,
24        metrics_middleware, oidc_auth_middleware, require_json_content_type, trace_layer,
25    },
26    routes::{
27        PlaygroundState, SubscriptionState, api, graphql::AppState, graphql_get_handler,
28        graphql_handler, health_handler, introspection_handler, metrics_handler,
29        metrics_json_handler, playground_handler, readiness_handler, subscription_handler,
30    },
31    server_config::ServerConfig,
32    tls::TlsSetup,
33};
34
35mod builder;
36mod extensions;
37mod initialization;
38mod lifecycle;
39mod routing;
40
41/// FraiseQL HTTP Server.
42///
43/// `Server<A>` is generic over a `DatabaseAdapter` implementation, which allows
44/// swapping database backends and injecting mock adapters in tests.
45///
46/// # Feature: `observers`
47///
48/// When compiled with the `observers` Cargo feature, the server mounts observer
49/// management and runtime-health API endpoints under `/api/observers`. These
50/// endpoints require a live **PostgreSQL** connection pool (`sqlx::PgPool`).
51///
52/// Pass `Some(pg_pool)` as the `db_pool` argument to [`Server::new`] when the
53/// `observers` feature is enabled. Passing `None` causes the observer routes to
54/// be skipped at startup (an error is logged) rather than panicking, but the
55/// rest of the server continues to function normally.
56///
57/// The PostgreSQL pool is distinct from the generic `DatabaseAdapter`: the
58/// adapter handles application queries, while the pool is used exclusively by
59/// the observer subsystem to store and retrieve reactive rule metadata.
60pub struct Server<A: DatabaseAdapter> {
61    pub(super) config: ServerConfig,
62    pub(super) executor: Arc<Executor<A>>,
63    pub(super) subscription_manager: Arc<SubscriptionManager>,
64    pub(super) subscription_lifecycle: Arc<dyn crate::subscriptions::SubscriptionLifecycle>,
65    pub(super) max_subscriptions_per_connection: Option<u32>,
66    pub(super) oidc_validator: Option<Arc<OidcValidator>>,
67    /// Local HS256 JWT validator (alternative to `oidc_validator`).
68    ///
69    /// When set, the GraphQL endpoint is protected by shared-secret JWT
70    /// validation instead of OIDC. Intended for integration testing and
71    /// internal service-to-service auth.
72    pub(super) hs256_auth: Option<Arc<AuthMiddleware>>,
73    pub(super) rate_limiter: Option<Arc<RateLimiter>>,
74    #[cfg(feature = "secrets")]
75    pub(super) secrets_manager: Option<Arc<crate::secrets_manager::SecretsManager>>,
76    #[cfg(feature = "federation")]
77    pub(super) circuit_breaker:
78        Option<Arc<crate::federation::circuit_breaker::FederationCircuitBreakerManager>>,
79    pub(super) error_sanitizer: Arc<crate::config::error_sanitization::ErrorSanitizer>,
80    #[cfg(feature = "auth")]
81    pub(super) state_encryption: Option<Arc<crate::auth::state_encryption::StateEncryptionService>>,
82    #[cfg(feature = "auth")]
83    pub(super) pkce_store: Option<Arc<crate::auth::PkceStateStore>>,
84    #[cfg(feature = "auth")]
85    pub(super) oidc_server_client: Option<Arc<crate::auth::OidcServerClient>>,
86    pub(super) api_key_authenticator: Option<Arc<crate::api_key::ApiKeyAuthenticator>>,
87    // Reason: only read inside #[cfg(feature = "auth")] blocks in routing.rs
88    #[allow(dead_code)] // Reason: field kept for API completeness; may be used in future features
89    pub(super) revocation_manager: Option<Arc<crate::token_revocation::TokenRevocationManager>>,
90    pub(super) apq_store: Option<fraiseql_core::apq::ArcApqStorage>,
91    pub(super) trusted_docs: Option<Arc<crate::trusted_documents::TrustedDocumentStore>>,
92
93    #[cfg(feature = "observers")]
94    pub(super) observer_runtime: Option<Arc<RwLock<ObserverRuntime>>>,
95
96    #[cfg(feature = "observers")]
97    pub(super) db_pool: Option<sqlx::PgPool>,
98
99    #[cfg(feature = "arrow")]
100    pub(super) flight_service: Option<FraiseQLFlightService>,
101
102    #[cfg(feature = "mcp")]
103    pub(super) mcp_config: Option<fraiseql_core::schema::McpConfig>,
104
105    /// Pool pressure monitoring configuration (loaded from `[pool_tuning]` in `fraiseql.toml`).
106    pub(super) pool_tuning_config: Option<crate::config::pool_tuning::PoolPressureMonitorConfig>,
107
108    /// Whether the adapter-level query result cache (`CachedDatabaseAdapter`) is active.
109    ///
110    /// Set to `true` when `ServerConfig::cache_enabled = true` and the server was built
111    /// with `Server::new` or `Server::with_relay_pagination`.
112    pub(super) adapter_cache_enabled: bool,
113}