crates-docs 0.9.0

High-performance Rust crate documentation query MCP server, supports Stdio/HTTP/SSE transport and OAuth authentication
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
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
//! Configuration module
//!
//! Provides application configuration management, supports loading from files, environment variables, and default values.
//!
//! # Configuration Source Priority
//!
//! 1. Environment variables (highest priority)
//! 2. Configuration file
//! 3. Default values (lowest priority)
//!
//! # Supported Configuration Formats
//!
//! - TOML configuration file
//! - Environment variables (prefix `CRATES_DOCS_`)
//!
//! # Examples
//!
//! ```rust,no_run
//! use crates_docs::config::AppConfig;
//!
//! // Load configuration from file
//! let config = AppConfig::from_file("config.toml").expect("Failed to load config");
//!
//! // Load configuration from environment variables
//! let config = AppConfig::from_env().expect("Failed to load config from env");
//!
//! // Use default configuration
//! let config = AppConfig::default();
//! ```

use crate::cache::CacheConfig;
use crate::server::auth::{AuthConfig, OAuthConfig};
use rust_mcp_sdk::schema::{Icon, IconTheme};
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;

// HTTP Client defaults

/// Default HTTP client connection pool size (10 connections)
const DEFAULT_HTTP_CLIENT_POOL_SIZE: usize = 10;
/// Default HTTP client pool idle timeout in seconds (90 seconds)
const DEFAULT_HTTP_CLIENT_POOL_IDLE_TIMEOUT_SECS: u64 = 90;
/// Default HTTP client connection timeout in seconds (10 seconds)
const DEFAULT_HTTP_CLIENT_CONNECT_TIMEOUT_SECS: u64 = 10;
/// Default HTTP client request timeout in seconds (30 seconds)
const DEFAULT_HTTP_CLIENT_TIMEOUT_SECS: u64 = 30;
/// Default HTTP client read timeout in seconds (30 seconds)
const DEFAULT_HTTP_CLIENT_READ_TIMEOUT_SECS: u64 = 30;
/// Default HTTP client max retry attempts (3 retries)
const DEFAULT_HTTP_CLIENT_MAX_RETRIES: u32 = 3;
/// Default HTTP client retry initial delay in milliseconds (100ms)
const DEFAULT_HTTP_CLIENT_RETRY_INITIAL_DELAY_MS: u64 = 100;
/// Default HTTP client retry max delay in milliseconds (10 seconds)
const DEFAULT_HTTP_CLIENT_RETRY_MAX_DELAY_MS: u64 = 10_000;

// Server defaults

/// Default server port (8080)
const DEFAULT_SERVER_PORT: u16 = 8080;
/// Default server max concurrent connections (100 connections)
const DEFAULT_SERVER_MAX_CONNECTIONS: usize = 100;
/// Default request timeout in seconds (30 seconds)
const DEFAULT_REQUEST_TIMEOUT_SECS: u64 = 30;
/// Default response timeout in seconds (60 seconds)
const DEFAULT_RESPONSE_TIMEOUT_SECS: u64 = 60;

// Cache/Rate limit defaults

/// Default cache max size in number of entries (1000 entries)
const DEFAULT_CACHE_MAX_SIZE: usize = 1000;
/// Default cache TTL in seconds (1 hour = 3600 seconds)
const DEFAULT_CACHE_DEFAULT_TTL_SECS: u64 = 3600;
/// Default rate limit per second (100 requests)
const DEFAULT_RATE_LIMIT_PER_SECOND: u32 = 100;
/// Default concurrent request limit (50 requests)
const DEFAULT_CONCURRENT_REQUEST_LIMIT: usize = 50;

// File upload defaults

/// Default max log file size in MB (100 MB)
const DEFAULT_MAX_FILE_SIZE_MB: u64 = 100;
/// Default number of log files to retain (10 files)
const DEFAULT_MAX_FILES: usize = 10;

