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::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::{AuthPkceState, auth_callback, 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    pub(super) rate_limiter: Option<Arc<RateLimiter>>,
68    #[cfg(feature = "secrets")]
69    pub(super) secrets_manager: Option<Arc<crate::secrets_manager::SecretsManager>>,
70    #[cfg(feature = "federation")]
71    pub(super) circuit_breaker:
72        Option<Arc<crate::federation::circuit_breaker::FederationCircuitBreakerManager>>,
73    pub(super) error_sanitizer: Arc<crate::config::error_sanitization::ErrorSanitizer>,
74    #[cfg(feature = "auth")]
75    pub(super) state_encryption: Option<Arc<crate::auth::state_encryption::StateEncryptionService>>,
76    #[cfg(feature = "auth")]
77    pub(super) pkce_store: Option<Arc<crate::auth::PkceStateStore>>,
78    #[cfg(feature = "auth")]
79    pub(super) oidc_server_client: Option<Arc<crate::auth::OidcServerClient>>,
80    pub(super) api_key_authenticator: Option<Arc<crate::api_key::ApiKeyAuthenticator>>,
81    // Reason: only read inside #[cfg(feature = "auth")] blocks in routing.rs
82    #[allow(dead_code)] // Reason: field kept for API completeness; may be used in future features
83    pub(super) revocation_manager: Option<Arc<crate::token_revocation::TokenRevocationManager>>,
84    pub(super) apq_store: Option<fraiseql_core::apq::ArcApqStorage>,
85    pub(super) trusted_docs: Option<Arc<crate::trusted_documents::TrustedDocumentStore>>,
86
87    #[cfg(feature = "observers")]
88    pub(super) observer_runtime: Option<Arc<RwLock<ObserverRuntime>>>,
89
90    #[cfg(feature = "observers")]
91    pub(super) db_pool: Option<sqlx::PgPool>,
92
93    #[cfg(feature = "arrow")]
94    pub(super) flight_service: Option<FraiseQLFlightService>,
95
96    #[cfg(feature = "mcp")]
97    pub(super) mcp_config: Option<fraiseql_core::schema::McpConfig>,
98
99    /// Pool pressure monitoring configuration (loaded from `[pool_tuning]` in `fraiseql.toml`).
100    pub(super) pool_tuning_config: Option<crate::config::pool_tuning::PoolPressureMonitorConfig>,
101}