grpc_graphql_gateway 1.2.4

A Rust implementation of gRPC-GraphQL gateway - generates GraphQL execution code from gRPC services
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
//! Health check endpoints for Kubernetes/container orchestration.
//!
//! This module provides health check handlers for liveness and readiness probes,
//! essential for production deployments with Kubernetes, Docker Swarm, or similar.
//!
//! # Endpoints
//!
//! - `/health` - Liveness probe: Returns 200 if the server is running
//! - `/ready` - Readiness probe: Returns 200 if all gRPC backends are reachable
//!
//! # Example
//!
//! ```rust,no_run
//! use grpc_graphql_gateway::Gateway;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let gateway = Gateway::builder()
//!     .with_descriptor_set_bytes(b"...")
//!     .enable_health_checks()  // Adds /health and /ready endpoints
//!     .build()?;
//! # Ok(())
//! # }
//! ```

use axum::{
    extract::State,
    http::StatusCode,
    response::{IntoResponse, Response},
    Json,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

use crate::grpc_client::GrpcClientPool;

/// Health check response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthResponse {
    /// Overall health status
    pub status: HealthStatus,
    /// Optional message with details
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    /// Individual component checks
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub checks: Vec<ComponentHealth>,
}

/// Health status enum
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum HealthStatus {
    /// Service is healthy
    Healthy,
    /// Service is degraded but operational
    Degraded,
    /// Service is unhealthy
    Unhealthy,
}

/// Individual component health check result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComponentHealth {
    /// Component name
    pub name: String,
    /// Component health status
    pub status: HealthStatus,
    /// Optional message
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
}

impl HealthResponse {
    /// Create a healthy response
    pub fn healthy() -> Self {
        Self {
            status: HealthStatus::Healthy,
            message: None,
            checks: Vec::new(),
        }
    }

    /// Create a healthy response with a message
    pub fn healthy_with_message(message: impl Into<String>) -> Self {
        Self {
            status: HealthStatus::Healthy,
            message: Some(message.into()),
            checks: Vec::new(),
        }
    }

    /// Create an unhealthy response
    pub fn unhealthy(message: impl Into<String>) -> Self {
        Self {
            status: HealthStatus::Unhealthy,
            message: Some(message.into()),
            checks: Vec::new(),
        }
    }

    /// Create a degraded response
    pub fn degraded(message: impl Into<String>) -> Self {
        Self {
            status: HealthStatus::Degraded,
            message: Some(message.into()),
            checks: Vec::new(),
        }
    }

    /// Add a component check
    pub fn with_check(mut self, check: ComponentHealth) -> Self {
        // Update overall status based on component
        match (&self.status, check.status) {
            (HealthStatus::Healthy, HealthStatus::Unhealthy) => {
                self.status = HealthStatus::Unhealthy;
            }
            (HealthStatus::Healthy, HealthStatus::Degraded) => {
                self.status = HealthStatus::Degraded;
            }
            (HealthStatus::Degraded, HealthStatus::Unhealthy) => {
                self.status = HealthStatus::Unhealthy;
            }
            _ => {}
        }
        self.checks.push(check);
        self
    }
}

impl IntoResponse for HealthResponse {
    fn into_response(self) -> Response {
        let status_code = match self.status {
            HealthStatus::Healthy => StatusCode::OK,
            HealthStatus::Degraded => StatusCode::OK, // Still OK but with warning
            HealthStatus::Unhealthy => StatusCode::SERVICE_UNAVAILABLE,
        };
        (status_code, Json(self)).into_response()
    }
}

/// Shared state for health check handlers
#[derive(Clone)]
pub struct HealthState {
    /// Reference to the gRPC client pool for readiness checks
    pub client_pool: Arc<GrpcClientPool>,
    /// Optional custom health check function
    pub custom_check: Option<Arc<dyn Fn() -> HealthResponse + Send + Sync>>,
}

impl HealthState {
    /// Create a new health state
    pub fn new(client_pool: GrpcClientPool) -> Self {
        Self {
            client_pool: Arc::new(client_pool),
            custom_check: None,
        }
    }

    /// Add a custom health check
    pub fn with_custom_check<F>(mut self, check: F) -> Self
    where
        F: Fn() -> HealthResponse + Send + Sync + 'static,
    {
        self.custom_check = Some(Arc::new(check));
        self
    }
}

/// Liveness probe handler - `/health`
///
/// Returns 200 OK if the server process is running.
/// This is a simple check that doesn't verify backend connectivity.
pub async fn health_handler() -> HealthResponse {
    HealthResponse::healthy_with_message("Gateway is running")
}