/// Application configuration
///
/// Contains server, cache, authentication, logging, and performance configuration.
///
/// # Fields
///
/// - `server`: Server configuration
/// - `cache`: Cache configuration
/// - `auth`: Authentication configuration (OAuth and API Key)
/// - `logging`: Logging configuration
/// - `performance`: Performance configuration
///
/// # Hot Reload Support
///
/// The following configuration items support hot reload (runtime update without restart):
/// - `logging` section: All fields
/// - `auth` section: All fields (including API Key and OAuth)
/// - `cache` section: TTL-related fields (`default_ttl`, `crate_docs_ttl_secs`, `item_docs_ttl_secs`, `search_results_ttl_secs`)
/// - `performance` section: `rate_limit_per_second`, `concurrent_request_limit`, `enable_metrics`, `enable_response_compression`
///
/// The following configuration items **do not** support hot reload (require server restart):
/// - `server` section: All fields (host, port, `transport_mode`, `max_connections`, etc.)
/// - `cache` section: `cache_type`, `memory_size`, `redis_url` (cache initialization parameters)
/// - `performance` section: `http_client_*`, `cache_max_size`, `cache_default_ttl_secs`, `metrics_port`
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
pub struct AppConfig {
    /// Server configuration
    pub server: ServerConfig,

    /// Cache configuration
    pub cache: CacheConfig,

    /// Authentication configuration (OAuth and API Key)
    #[serde(default)]
    pub auth: AuthConfig,

    /// OAuth configuration (backwards compatible, prefer using auth.oauth)
    #[serde(default)]
    pub oauth: OAuthConfig,

    /// Logging configuration
    pub logging: LoggingConfig,

    /// Performance configuration
    pub performance: PerformanceConfig,
}

/// Server configuration
///
/// # Hot Reload Support
///
/// ⚠️ **Does not support hot reload** - Server configuration changes require server restart to take effect.
///
/// Reason: These configurations involve server listening socket, transport layer initialization and other core parameters,
/// runtime changes may cause connection interruption or state inconsistency.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ServerConfig {
    /// Server name
    pub name: String,

    /// Server version
    #[serde(default = "default_version")]
    pub version: String,

    /// Server description
    pub description: Option<String>,

    /// Server icons
    #[serde(default = "default_icons")]
    pub icons: Vec<Icon>,

    /// Website URL
    pub website_url: Option<String>,

    /// Host address
    pub host: String,

    /// Port
    pub port: u16,

    /// Transport mode
    pub transport_mode: String,

    /// Enable SSE support
    pub enable_sse: bool,

    /// Enable OAuth authentication
    pub enable_oauth: bool,

    /// Maximum concurrent connections
    pub max_connections: usize,

    /// Request timeout (seconds)
    pub request_timeout_secs: u64,

    /// Response timeout (seconds)
    pub response_timeout_secs: u64,

    /// Allowed hosts for CORS (e.g., `["localhost", "127.0.0.1"]`)
    pub allowed_hosts: Vec<String>,

    /// Allowed origins for CORS (e.g., `["http://localhost:*"]`)
    /// Use `"*"` only in development, specify exact origins in production
    pub allowed_origins: Vec<String>,
}

/// Default server version from Cargo.toml
fn default_version() -> String {
    crate::VERSION.to_string()
}

/// Default icons for the server
fn default_icons() -> Vec<Icon> {
    vec![
        Icon {
            src: "https://docs.rs/static/favicon-32x32.png".to_string(),
            mime_type: Some("image/png".to_string()),
            sizes: vec!["32x32".to_string()],
            theme: Some(IconTheme::Light),
        },
        Icon {
            src: "https://docs.rs/static/favicon-32x32.png".to_string(),
            mime_type: Some("image/png".to_string()),
            sizes: vec!["32x32".to_string()],
            theme: Some(IconTheme::Dark),
        },
    ]
}

/// Logging configuration
///
/// # Hot Reload Support
///
/// ✅ **Supports hot reload** - All logging configuration items can be dynamically updated at runtime.
///
/// Hot reload supported fields:
/// - `level`: Log level (trace/debug/info/warn/error)
/// - `file_path`: Log file path
/// - `enable_console`: Console logging toggle
/// - `enable_file`: File logging toggle
/// - `max_file_size_mb`: Maximum log file size
/// - `max_files`: Number of log files to retain
///
/// Note: After file logging path changes, new logs will be written to the new file, but old file handles will not be automatically closed.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct LoggingConfig {
    /// Log level
    pub level: String,

    /// Log file path
    pub file_path: Option<String>,

    /// Whether to enable console logging
    pub enable_console: bool,

    /// Whether to enable file logging
    pub enable_file: bool,

    /// Maximum log file size (MB)
    pub max_file_size_mb: u64,

    /// Number of log files to retain
    pub max_files: usize,
}

