fraiseql-server 2.2.0

HTTP server for FraiseQL v2 GraphQL engine
Documentation
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
//! Runtime configuration types for the FraiseQL server.
//!
//! Structs in this module are deserialized from `fraiseql.toml` (via the
//! `loader` sub-module) or assembled from environment variables (via the
//! `env` sub-module).
//! Sub-modules contain configuration for specific subsystems such as CORS,
//! metrics, rate limiting, and TLS.

use std::{collections::HashMap, path::PathBuf};

use serde::Deserialize;

pub mod cors;
pub mod env;
pub mod error_sanitization;
pub mod loader;
pub mod metrics;
pub mod pool_tuning;
pub mod rate_limiting;
#[cfg(test)]
mod tests;
pub mod tracing;
pub mod validation;

// Re-export config types
pub use cors::CorsConfig;
pub use error_sanitization::{ErrorSanitizationConfig, ErrorSanitizer};
pub use metrics::{LatencyTargets, MetricsConfig, SloConfig};
#[allow(deprecated)] // Reason: re-export deprecated alias for backwards compatibility
pub use pool_tuning::{PoolPressureMonitorConfig, PoolTuningConfig};
pub use rate_limiting::{BackpressureConfig, RateLimitRule, RateLimitingConfig};
pub use tracing::TracingConfig;

/// Root configuration structure loaded from `fraiseql.toml`.
#[derive(Debug, Clone, Deserialize)]
pub struct RuntimeConfig {
    /// HTTP server binding, TLS, and connection-limit settings.
    pub server:   HttpServerConfig,
    /// Primary database connection and pool settings.
    pub database: DatabaseConfig,

    /// Named webhook route configurations, keyed by route name.
    #[serde(default)]
    pub webhooks: HashMap<String, WebhookRouteConfig>,

    /// Named file-upload route configurations, keyed by route name.
    #[serde(default)]
    pub files: HashMap<String, FileConfig>,

    /// Optional JWT authentication and OAuth provider configuration.
    #[serde(default)]
    pub auth: Option<AuthConfig>,

    /// Reserved: placeholder for future notification system configuration.
    #[serde(default)]
    pub notifications: Option<NotificationsConfig>,

    /// Event observer configurations, keyed by observer name.
    #[serde(default)]
    pub observers: HashMap<String, ObserverConfig>,

    /// Request interceptor chains, keyed by interceptor name.
    #[serde(default)]
    pub interceptors: HashMap<String, Vec<String>>,

    /// Optional rate-limiting rules and backpressure thresholds.
    #[serde(default)]
    pub rate_limiting: Option<RateLimitingConfig>,

    /// Optional CORS origin and header policy.
    #[serde(default)]
    pub cors: Option<CorsConfig>,

    /// Optional Prometheus metrics and SLO tracking configuration.
    #[serde(default)]
    pub metrics: Option<MetricsConfig>,

    /// Optional distributed-tracing (OTLP/Jaeger) configuration.
    #[serde(default)]
    pub tracing: Option<TracingConfig>,

    /// Optional structured-logging configuration.
    #[serde(default)]
    pub logging: Option<LoggingConfig>,

    /// Named object-storage backend configurations, keyed by storage name.
    #[serde(default)]
    pub storage: HashMap<String, StorageConfig>,

    /// Reserved: placeholder for future search-indexing configuration.
    #[serde(default)]
    pub search: Option<SearchConfig>,

    /// Reserved: placeholder for future advanced caching strategy configuration.
    #[serde(default)]
    pub cache: Option<CacheConfig>,

    /// Reserved: placeholder for future job-queue configuration.
    #[serde(default)]
    pub queues: Option<QueueConfig>,

    /// Reserved: placeholder for future real-time update configuration.
    #[serde(default)]
    pub realtime: Option<RealtimeConfig>,

    /// Reserved: placeholder for future custom-endpoint configuration.
    #[serde(default)]
    pub custom_endpoints: Option<CustomEndpointsConfig>,

    /// Graceful-shutdown timing and health-check endpoint paths.
    #[serde(default)]
    pub lifecycle: Option<LifecycleConfig>,
}

/// HTTP server binding configuration.
#[derive(Debug, Clone, Deserialize)]
pub struct HttpServerConfig {
    /// TCP port to listen on.  Default: `4000`.
    #[serde(default = "default_port")]
    pub port: u16,

    /// Network interface to bind.  Default: `"127.0.0.1"`.
    #[serde(default = "default_host")]
    pub host: String,

    /// Number of async worker threads.  `None` uses the Tokio default (number of CPU cores).
    #[serde(default)]
    pub workers: Option<usize>,

    /// Optional TLS certificate and private key paths.
    #[serde(default)]
    pub tls: Option<TlsConfig>,