/// Readiness probe handler - `/ready`
///
/// Returns 200 OK if the server is ready to accept traffic.
/// This checks that gRPC backends are configured (but doesn't actively ping them
/// to avoid adding latency to the probe).
pub async fn readiness_handler(State(state): State<Arc<HealthState>>) -> HealthResponse {
    let mut response = HealthResponse::healthy();

    // Check gRPC client pool
    let client_names = state.client_pool.names();
    if client_names.is_empty() {
        response = response.with_check(ComponentHealth {
            name: "grpc_clients".to_string(),
            status: HealthStatus::Degraded,
            message: Some("No gRPC clients configured".to_string()),
        });
    } else {
        response = response.with_check(ComponentHealth {
            name: "grpc_clients".to_string(),
            status: HealthStatus::Healthy,
            message: Some(format!("{} clients configured", client_names.len())),
        });
    }

    // Run custom health check if provided
    if let Some(custom_check) = &state.custom_check {
        let custom_result = custom_check();
        for check in custom_result.checks {
            response = response.with_check(check);
        }
    }

    response
}

/// Deep health check handler - `/ready/deep`
///
/// Performs actual connectivity checks to gRPC backends.
/// This is more expensive but provides accurate health status.
pub async fn deep_readiness_handler(State(state): State<Arc<HealthState>>) -> HealthResponse {
    let mut response = HealthResponse::healthy();

    let client_names = state.client_pool.names();

    if client_names.is_empty() {
        return HealthResponse::degraded("No gRPC clients configured");
    }

    for name in client_names {
        // Check if client exists and is configured
        // Note: We can't easily ping gRPC without making an actual RPC call,
        // so we just verify the client is registered
        if state.client_pool.get(&name).is_some() {
            response = response.with_check(ComponentHealth {
                name: format!("grpc:{}", name),
                status: HealthStatus::Healthy,
                message: Some("Client configured".to_string()),
            });
        } else {
            response = response.with_check(ComponentHealth {
                name: format!("grpc:{}", name),
                status: HealthStatus::Unhealthy,
                message: Some("Client not found".to_string()),
            });
        }
    }

    response
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_healthy_response() {
        let response = HealthResponse::healthy();
        assert_eq!(response.status, HealthStatus::Healthy);
        assert_eq!(response.message, None);
        assert!(response.checks.is_empty());
    }

    #[test]
    fn test_healthy_with_message() {
        let response = HealthResponse::healthy_with_message("All systems operational");
        assert_eq!(response.status, HealthStatus::Healthy);
        assert_eq!(
            response.message,
            Some("All systems operational".to_string())
        );
        assert!(response.checks.is_empty());
    }

    #[test]
    fn test_unhealthy_response() {
        let response = HealthResponse::unhealthy("Something is wrong");
        assert_eq!(response.status, HealthStatus::Unhealthy);
        assert_eq!(response.message, Some("Something is wrong".to_string()));
    }

    #[test]
    fn test_degraded_response() {
        let response = HealthResponse::degraded("Performance degraded");
        assert_eq!(response.status, HealthStatus::Degraded);
        assert_eq!(response.message, Some("Performance degraded".to_string()));
    }

    #[test]
    fn test_component_health_propagation_unhealthy() {
        let response = HealthResponse::healthy()
            .with_check(ComponentHealth {
                name: "db".to_string(),
                status: HealthStatus::Healthy,
                message: None,
            })
            .with_check(ComponentHealth {
                name: "cache".to_string(),
                status: HealthStatus::Unhealthy,
                message: Some("Redis connection failed".to_string()),
            });

        // Overall should be unhealthy because one component is unhealthy
        assert_eq!(response.status, HealthStatus::Unhealthy);
        assert_eq!(response.checks.len(), 2);
    }

    #[test]
    fn test_degraded_propagation() {
        let response = HealthResponse::healthy()
            .with_check(ComponentHealth {
                name: "primary".to_string(),
                status: HealthStatus::Healthy,
                message: None,
            })
            .with_check(ComponentHealth {
                name: "secondary".to_string(),
                status: HealthStatus::Degraded,
                message: Some("Replica lag detected".to_string()),
            });

        // Overall should be degraded
        assert_eq!(response.status, HealthStatus::Degraded);
    }

    #[test]
    fn test_degraded_to_unhealthy_propagation() {
        let response = HealthResponse::degraded("Already degraded").with_check(ComponentHealth {
            name: "service".to_string(),
            status: HealthStatus::Unhealthy,
            message: Some("Critical failure".to_string()),
        });

        // Degraded + Unhealthy = Unhealthy
        assert_eq!(response.status, HealthStatus::Unhealthy);
    }

    #[test]
    fn test_multiple_healthy_checks() {
        let response = HealthResponse::healthy()
            .with_check(ComponentHealth {
                name: "db".to_string(),
                status: HealthStatus::Healthy,
                message: None,
            })
            .with_check(ComponentHealth {
                name: "cache".to_string(),
                status: HealthStatus::Healthy,
                message: None,
            })
            .with_check(ComponentHealth {
                name: "queue".to_string(),
                status: HealthStatus::Healthy,
                message: None,
            });

        // All healthy = overall healthy
        assert_eq!(response.status, HealthStatus::Healthy);
        assert_eq!(response.checks.len(), 3);
    }

    #[test]
    fn test_health_status_equality() {
        assert_eq!(HealthStatus::Healthy, HealthStatus::Healthy);
        assert_eq!(HealthStatus::Degraded, HealthStatus::Degraded);
        assert_eq!(HealthStatus::Unhealthy, HealthStatus::Unhealthy);

        assert_ne!(HealthStatus::Healthy, HealthStatus::Degraded);
        assert_ne!(HealthStatus::Healthy, HealthStatus::Unhealthy);
        assert_ne!(HealthStatus::Degraded, HealthStatus::Unhealthy);
    }

    #[test]
    fn test_health_status_serialization() {
        let status = HealthStatus::Healthy;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, "\"healthy\"");

        let status = HealthStatus::Degraded;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, "\"degraded\"");

        let status = HealthStatus::Unhealthy;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, "\"unhealthy\"");
    }

    #[test]
    fn test_health_status_deserialization() {
        let status: HealthStatus = serde_json::from_str("\"healthy\"").unwrap();
        assert_eq!(status, HealthStatus::Healthy);

        let status: HealthStatus = serde_json::from_str("\"degraded\"").unwrap();
        assert_eq!(status, HealthStatus::Degraded);

        let status: HealthStatus = serde_json::from_str("\"unhealthy\"").unwrap();
        assert_eq!(status, HealthStatus::Unhealthy);
    }

    #[test]
    fn test_health_response_serialization() {
        let response = HealthResponse::healthy_with_message("OK");
        let json = serde_json::to_string(&response).unwrap();

        assert!(json.contains("healthy"));
        assert!(json.contains("OK"));
    }

    #[test]
    fn test_health_response_serialization_skip_empty() {
        let response = HealthResponse::healthy();
        let json = serde_json::to_string(&response).unwrap();

        // Empty checks and None message should be skipped
        assert!(!json.contains("message"));
        assert!(!json.contains("checks"));
    }

    #[test]
    fn test_health_response_deserialization() {
        let json = r#"{"status":"healthy","message":"All good"}"#;
        let response: HealthResponse = serde_json::from_str(json).unwrap();

        assert_eq!(response.status, HealthStatus::Healthy);
        assert_eq!(response.message, Some("All good".to_string()));
    }

    #[test]
    fn test_component_health_serialization() {
        let component = ComponentHealth {
            name: "database".to_string(),
            status: HealthStatus::Healthy,
            message: Some("Connected".to_string()),
        };

        let json = serde_json::to_string(&component).unwrap();
        assert!(json.contains("database"));
        assert!(json.contains("healthy"));
        assert!(json.contains("Connected"));
    }

    #[test]
    fn test_component_health_without_message() {
        let component = ComponentHealth {
            name: "cache".to_string(),
            status: HealthStatus::Healthy,
            message: None,
        };

        let json = serde_json::to_string(&component).unwrap();
        assert!(!json.contains("message"));
    }

    #[test]
    fn test_into_response_healthy() {
        let response = HealthResponse::healthy();
        let http_response = response.into_response();

        assert_eq!(http_response.status(), StatusCode::OK);
    }

    #[test]
    fn test_into_response_degraded() {
        let response = HealthResponse::degraded("Slow performance");
        let http_response = response.into_response();

        // Degraded still returns OK (200) but with warning
        assert_eq!(http_response.status(), StatusCode::OK);
    }

    #[test]
    fn test_into_response_unhealthy() {
        let response = HealthResponse::unhealthy("Service down");
        let http_response = response.into_response();

        assert_eq!(http_response.status(), StatusCode::SERVICE_UNAVAILABLE);
    }

    #[test]
    fn test_health_state_new() {
        let pool = GrpcClientPool::new();
        let state = HealthState::new(pool);

        assert!(state.custom_check.is_none());
        assert_eq!(state.client_pool.names().len(), 0);
    }

    #[test]
    fn test_health_state_with_custom_check() {
        let pool = GrpcClientPool::new();
        let state = HealthState::new(pool)
            .with_custom_check(|| HealthResponse::healthy_with_message("Custom check passed"));

        assert!(state.custom_check.is_some());

        // Execute custom check
        if let Some(check) = &state.custom_check {
            let result = check();
            assert_eq!(result.status, HealthStatus::Healthy);
            assert_eq!(result.message, Some("Custom check passed".to_string()));
        }
    }

    #[test]
    fn test_health_state_clone() {
        let pool = GrpcClientPool::new();
        let state = HealthState::new(pool);
        let cloned = state.clone();

        assert_eq!(
            Arc::strong_count(&state.client_pool),
            Arc::strong_count(&cloned.client_pool)
        );
    }

    #[tokio::test]
    async fn test_health_handler() {
        let response = health_handler().await;
        assert_eq!(response.status, HealthStatus::Healthy);
        assert!(response.message.is_some());
    }

    #[tokio::test]
    async fn test_readiness_handler_no_clients() {
        let pool = GrpcClientPool::new();
        let state = Arc::new(HealthState::new(pool));

        let response = readiness_handler(State(state)).await;

        // Should be degraded when no clients configured
        assert_eq!(response.status, HealthStatus::Degraded);
        assert_eq!(response.checks.len(), 1);
        assert_eq!(response.checks[0].name, "grpc_clients");
    }

    #[tokio::test]
    async fn test_readiness_handler_with_clients() {
        use crate::grpc_client::GrpcClient;

        let pool = GrpcClientPool::new();

        // Create a mock client (using a dummy endpoint - test won't actually connect)
        let client = GrpcClient::connect_lazy("http://localhost:50051", true).unwrap();
        pool.add("test-service", client);

        let state = Arc::new(HealthState::new(pool));

        let response = readiness_handler(State(state)).await;

        // Should be healthy with clients configured
        assert_eq!(response.status, HealthStatus::Healthy);
        assert_eq!(response.checks.len(), 1);
        assert_eq!(response.checks[0].name, "grpc_clients");
        assert_eq!(response.checks[0].status, HealthStatus::Healthy);
    }

    #[tokio::test]
    async fn test_readiness_handler_with_custom_check() {
        let pool = GrpcClientPool::new();
        let state = Arc::new(HealthState::new(pool).with_custom_check(|| {
            HealthResponse::healthy().with_check(ComponentHealth {
                name: "custom".to_string(),
                status: HealthStatus::Healthy,
                message: Some("Custom check OK".to_string()),
            })
        }));

        let response = readiness_handler(State(state)).await;

        // Should have both grpc_clients and custom checks
        assert_eq!(response.checks.len(), 2);
        assert!(response.checks.iter().any(|c| c.name == "custom"));
    }

    #[tokio::test]
    async fn test_deep_readiness_handler_no_clients() {
        let pool = GrpcClientPool::new();
        let state = Arc::new(HealthState::new(pool));

        let response = deep_readiness_handler(State(state)).await;

        assert_eq!(response.status, HealthStatus::Degraded);
        assert_eq!(
            response.message,
            Some("No gRPC clients configured".to_string())
        );
    }

    #[tokio::test]
    async fn test_deep_readiness_handler_with_clients() {
        use crate::grpc_client::GrpcClient;

        let pool = GrpcClientPool::new();
        let client = GrpcClient::connect_lazy("http://localhost:50051", true).unwrap();
        pool.add("test-service", client);

        let state = Arc::new(HealthState::new(pool));

        let response = deep_readiness_handler(State(state)).await;

        // Should check each client
        assert_eq!(response.checks.len(), 1);
        assert_eq!(response.checks[0].name, "grpc:test-service");
        assert_eq!(response.checks[0].status, HealthStatus::Healthy);
    }

    #[test]
    fn test_response_with_multiple_component_states() {
        let response = HealthResponse::healthy()
            .with_check(ComponentHealth {
                name: "comp1".to_string(),
                status: HealthStatus::Healthy,
                message: None,
            })
            .with_check(ComponentHealth {
                name: "comp2".to_string(),
                status: HealthStatus::Degraded,
                message: Some("Warning".to_string()),
            })
            .with_check(ComponentHealth {
                name: "comp3".to_string(),
                status: HealthStatus::Degraded,
                message: Some("Another warning".to_string()),
            });

        // Multiple degraded components should keep status as degraded
        assert_eq!(response.status, HealthStatus::Degraded);
        assert_eq!(response.checks.len(), 3);
    }

    #[test]
    fn test_health_response_clone() {
        let original = HealthResponse::healthy_with_message("Test");
        let cloned = original.clone();

        assert_eq!(cloned.status, original.status);
        assert_eq!(cloned.message, original.message);
    }

    #[test]
    fn test_health_response_debug() {
        let response = HealthResponse::healthy();
        let debug_str = format!("{:?}", response);

        assert!(debug_str.contains("HealthResponse"));
        assert!(debug_str.contains("Healthy"));
    }

    #[test]
    fn test_component_health_clone() {
        let original = ComponentHealth {
            name: "test".to_string(),
            status: HealthStatus::Healthy,
            message: Some("OK".to_string()),
        };
        let cloned = original.clone();

        assert_eq!(cloned.name, original.name);
        assert_eq!(cloned.status, original.status);
        assert_eq!(cloned.message, original.message);
    }
}