/// Performance configuration
///
/// # Hot Reload Support
///
/// ## Hot reload supported fields ✅
///
/// The following fields can be dynamically updated at runtime:
/// - `rate_limit_per_second`: Request rate limit (requests per second)
/// - `concurrent_request_limit`: Concurrent request limit
/// - `enable_metrics`: Prometheus metrics collection toggle
/// - `enable_response_compression`: Response compression toggle
///
/// ## Hot reload not supported fields ❌
///
/// The following fields require server restart to take effect:
/// - `http_client_*`: HTTP client configuration (pool size, timeouts, etc.)
/// - `cache_max_size`: Cache maximum size
/// - `cache_default_ttl_secs`: Cache default TTL
/// - `metrics_port`: Metrics server port
///
/// Reason: These configurations involve underlying connection pool, cache instance initialization parameters.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct PerformanceConfig {
    /// HTTP client connection pool size
    pub http_client_pool_size: usize,

    /// HTTP client pool idle timeout (seconds)
    pub http_client_pool_idle_timeout_secs: u64,

    /// HTTP client connection timeout (seconds)
    pub http_client_connect_timeout_secs: u64,

    /// HTTP client request timeout (seconds)
    pub http_client_timeout_secs: u64,

    /// HTTP client read timeout (seconds)
    pub http_client_read_timeout_secs: u64,

    /// HTTP client max retry attempts
    pub http_client_max_retries: u32,

    /// HTTP client retry initial delay (milliseconds)
    pub http_client_retry_initial_delay_ms: u64,

    /// HTTP client retry max delay (milliseconds)
    pub http_client_retry_max_delay_ms: u64,

    /// Maximum cache size (number of entries)
    pub cache_max_size: usize,

    /// Default cache TTL (seconds)
    pub cache_default_ttl_secs: u64,

    /// Request rate limit (requests per second)
    pub rate_limit_per_second: u32,

    /// Concurrent request limit
    pub concurrent_request_limit: usize,

    /// Enable response compression
    pub enable_response_compression: bool,

    /// Enable Prometheus metrics
    pub enable_metrics: bool,

    /// Metrics endpoint port (0 = use server port)
    pub metrics_port: u16,
}

impl Default for ServerConfig {
    fn default() -> Self {
        Self {
            name: "crates-docs".to_string(),
            version: crate::VERSION.to_string(),
            description: Some(
                "High-performance Rust crate documentation query MCP server".to_string(),
            ),
            icons: default_icons(),
            website_url: Some("https://github.com/KingingWang/crates-docs".to_string()),
            host: "127.0.0.1".to_string(),
            port: DEFAULT_SERVER_PORT,
            transport_mode: "hybrid".to_string(),
            enable_sse: true,
            enable_oauth: false,
            max_connections: DEFAULT_SERVER_MAX_CONNECTIONS,
            request_timeout_secs: DEFAULT_REQUEST_TIMEOUT_SECS,
            response_timeout_secs: DEFAULT_RESPONSE_TIMEOUT_SECS,
            // Secure defaults: only allow localhost by default
            allowed_hosts: vec!["localhost".to_string(), "127.0.0.1".to_string()],
            allowed_origins: vec!["http://localhost:*".to_string()],
        }
    }
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            level: "info".to_string(),
            file_path: Some("./logs/crates-docs.log".to_string()),
            enable_console: true,
            enable_file: false, // Default: console output only
            max_file_size_mb: DEFAULT_MAX_FILE_SIZE_MB,
            max_files: DEFAULT_MAX_FILES,
        }
    }
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            http_client_pool_size: DEFAULT_HTTP_CLIENT_POOL_SIZE,
            http_client_pool_idle_timeout_secs: DEFAULT_HTTP_CLIENT_POOL_IDLE_TIMEOUT_SECS,
            http_client_connect_timeout_secs: DEFAULT_HTTP_CLIENT_CONNECT_TIMEOUT_SECS,
            http_client_timeout_secs: DEFAULT_HTTP_CLIENT_TIMEOUT_SECS,
            http_client_read_timeout_secs: DEFAULT_HTTP_CLIENT_READ_TIMEOUT_SECS,
            http_client_max_retries: DEFAULT_HTTP_CLIENT_MAX_RETRIES,
            http_client_retry_initial_delay_ms: DEFAULT_HTTP_CLIENT_RETRY_INITIAL_DELAY_MS,
            http_client_retry_max_delay_ms: DEFAULT_HTTP_CLIENT_RETRY_MAX_DELAY_MS,
            cache_max_size: DEFAULT_CACHE_MAX_SIZE,
            cache_default_ttl_secs: DEFAULT_CACHE_DEFAULT_TTL_SECS,
            rate_limit_per_second: DEFAULT_RATE_LIMIT_PER_SECOND,
            concurrent_request_limit: DEFAULT_CONCURRENT_REQUEST_LIMIT,
            enable_response_compression: true,
            enable_metrics: true,
            metrics_port: 0,
        }
    }
}