    /// Optional per-request and concurrency limits.
    #[serde(default)]
    pub limits: Option<ServerLimitsConfig>,
}

const fn default_port() -> u16 {
    4000
}
fn default_host() -> String {
    "127.0.0.1".to_string()
}

/// TLS certificate and private key paths for HTTPS listeners.
#[derive(Debug, Clone, Deserialize)]
pub struct TlsConfig {
    /// Path to the PEM-encoded TLS certificate (or certificate chain).
    pub cert_file: PathBuf,
    /// Path to the PEM-encoded private key corresponding to `cert_file`.
    pub key_file:  PathBuf,
}

/// Per-request body size and concurrency limits for the HTTP server.
#[derive(Debug, Clone, Deserialize)]
pub struct ServerLimitsConfig {
    /// Maximum allowed request body size as a human-readable string (e.g. `"10MB"`).  Default:
    /// `"10MB"`.
    #[serde(default = "default_max_request_size")]
    pub max_request_size: String,

    /// Maximum time to process a single request (e.g. `"30s"`).  Default: `"30s"`.
    #[serde(default = "default_request_timeout")]
    pub request_timeout: String,

    /// Maximum number of requests being processed simultaneously.  Default: `1000`.
    #[serde(default = "default_max_concurrent")]
    pub max_concurrent_requests: usize,

    /// Maximum number of requests waiting in the accept queue.  Default: `5000`.
    #[serde(default = "default_max_queue_depth")]
    pub max_queue_depth: usize,
}

fn default_max_request_size() -> String {
    "10MB".to_string()
}
fn default_request_timeout() -> String {
    "30s".to_string()
}
const fn default_max_concurrent() -> usize {
    1000
}
const fn default_max_queue_depth() -> usize {
    5000
}

/// Primary database connection and connection-pool configuration.
#[derive(Debug, Clone, Deserialize)]
pub struct DatabaseConfig {
    /// Name of the environment variable that holds the database connection URL.
    pub url_env: String,

    /// Maximum number of connections in the pool.  Default: `10`.
    #[serde(default = "default_pool_size")]
    pub pool_size: u32,

    /// How long to wait for an available connection before returning an error (e.g. `"5s"`).
    #[serde(default)]
    pub pool_timeout: Option<String>,

    /// Per-query statement timeout sent to the database (e.g. `"30s"`).
    #[serde(default)]
    pub statement_timeout: Option<String>,

    /// Optional read-replica pools used for load balancing SELECT queries.
    #[serde(default)]
    pub replicas: Vec<ReplicaConfig>,

    /// How often to ping the database to verify liveness (e.g. `"60s"`).
    #[serde(default)]
    pub health_check_interval: Option<String>,
}

const fn default_pool_size() -> u32 {
    10
}

/// Connection configuration for a single read replica.
#[derive(Debug, Clone, Deserialize)]
pub struct ReplicaConfig {
    /// Name of the environment variable that holds this replica's connection URL.
    pub url_env: String,

    /// Relative weight for load-balancing SELECT queries across replicas.  Default: `1`.
    #[serde(default = "default_weight")]
    pub weight: u32,
}

const fn default_weight() -> u32 {
    1
}

/// Lifecycle configuration for graceful shutdown
#[derive(Debug, Clone, Deserialize)]
pub struct LifecycleConfig {
    /// Time to wait for in-flight requests to complete
    #[serde(default = "default_shutdown_timeout")]
    pub shutdown_timeout: String,

    /// Time to wait before starting shutdown (for load balancer deregistration)
    #[serde(default = "default_shutdown_delay")]
    pub shutdown_delay: String,

    /// Health check endpoint path
    #[serde(default = "default_health_path")]
    pub health_path: String,

    /// Readiness check endpoint path
    #[serde(default = "default_ready_path")]
    pub ready_path: String,
}

impl Default for LifecycleConfig {
    fn default() -> Self {
        Self {
            shutdown_timeout: default_shutdown_timeout(),
            shutdown_delay:   default_shutdown_delay(),
            health_path:      default_health_path(),
            ready_path:       default_ready_path(),
        }
    }
}

fn default_shutdown_timeout() -> String {
    "30s".to_string()
}
fn default_shutdown_delay() -> String {
    "5s".to_string()
}
fn default_health_path() -> String {
    "/health".to_string()
}
fn default_ready_path() -> String {
    "/ready".to_string()
}

/// Configuration for a single incoming webhook route.
#[derive(Debug, Clone, Deserialize)]
pub struct WebhookRouteConfig {
    /// Name of the environment variable that holds the webhook signing secret.
    pub secret_env: String,
    /// Webhook provider identifier (e.g. `"github"`, `"stripe"`).
    pub provider:   String,
    /// URL path override; if absent, the route name is used as the path segment.
    #[serde(default)]
    pub path:       Option<String>,
}

