1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! Server configuration (`*Config` types).
//!
//! These are developer-facing configuration types loaded from `fraiseql.toml`,
//! environment variables, or CLI flags. They are mutable between deployments.
//!
//! For the distinction between `*Config` (developer-facing, mutable) and
//! `*Settings` (compiled into `schema.compiled.json`, immutable at runtime),
//! see `docs/architecture/config-vs-settings.md`.
pub(crate) mod defaults;
pub mod hs256;
mod methods;
pub mod observers;
pub mod tls;
#[cfg(test)]
mod tests;
use std::{net::SocketAddr, path::PathBuf};
use defaults::{
default_bind_addr, default_database_url, default_graphql_path, default_health_path,
default_introspection_path, default_max_header_bytes, default_max_header_count,
default_max_request_body_bytes, default_metrics_json_path, default_metrics_path,
default_playground_path, default_pool_max_size, default_pool_min_size, default_pool_timeout,
default_readiness_path, default_schema_path, default_shutdown_timeout_secs,
default_subscription_path,
};
use fraiseql_core::security::OidcConfig;
pub use hs256::Hs256Config;
pub use observers::AdmissionConfig;
#[cfg(feature = "observers")]
pub use observers::{ObserverConfig, ObserverPoolConfig};
use serde::{Deserialize, Serialize};
pub use tls::{DatabaseTlsConfig, PlaygroundTool, TlsServerConfig};
use crate::middleware::RateLimitConfig;
/// Server configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerConfig {
/// Path to compiled schema JSON file.
#[serde(default = "defaults::default_schema_path")]
pub schema_path: PathBuf,
/// Database connection URL (PostgreSQL, MySQL, SQLite, SQL Server).
#[serde(default = "defaults::default_database_url")]
pub database_url: String,
/// Server bind address.
#[serde(default = "defaults::default_bind_addr")]
pub bind_addr: SocketAddr,
/// Arrow Flight gRPC bind address (requires `arrow` feature).
///
/// Defaults to `0.0.0.0:50051`. Override with `FRAISEQL_FLIGHT_BIND_ADDR`
/// environment variable or this field in the config file.
#[cfg(feature = "arrow")]
#[serde(default = "defaults::default_flight_bind_addr")]
pub flight_bind_addr: SocketAddr,
/// Enable CORS.
#[serde(default = "defaults::default_true")]
pub cors_enabled: bool,
/// CORS allowed origins (if empty, allows all).
#[serde(default)]
pub cors_origins: Vec<String>,
/// Enable framework-level response compression.
///
/// Defaults to `false`. In production FraiseQL is typically deployed
/// behind a reverse proxy (Nginx, Caddy, cloud load balancer) that
/// handles compression more efficiently (brotli, shared across upstreams,
/// cacheable). Enable this only for single-binary / no-proxy deployments.
#[serde(default = "defaults::default_false")]
pub compression_enabled: bool,
/// Enable request tracing.
#[serde(default = "defaults::default_true")]
pub tracing_enabled: bool,
/// OTLP exporter endpoint for distributed tracing.
///
/// When set (e.g. `"http://otel-collector:4317"`), the server initializes an
/// `OpenTelemetry` OTLP exporter. When `None`, the `OTEL_EXPORTER_OTLP_ENDPOINT`
/// environment variable is checked as a fallback. If neither is set, no OTLP
/// export occurs (zero overhead).
#[serde(default)]
pub otlp_endpoint: Option<String>,
/// OTLP exporter timeout in seconds (default: 10).
#[serde(default = "defaults::default_otlp_timeout_secs")]
pub otlp_export_timeout_secs: u64,
/// Service name for distributed tracing (default: `"fraiseql"`).
#[serde(default = "defaults::default_service_name")]
pub tracing_service_name: String,
/// Enable APQ (Automatic Persisted Queries).
#[serde(default = "defaults::default_true")]
pub apq_enabled: bool,
/// Enable query caching.
#[serde(default = "defaults::default_true")]
pub cache_enabled: bool,
/// GraphQL endpoint path.
#[serde(default = "defaults::default_graphql_path")]
pub graphql_path: String,
/// Health check endpoint path (liveness probe).
///
/// Returns 200 as long as the process is alive, 503 if the database is down.
#[serde(default = "defaults::default_health_path")]
pub health_path: String,
/// Readiness probe endpoint path.
///
/// Returns 200 when the server is ready to serve traffic (database reachable),
/// 503 otherwise. Kubernetes `readinessProbe` should point here.
#[serde(default = "defaults::default_readiness_path")]
pub readiness_path: String,
/// Introspection endpoint path.
#[serde(default = "defaults::default_introspection_path")]
pub introspection_path: String,
/// Metrics endpoint path (Prometheus format).
#[serde(default = "defaults::default_metrics_path")]
pub metrics_path: String,
/// Metrics JSON endpoint path.
#[serde(default = "defaults::default_metrics_json_path")]
pub metrics_json_path: String,
/// Playground (GraphQL IDE) endpoint path.
#[serde(default = "defaults::default_playground_path")]
pub playground_path: String,
/// Enable GraphQL playground/IDE (default: false for production safety).
///
/// When enabled, serves a GraphQL IDE (`GraphiQL` or Apollo Sandbox)
/// at the configured `playground_path`.
///
/// **Security**: Disabled by default for production safety. Set to true for development
/// environments only. The playground exposes schema information and can be a
/// reconnaissance vector for attackers.
#[serde(default)]
pub playground_enabled: bool,
/// Which GraphQL IDE to use.
///
/// - `graphiql`: The classic GraphQL IDE (default)
/// - `apollo-sandbox`: Apollo's embeddable sandbox
#[serde(default)]
pub playground_tool: PlaygroundTool,
/// `WebSocket` endpoint path for GraphQL subscriptions.
#[serde(default = "defaults::default_subscription_path")]
pub subscription_path: String,
/// Enable GraphQL subscriptions over `WebSocket`.
///
/// When enabled, provides graphql-ws (graphql-transport-ws) protocol
/// support for real-time subscription events.
#[serde(default = "defaults::default_true")]
pub subscriptions_enabled: bool,
/// Enable metrics endpoints.
///
/// **Security**: Disabled by default for production safety.
/// When enabled, requires `metrics_token` to be set for authentication.
#[serde(default)]
pub metrics_enabled: bool,
/// Bearer token for metrics endpoint authentication.
///
/// Required when `metrics_enabled` is true. Requests must include:
/// `Authorization: Bearer <token>`
///
/// **Security**: Use a strong, random token (e.g., 32+ characters).
#[serde(default)]
pub metrics_token: Option<String>,
/// Enable admin API endpoints (default: false for production safety).
///
/// **Security**: Disabled by default. When enabled, requires `admin_token` to be set.
/// Admin endpoints allow schema reloading, cache management, and config inspection.
#[serde(default)]
pub admin_api_enabled: bool,
/// Bearer token for admin API authentication.
///
/// Required when `admin_api_enabled` is true. Requests must include:
/// `Authorization: Bearer <token>`
///
/// **Security**: Use a strong, random token (minimum 32 characters).
/// This token grants access to **destructive** admin operations:
/// `reload-schema`, `cache/clear`.
///
/// If `admin_readonly_token` is set, this token is restricted to write
/// operations only. If `admin_readonly_token` is not set, this token
/// also grants access to read-only endpoints (backwards-compatible).
#[serde(default)]
pub admin_token: Option<String>,
/// Optional separate bearer token for read-only admin operations.
///
/// When set, restricts `admin_token` to destructive operations only
/// (`reload-schema`, `cache/clear`) and uses this token for read-only
/// endpoints (`config`, `cache/stats`, `explain`, `grafana-dashboard`).
///
/// Operators and monitoring tools can use this token without gaining
/// the ability to modify server state or reload the schema.
///
/// **Security**: Must be different from `admin_token` and at least 32
/// characters. Requires `admin_api_enabled = true` and `admin_token` set.
#[serde(default)]
pub admin_readonly_token: Option<String>,
/// Enable introspection endpoint (default: false for production safety).
///
/// **Security**: Disabled by default. When enabled, the introspection endpoint
/// exposes the complete GraphQL schema structure. Combined with `introspection_require_auth`,
/// you can optionally protect it with OIDC authentication.
#[serde(default)]
pub introspection_enabled: bool,
/// Require authentication for introspection endpoint (default: true).
///
/// When true and OIDC is configured, introspection requires same auth as GraphQL endpoint.
/// When false, introspection is publicly accessible (use only in development).
#[serde(default = "defaults::default_true")]
pub introspection_require_auth: bool,
/// Require authentication for design audit API endpoints (default: true).
///
/// Design audit endpoints expose system architecture and optimization opportunities.
/// When true and OIDC is configured, design endpoints require same auth as GraphQL endpoint.
/// When false, design endpoints are publicly accessible (use only in development).
#[serde(default = "defaults::default_true")]
pub design_api_require_auth: bool,
/// Database connection pool minimum size.
#[serde(default = "defaults::default_pool_min_size")]
pub pool_min_size: usize,
/// Database connection pool maximum size.
#[serde(default = "defaults::default_pool_max_size")]
pub pool_max_size: usize,
/// Database connection pool timeout in seconds.
#[serde(default = "defaults::default_pool_timeout")]
pub pool_timeout_secs: u64,
/// OIDC authentication configuration (optional).
///
/// When set, enables JWT authentication using OIDC discovery.
/// Supports Auth0, Keycloak, Okta, Cognito, Azure AD, and any
/// OIDC-compliant provider.
///
/// # Example (TOML)
///
/// ```toml
/// [auth]
/// issuer = "https://your-tenant.auth0.com/"
/// audience = "your-api-identifier"
/// ```
#[serde(default)]
pub auth: Option<OidcConfig>,
/// HS256 symmetric-key authentication (optional).
///
/// Alternative to `auth` (OIDC) for integration testing and internal
/// service-to-service scenarios. Mutually exclusive with `auth`.
///
/// Validation is fully local — no discovery endpoint, no JWKS fetch.
/// Not recommended for public-facing production.
///
/// # Example (TOML)
///
/// ```toml
/// [auth_hs256]
/// secret_env = "FRAISEQL_HS256_SECRET"
/// issuer = "my-test-suite"
/// audience = "my-api"
/// ```
#[serde(default)]
pub auth_hs256: Option<Hs256Config>,
/// TLS/SSL configuration for HTTPS and encrypted connections.
///
/// When set, enables TLS enforcement for HTTP/gRPC endpoints and
/// optionally requires mutual TLS (mTLS) for client certificates.
///
/// # Example (TOML)
///
/// ```toml
/// [tls]
/// enabled = true
/// cert_path = "/etc/fraiseql/cert.pem"
/// key_path = "/etc/fraiseql/key.pem"
/// require_client_cert = false
/// min_version = "1.2" # "1.2" or "1.3"
/// ```
#[serde(default)]
pub tls: Option<TlsServerConfig>,
/// Database TLS configuration.
///
/// Enables TLS for database connections and configures
/// per-database TLS settings (PostgreSQL, Redis, `ClickHouse`, etc.).
///
/// # Example (TOML)
///
/// ```toml
/// [database_tls]
/// postgres_ssl_mode = "require" # disable, allow, prefer, require, verify-ca, verify-full
/// redis_ssl = true # Use rediss:// protocol
/// clickhouse_https = true # Use HTTPS
/// elasticsearch_https = true # Use HTTPS
/// verify_certificates = true # Verify server certificates
/// ```
#[serde(default)]
pub database_tls: Option<DatabaseTlsConfig>,
/// Require `Content-Type: application/json` on POST requests (default: true).
///
/// CSRF protection: rejects POST requests with non-JSON Content-Type
/// (e.g. `text/plain`, `application/x-www-form-urlencoded`) with 415.
#[serde(default = "defaults::default_true")]
pub require_json_content_type: bool,
/// Maximum request body size in bytes (default: 1 MB).
///
/// Requests exceeding this limit receive 413 Payload Too Large.
/// Set to 0 to use axum's default (no limit).
#[serde(default = "defaults::default_max_request_body_bytes")]
pub max_request_body_bytes: usize,
/// Maximum number of HTTP headers per request (default: 100).
///
/// Requests with more headers than this limit receive 431 Request Header Fields Too Large.
/// Prevents header-flooding `DoS` attacks that exhaust memory.
#[serde(default = "defaults::default_max_header_count")]
pub max_header_count: usize,
/// Maximum total size of all HTTP headers in bytes (default: 32 `KiB`).
///
/// Requests whose combined header name+value bytes exceed this limit receive
/// 431 Request Header Fields Too Large. Prevents memory exhaustion from
/// oversized header values.
#[serde(default = "defaults::default_max_header_bytes")]
pub max_header_bytes: usize,
/// Per-request processing timeout in seconds (default: `None` — no timeout).
///
/// When set, each HTTP request must complete within this many seconds or
/// the server returns **408 Request Timeout**. This is a defence-in-depth
/// measure against slow or runaway database queries.
///
/// **Recommendation**: set to `60` for production deployments.
///
/// # Example (TOML)
///
/// ```toml
/// request_timeout_secs = 60
/// ```
#[serde(default)]
pub request_timeout_secs: Option<u64>,
/// Maximum byte length for a query string delivered via HTTP GET.
///
/// GET queries are URL-encoded and passed as a query parameter. Very long
/// strings are either a `DoS` attempt or a sign that the caller should use
/// POST instead. Default: `100_000` (100 `KiB`).
///
/// # Example (TOML)
///
/// ```toml
/// max_get_query_bytes = 50000
/// ```
#[serde(default = "defaults::default_max_get_query_bytes")]
pub max_get_query_bytes: usize,
/// Rate limiting configuration for GraphQL requests.
///
/// When configured, enables per-IP and per-user rate limiting with token bucket algorithm.
/// Defaults to enabled with sensible per-IP limits for security-by-default.
///
/// # Example (TOML)
///
/// ```toml
/// [rate_limiting]
/// enabled = true
/// rps_per_ip = 100 # 100 requests/second per IP
/// rps_per_user = 1000 # 1000 requests/second per authenticated user
/// burst_size = 500 # Allow bursts up to 500 requests
/// ```
#[serde(default)]
pub rate_limiting: Option<RateLimitConfig>,
/// Observer runtime configuration (optional, requires `observers` feature).
#[cfg(feature = "observers")]
#[serde(default)]
pub observers: Option<ObserverConfig>,
/// Connection pool pressure monitoring configuration.
///
/// When `enabled = true`, the server spawns a background task that monitors
/// pool metrics and emits scaling recommendations via Prometheus metrics and
/// log lines. **The pool is not resized at runtime** — act on
/// `fraiseql_pool_tuning_*` events by adjusting `max_connections` and restarting.
///
/// # Example (TOML)
///
/// ```toml
/// [pool_tuning]
/// enabled = true
/// min_pool_size = 5
/// max_pool_size = 50
/// tuning_interval_ms = 30000
/// ```
#[serde(default)]
pub pool_tuning: Option<crate::config::pool_tuning::PoolPressureMonitorConfig>,
/// Admission control configuration.
///
/// When set, enforces a maximum number of concurrent in-flight requests and
/// a maximum queue depth. Requests that exceed either limit receive
/// `503 Service Unavailable` immediately instead of stalling under load.
///
/// # Example (TOML)
///
/// ```toml
/// [admission_control]
/// max_concurrent = 500
/// max_queue_depth = 1000
/// ```
#[serde(default)]
pub admission_control: Option<AdmissionConfig>,
/// Security contact email for `/.well-known/security.txt` (RFC 9116).
///
/// When set, the server exposes a `/.well-known/security.txt` endpoint
/// with this email address as the security contact. This helps security
/// researchers report vulnerabilities responsibly.
///
/// # Example (TOML)
///
/// ```toml
/// security_contact = "security@example.com"
/// ```
#[serde(default)]
pub security_contact: Option<String>,
/// Query validation overrides (depth and complexity limits).
///
/// When present, these values take precedence over the limits baked into
/// the compiled schema, allowing operators to tune validation without
/// recompiling.
///
/// # Example (TOML)
///
/// ```toml
/// [validation]
/// max_query_depth = 15
/// max_query_complexity = 200
/// ```
#[serde(default)]
pub validation: Option<fraiseql_core::schema::ValidationConfig>,
/// Graceful shutdown drain timeout in seconds (default: 30).
///
/// After a SIGTERM or Ctrl+C signal, the server stops accepting new connections and
/// waits for in-flight requests and background runtimes (observers) to finish.
/// If the drain takes longer than this value, the process logs a warning and exits
/// immediately instead of hanging indefinitely.
///
/// Set this to match `terminationGracePeriodSeconds` in your Kubernetes pod spec
/// minus a small buffer (e.g., 25s when `terminationGracePeriodSeconds = 30`).
///
/// Override with `FRAISEQL_SHUTDOWN_TIMEOUT_SECS`.
#[serde(default = "defaults::default_shutdown_timeout_secs")]
pub shutdown_timeout_secs: u64,
}
impl Default for ServerConfig {
fn default() -> Self {
Self {
schema_path: default_schema_path(),
database_url: default_database_url(),
bind_addr: default_bind_addr(),
#[cfg(feature = "arrow")]
flight_bind_addr: defaults::default_flight_bind_addr(),
cors_enabled: true,
cors_origins: Vec::new(),
compression_enabled: false,
tracing_enabled: true,
otlp_endpoint: None,
otlp_export_timeout_secs: defaults::default_otlp_timeout_secs(),
tracing_service_name: defaults::default_service_name(),
apq_enabled: true,
cache_enabled: true,
graphql_path: default_graphql_path(),
health_path: default_health_path(),
readiness_path: default_readiness_path(),
introspection_path: default_introspection_path(),
metrics_path: default_metrics_path(),
metrics_json_path: default_metrics_json_path(),
playground_path: default_playground_path(),
playground_enabled: false, // Disabled by default for security
playground_tool: PlaygroundTool::default(),
subscription_path: default_subscription_path(),
subscriptions_enabled: true,
metrics_enabled: false, // Disabled by default for security
metrics_token: None,
admin_api_enabled: false, // Disabled by default for security
admin_token: None,
admin_readonly_token: None,
introspection_enabled: false, // Disabled by default for security
introspection_require_auth: true, // Require auth when enabled
design_api_require_auth: true, // Require auth for design endpoints
pool_min_size: default_pool_min_size(),
pool_max_size: default_pool_max_size(),
pool_timeout_secs: default_pool_timeout(),
auth: None, // No auth by default
auth_hs256: None, // No HS256 auth by default
tls: None, // TLS disabled by default
database_tls: None, /* Database TLS disabled
* by default */
require_json_content_type: true, // CSRF protection
max_request_body_bytes: default_max_request_body_bytes(), // 1 MB
max_header_count: default_max_header_count(), // 100 headers
max_header_bytes: default_max_header_bytes(), // 32 KiB
rate_limiting: None, // Rate limiting uses defaults
#[cfg(feature = "observers")]
observers: None, // Observers disabled by default
pool_tuning: None, // Pool pressure monitoring disabled by default
admission_control: None, // Admission control disabled by default
security_contact: None, // No security.txt by default
validation: None, // Use compiled schema defaults
shutdown_timeout_secs: default_shutdown_timeout_secs(),
request_timeout_secs: None,
max_get_query_bytes: defaults::default_max_get_query_bytes(),
}
}
}