/// Environment variable configuration for server
///
/// All fields are `Option<T>` to distinguish between "not set from environment"
/// and "explicitly set from environment".
///
/// # Semantics
///
/// - `None` - The environment variable was not set; use the config file or default value
/// - `Some(value)` - The environment variable was explicitly set to `value`
///
/// # Example
///
/// ```rust,ignore
/// // CRATES_DOCS_HOST not set
/// let config = EnvServerConfig::from_env(); // host == None, use default
///
/// // CRATES_DOCS_HOST=127.0.0.1
/// let config = EnvServerConfig::from_env(); // host == Some("127.0.0.1")
/// ```
#[derive(Debug, Clone, Default)]
pub struct EnvServerConfig {
    /// Server name
    pub name: Option<String>,
    /// Host address
    pub host: Option<String>,
    /// Port
    pub port: Option<u16>,
    /// Transport mode
    pub transport_mode: Option<String>,
}

/// Environment variable configuration for logging
///
/// All fields are `Option<T>` to distinguish between "not set from environment"
/// and "explicitly set from environment".
///
/// # Semantics
///
/// - `None` - The environment variable was not set; use the config file or default value
/// - `Some(value)` - The environment variable was explicitly set to `value`
#[derive(Debug, Clone, Default)]
pub struct EnvLoggingConfig {
    /// Log level
    pub level: Option<String>,
    /// Whether to enable console logging
    pub enable_console: Option<bool>,
    /// Whether to enable file logging
    pub enable_file: Option<bool>,
}

/// Environment variable configuration for API key (when feature enabled)
///
/// All fields are `Option<T>` to distinguish between "not set from environment"
/// and "explicitly set from environment".
///
/// # Semantics
///
/// - `None` - The environment variable was not set; use the config file or default value
/// - `Some(value)` - The environment variable was explicitly set to `value`
#[cfg(feature = "api-key")]
#[derive(Debug, Clone, Default)]
pub struct EnvApiKeyConfig {
    /// Whether API key authentication is enabled
    pub enabled: Option<bool>,
    /// List of valid API keys
    pub keys: Option<Vec<String>>,
    /// Header name for API key
    pub header_name: Option<String>,
    /// Query parameter name for API key
    pub query_param_name: Option<String>,
    /// Whether to allow API key in query parameters
    pub allow_query_param: Option<bool>,
    /// API key prefix
    pub key_prefix: Option<String>,
}

/// Environment variable configuration
///
/// Uses `Option<T>` for all fields to properly distinguish between
/// "not set" and "explicitly set to default value".
#[derive(Debug, Clone, Default)]
pub struct EnvAppConfig {
    /// Server configuration from environment
    pub server: EnvServerConfig,
    /// Logging configuration from environment
    pub logging: EnvLoggingConfig,
    /// API key configuration from environment
    #[cfg(feature = "api-key")]
    pub auth_api_key: EnvApiKeyConfig,
}

impl AppConfig {
    /// Load configuration from file
    ///
    /// # Errors
    ///
    /// Returns an error if file does not exist, cannot be read, or format is invalid
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, crate::error::Error> {
        let content = fs::read_to_string(path).map_err(|e| {
            crate::error::Error::config("file", format!("Failed to read config file: {e}"))
        })?;

        let config: Self = toml::from_str(&content).map_err(|e| {
            crate::error::Error::parse("config", None, format!("Failed to parse config file: {e}"))
        })?;

        config.validate()?;
        Ok(config)
    }

