mockforge_http/handlers/
deceptive_canary.rs

1//! Deceptive Canary API handlers
2
3use crate::middleware::DeceptiveCanaryState;
4use axum::extract::State;
5use axum::response::Json;
6use mockforge_core::deceptive_canary::DeceptiveCanaryConfig;
7use serde::{Deserialize, Serialize};
8use serde_json::{json, Value};
9
10/// Request to update canary configuration
11#[derive(Debug, Deserialize)]
12pub struct UpdateCanaryConfigRequest {
13    /// New canary configuration
14    pub config: DeceptiveCanaryConfig,
15}
16
17/// Response for canary statistics
18#[derive(Debug, Serialize)]
19pub struct CanaryStatsResponse {
20    /// Success flag
21    pub success: bool,
22    /// Current statistics
23    pub stats: Value,
24    /// Canary routing percentage
25    pub canary_percentage: f64,
26}
27
28/// Get current canary configuration
29///
30/// GET /api/v1/deceptive-canary/config
31pub async fn get_canary_config(State(state): State<DeceptiveCanaryState>) -> Json<Value> {
32    let config = state.router.config();
33    Json(json!({
34        "success": true,
35        "config": config,
36    }))
37}
38
39/// Update canary configuration
40///
41/// POST /api/v1/deceptive-canary/config
42pub async fn update_canary_config(
43    State(_state): State<DeceptiveCanaryState>,
44    Json(request): Json<UpdateCanaryConfigRequest>,
45) -> Json<Value> {
46    // Update router configuration
47    // Note: This requires mutable access, which would need Arc<RwLock<DeceptiveCanaryRouter>>
48    // For now, we'll return the config that should be applied
49    Json(json!({
50        "success": true,
51        "message": "Configuration update requires router state management",
52        "config": request.config,
53    }))
54}
55
56/// Get canary routing statistics
57///
58/// GET /api/v1/deceptive-canary/stats
59pub async fn get_canary_stats(State(state): State<DeceptiveCanaryState>) -> Json<Value> {
60    let stats = state.router.stats();
61    let canary_percentage = stats.map(|s| s.canary_percentage()).unwrap_or(0.0);
62
63    Json(json!({
64        "success": true,
65        "stats": stats,
66        "canary_percentage": canary_percentage,
67    }))
68}