/// Configuration for a file-upload route.
#[derive(Debug, Clone, Deserialize)]
pub struct FileConfig {
    /// Named storage backend (must match a key in `storage`).
    pub storage:  String,
    /// Maximum upload size as a human-readable string (e.g. `"50MB"`).
    pub max_size: String,
    /// URL path prefix for upload and download endpoints.
    #[serde(default)]
    pub path:     Option<String>,
}

/// JWT authentication and OAuth provider configuration.
#[derive(Debug, Clone, Deserialize)]
pub struct AuthConfig {
    /// JWT signing secret configuration.
    pub jwt:               JwtConfig,
    /// Named OAuth2/OIDC provider configurations.
    #[serde(default)]
    pub providers:         HashMap<String, OAuthProviderConfig>,
    /// Base URL for OAuth callback endpoints (e.g. `"https://api.example.com"`).
    #[serde(default)]
    pub callback_base_url: Option<String>,
}

/// JWT signing-secret configuration.
#[derive(Debug, Clone, Deserialize)]
pub struct JwtConfig {
    /// Name of the environment variable that holds the JWT signing secret.
    pub secret_env: String,
}

/// Configuration for a single OAuth2/OIDC provider.
#[derive(Debug, Clone, Deserialize)]
pub struct OAuthProviderConfig {
    /// Well-known provider type identifier (e.g. `"auth0"`, `"github"`, `"google"`).
    pub provider_type:     String,
    /// Name of the environment variable that holds the OAuth client ID.
    pub client_id_env:     String,
    /// Name of the environment variable that holds the OAuth client secret.
    pub client_secret_env: String,
    /// OIDC issuer URL (required for providers that support OIDC discovery).
    #[serde(default)]
    pub issuer_url:        Option<String>,
}

/// Reserved: placeholder for future notification system configuration.
#[derive(Debug, Clone, Deserialize)]
pub struct NotificationsConfig {}

/// Configuration for a single event observer (entity-event → action).
#[derive(Debug, Clone, Deserialize)]
pub struct ObserverConfig {
    /// GraphQL entity type name to watch (e.g. `"User"`).
    pub entity:  String,
    /// List of mutation operation names that trigger this observer.
    pub events:  Vec<String>,
    /// Ordered list of actions to execute when an observed event fires.
    pub actions: Vec<ActionConfig>,
}

/// A single action within an observer pipeline.
#[derive(Debug, Clone, Deserialize)]
pub struct ActionConfig {
    /// Action type identifier (e.g. `"webhook"`, `"email"`, `"queue"`).
    #[serde(rename = "type")]
    pub action_type: String,
    /// Optional Jinja2-style template used to render the action payload.
    #[serde(default)]
    pub template:    Option<String>,
}

// These types are now defined in their own modules and re-exported above

/// Reserved: placeholder for future structured-logging configuration.
#[derive(Debug, Clone, Deserialize)]
pub struct LoggingConfig {}

/// Configuration for a single object-storage backend.
#[derive(Debug, Clone, Deserialize)]
pub struct StorageConfig {
    /// Storage backend identifier (e.g. `"s3"`, `"gcs"`, `"local"`).
    pub backend:      String,
    /// Bucket or container name (required for cloud backends).
    #[serde(default)]
    pub bucket:       Option<String>,
    /// Local filesystem path (used by the `"local"` backend).
    #[serde(default)]
    pub path:         Option<String>,
    /// Cloud region (e.g. `"eu-west-1"` for AWS, `"fr-par"` for Scaleway).
    #[serde(default)]
    pub region:       Option<String>,
    /// Custom endpoint URL (for S3-compatible providers or local development).
    #[serde(default)]
    pub endpoint:     Option<String>,
    /// GCP project ID (used by the `"gcs"` backend).
    #[serde(default)]
    pub project_id:   Option<String>,
    /// Azure storage account name (used by the `"azure"` backend).
    #[serde(default)]
    pub account_name: Option<String>,
}

/// Reserved: placeholder for future full-text search indexing configuration.
#[derive(Debug, Clone, Deserialize)]
pub struct SearchConfig {}

/// Reserved: placeholder for future advanced query-result caching configuration.
#[derive(Debug, Clone, Deserialize)]
pub struct CacheConfig {}

/// Reserved: placeholder for future background job-queue configuration.
#[derive(Debug, Clone, Deserialize)]
pub struct QueueConfig {}

/// Reserved: placeholder for future real-time subscription update configuration.
#[derive(Debug, Clone, Deserialize)]
pub struct RealtimeConfig {}

/// Reserved: placeholder for future custom HTTP endpoint configuration.
#[derive(Debug, Clone, Deserialize)]
pub struct CustomEndpointsConfig {}