    /// Save configuration to file
    ///
    /// # Errors
    ///
    /// Returns an error if configuration cannot be serialized, directory cannot be created, or file cannot be written
    pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), crate::error::Error> {
        let content = toml::to_string_pretty(self).map_err(|e| {
            crate::error::Error::config(
                "serialization",
                format!("Failed to serialize configuration: {e}"),
            )
        })?;

        // Ensure directory exists
        if let Some(parent) = path.as_ref().parent() {
            fs::create_dir_all(parent).map_err(|e| {
                crate::error::Error::config("directory", format!("Failed to create directory: {e}"))
            })?;
        }

        fs::write(path, content).map_err(|e| {
            crate::error::Error::config("file", format!("Failed to write config file: {e}"))
        })?;

        Ok(())
    }

    /// Validate configuration
    ///
    /// # Errors
    ///
    /// Returns an error if configuration is invalid (e.g., empty hostname, invalid port, etc.)
    pub fn validate(&self) -> Result<(), crate::error::Error> {
        // Validate server configuration
        if self.server.host.is_empty() {
            return Err(crate::error::Error::config("host", "cannot be empty"));
        }

        if self.server.port == 0 {
            return Err(crate::error::Error::config("port", "cannot be 0"));
        }

        if self.server.max_connections == 0 {
            return Err(crate::error::Error::config(
                "max_connections",
                "cannot be 0",
            ));
        }

        // Validate transport mode
        let valid_modes = ["stdio", "http", "sse", "hybrid"];
        if !valid_modes.contains(&self.server.transport_mode.as_str()) {
            return Err(crate::error::Error::config(
                "transport_mode",
                format!(
                    "Invalid transport mode: {}, valid values: {:?}",
                    self.server.transport_mode, valid_modes
                ),
            ));
        }

        // Validate log level
        let valid_levels = ["trace", "debug", "info", "warn", "error"];

        if !valid_levels.contains(&self.logging.level.as_str()) {
            return Err(crate::error::Error::config(
                "log_level",
                format!(
                    "Invalid log level: {}, valid values: {:?}",
                    self.logging.level, valid_levels
                ),
            ));
        }

        // Validate performance configuration
        if self.performance.http_client_pool_size == 0 {
            return Err(crate::error::Error::config(
                "http_client_pool_size",
                "cannot be 0",
            ));
        }

        if self.performance.http_client_pool_idle_timeout_secs == 0 {
            return Err(crate::error::Error::config(
                "http_client_pool_idle_timeout_secs",
                "cannot be 0",
            ));
        }

        if self.performance.http_client_connect_timeout_secs == 0 {
            return Err(crate::error::Error::config(
                "http_client_connect_timeout_secs",
                "cannot be 0",
            ));
        }

        if self.performance.http_client_timeout_secs == 0 {
            return Err(crate::error::Error::config(
                "http_client_timeout_secs",
                "cannot be 0",
            ));
        }

        if self.performance.cache_max_size == 0 {
            return Err(crate::error::Error::config("cache_max_size", "cannot be 0"));
        }

        // Validate OAuth configuration
        if self.server.enable_oauth {
            self.oauth.validate()?;
        }

        Ok(())
    }

    /// Load configuration from environment variables
    ///
    /// Returns an `EnvAppConfig` where all fields are `Option<T>`, allowing
    /// the caller to distinguish between "not set" and "explicitly set".
    ///
    /// # Errors
    ///
    /// Returns an error if environment variable format is invalid (e.g., non-numeric port)
    pub fn from_env() -> Result<EnvAppConfig, crate::error::Error> {
        let mut config = EnvAppConfig::default();

        // Load server configuration from environment variables
        if let Ok(name) = std::env::var("CRATES_DOCS_NAME") {
            config.server.name = Some(name);
        }

        if let Ok(host) = std::env::var("CRATES_DOCS_HOST") {
            config.server.host = Some(host);
        }

        if let Ok(port) = std::env::var("CRATES_DOCS_PORT") {
            config.server.port =
                Some(port.parse().map_err(|e| {
                    crate::error::Error::config("port", format!("Invalid port: {e}"))
                })?);
        }

        if let Ok(mode) = std::env::var("CRATES_DOCS_TRANSPORT_MODE") {
            config.server.transport_mode = Some(mode);
        }

        // Load logging configuration from environment variables
        if let Ok(level) = std::env::var("CRATES_DOCS_LOG_LEVEL") {
            config.logging.level = Some(level);
        }

        if let Ok(enable_console) = std::env::var("CRATES_DOCS_ENABLE_CONSOLE") {
            config.logging.enable_console = enable_console.parse().ok();
        }

        if let Ok(enable_file) = std::env::var("CRATES_DOCS_ENABLE_FILE") {
            config.logging.enable_file = enable_file.parse().ok();
        }

        #[cfg(feature = "api-key")]
        {
            if let Ok(enabled) = std::env::var("CRATES_DOCS_API_KEY_ENABLED") {
                config.auth_api_key.enabled = enabled.parse().ok();
            }

            if let Ok(keys) = std::env::var("CRATES_DOCS_API_KEYS") {
                config.auth_api_key.keys = Some(
                    keys.split(',')
                        .map(str::trim)
                        .filter(|s| !s.is_empty())
                        .map(ToOwned::to_owned)
                        .collect(),
                );
            }

            if let Ok(header_name) = std::env::var("CRATES_DOCS_API_KEY_HEADER") {
                config.auth_api_key.header_name = Some(header_name);
            }

            if let Ok(query_param_name) = std::env::var("CRATES_DOCS_API_KEY_QUERY_PARAM_NAME") {
                config.auth_api_key.query_param_name = Some(query_param_name);
            }

            if let Ok(allow_query_param) = std::env::var("CRATES_DOCS_API_KEY_ALLOW_QUERY") {
                config.auth_api_key.allow_query_param = allow_query_param.parse().ok();
            }

            if let Ok(key_prefix) = std::env::var("CRATES_DOCS_API_KEY_PREFIX") {
                config.auth_api_key.key_prefix = Some(key_prefix);
            }
        }

        Ok(config)
    }

    /// Merge configuration (environment variables take precedence over file configuration)
    ///
    /// Uses `Option<T>` semantics from `EnvAppConfig` to determine which values
    /// were explicitly set via environment variables. This eliminates fragile
    /// hardcoded default comparisons.
    #[must_use]
    pub fn merge(file_config: Option<Self>, env_config: Option<EnvAppConfig>) -> Self {
        let mut config = Self::default();

        // First apply file configuration
        if let Some(file) = file_config {
            config = file;
        }

        // Then apply environment variable configuration (overrides file configuration)
        // Uses Option::is_some() to check if value was explicitly set
        if let Some(env) = env_config {
            // Merge server configuration - only override if explicitly set
            if let Some(name) = env.server.name {
                config.server.name = name;
            }
            if let Some(host) = env.server.host {
                config.server.host = host;
            }
            if let Some(port) = env.server.port {
                config.server.port = port;
            }
            if let Some(transport_mode) = env.server.transport_mode {
                config.server.transport_mode = transport_mode;
            }

            // Merge logging configuration - only override if explicitly set
            if let Some(level) = env.logging.level {
                config.logging.level = level;
            }
            if let Some(enable_console) = env.logging.enable_console {
                config.logging.enable_console = enable_console;
            }
            if let Some(enable_file) = env.logging.enable_file {
                config.logging.enable_file = enable_file;
            }

            #[cfg(feature = "api-key")]
            {
                if let Some(enabled) = env.auth_api_key.enabled {
                    config.auth.api_key.enabled = enabled;
                }
                if let Some(keys) = env.auth_api_key.keys {
                    config.auth.api_key.keys = keys;
                }
                if let Some(header_name) = env.auth_api_key.header_name {
                    config.auth.api_key.header_name = header_name;
                }
                if let Some(query_param_name) = env.auth_api_key.query_param_name {
                    config.auth.api_key.query_param_name = query_param_name;
                }
                if let Some(allow_query_param) = env.auth_api_key.allow_query_param {
                    config.auth.api_key.allow_query_param = allow_query_param;
                }
                if let Some(key_prefix) = env.auth_api_key.key_prefix {
                    config.auth.api_key.key_prefix = key_prefix;
                }
            }
        }

        config
    }
}