a3s-gateway 0.2.1

A3S Gateway - AI-native API gateway with reverse proxy, routing, and agent orchestration
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
//! Dashboard API — serves gateway status, metrics, and management endpoints
//!
//! Extracted from `gateway.rs` to keep the orchestrator focused on lifecycle.

use crate::gateway::Gateway;
use serde::Serialize;

// ---------------------------------------------------------------------------
// Response types
// ---------------------------------------------------------------------------

/// Route information for the management API
#[derive(Debug, Clone, Serialize)]
pub struct RouteInfo {
    pub name: String,
    pub rule: String,
    pub service: String,
    pub entrypoints: Vec<String>,
    pub middlewares: Vec<String>,
    pub priority: i32,
}

/// Service information with live backend health
#[derive(Debug, Clone, Serialize)]
pub struct ServiceInfo {
    pub name: String,
    pub strategy: String,
    pub backends_total: usize,
    pub backends_healthy: usize,
    pub backends: Vec<BackendInfo>,
}

/// Backend health snapshot
#[derive(Debug, Clone, Serialize)]
pub struct BackendInfo {
    pub url: String,
    pub weight: u32,
    pub healthy: bool,
    pub active_connections: usize,
}

/// Backend detail with owning service name (for flat /backends listing)
#[derive(Debug, Clone, Serialize)]
pub struct BackendDetail {
    pub service: String,
    pub url: String,
    pub weight: u32,
    pub healthy: bool,
    pub active_connections: usize,
}

/// Gateway version information
#[derive(Debug, Clone, Serialize)]
pub struct VersionInfo {
    pub name: &'static str,
    pub version: &'static str,
}

impl VersionInfo {
    pub(crate) fn current() -> Self {
        Self {
            name: env!("CARGO_PKG_NAME"),
            version: env!("CARGO_PKG_VERSION"),
        }
    }
}

// ---------------------------------------------------------------------------
// Dashboard API handler
// ---------------------------------------------------------------------------

/// Dashboard API — serves gateway status and metrics
pub struct DashboardApi {
    /// Path prefix for the dashboard
    pub path_prefix: String,
}

impl DashboardApi {
    /// Create a new dashboard API
    pub fn new(path_prefix: impl Into<String>) -> Self {
        Self {
            path_prefix: path_prefix.into(),
        }
    }

    /// Check if a request path matches the dashboard
    pub fn matches(&self, path: &str) -> bool {
        path.starts_with(&self.path_prefix)
    }

    /// Handle a dashboard API request
    pub fn handle(&self, path: &str, gateway: &Gateway) -> Option<DashboardResponse> {
        let sub_path = path.strip_prefix(&self.path_prefix)?;

        match sub_path {
            "/health" | "/health/" => {
                let health = gateway.health();
                let body = serde_json::to_string_pretty(&health).unwrap_or_default();
                Some(DashboardResponse::json(200, body))
            }
            "/metrics" | "/metrics/" => {
                let _snapshot = gateway.metrics().snapshot();
                let body = gateway.metrics().render_prometheus();
                Some(DashboardResponse {
                    status: 200,
                    content_type: "text/plain; version=0.0.4".to_string(),
                    body,
                })
            }
            "/config" | "/config/" => {
                let config = gateway.config();
                let body = serde_json::to_string_pretty(&config).unwrap_or_default();
                Some(DashboardResponse::json(200, body))
            }
            "/routes" | "/routes/" => {
                let routes = gateway.routes_snapshot();
                let body = serde_json::to_string_pretty(&routes).unwrap_or_default();
                Some(DashboardResponse::json(200, body))
            }
            "/services" | "/services/" => {
                let services = gateway.services_snapshot();
                let body = serde_json::to_string_pretty(&services).unwrap_or_default();
                Some(DashboardResponse::json(200, body))
            }
            "/backends" | "/backends/" => {
                let backends = gateway.backends_snapshot();
                let body = serde_json::to_string_pretty(&backends).unwrap_or_default();
                Some(DashboardResponse::json(200, body))
            }
            "/version" | "/version/" => {
                let version = VersionInfo::current();
                let body = serde_json::to_string_pretty(&version).unwrap_or_default();
                Some(DashboardResponse::json(200, body))
            }
            s if s.starts_with("/routes/") => {
                let name = &s["/routes/".len()..].trim_end_matches('/');
                let routes = gateway.routes_snapshot();
                match routes.into_iter().find(|r| r.name == *name) {
                    Some(route) => {
                        let body = serde_json::to_string_pretty(&route).unwrap_or_default();
                        Some(DashboardResponse::json(200, body))
                    }
                    None => Some(DashboardResponse::not_found("Route not found")),
                }
            }
            s if s.starts_with("/services/") => {
                let name = &s["/services/".len()..].trim_end_matches('/');
                let services = gateway.services_snapshot();
                match services.into_iter().find(|svc| svc.name == *name) {
                    Some(svc) => {
                        let body = serde_json::to_string_pretty(&svc).unwrap_or_default();
                        Some(DashboardResponse::json(200, body))
                    }
                    None => Some(DashboardResponse::not_found("Service not found")),
                }
            }
            _ => Some(DashboardResponse::not_found("Not found")),
        }
    }
}

/// Response from the dashboard API
#[derive(Debug, Clone)]
pub struct DashboardResponse {
    /// HTTP status code
    pub status: u16,
    /// Content-Type header
    pub content_type: String,
    /// Response body
    pub body: String,
}

impl DashboardResponse {
    pub(crate) fn json(status: u16, body: String) -> Self {
        Self {
            status,
            content_type: "application/json".to_string(),
            body,
        }
    }

    pub(crate) fn not_found(message: &str) -> Self {
        Self::json(404, format!(r#"{{"error":"{}"}}"#, message))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{
        GatewayConfig, LoadBalancerConfig, RouterConfig, ServerConfig, ServiceConfig, Strategy,
    };
    use crate::router::RouterTable;
    use crate::service::ServiceRegistry;
    use std::sync::Arc;

    fn minimal_config() -> GatewayConfig {
        let mut config = GatewayConfig::default();
        config.routers.clear();
        config.services.clear();
        config.middlewares.clear();
        config
    }

    fn full_config() -> GatewayConfig {
        let mut config = GatewayConfig::default();
        config.routers.insert(
            "api".to_string(),
            RouterConfig {
                rule: "PathPrefix(`/api`)".to_string(),
                service: "backend".to_string(),
                entrypoints: vec!["web".to_string()],
                middlewares: vec![],
                priority: 0,
            },
        );
        config.services.insert(
            "backend".to_string(),
            ServiceConfig {
                load_balancer: LoadBalancerConfig {
                    strategy: Strategy::RoundRobin,
                    servers: vec![ServerConfig {
                        url: "http://127.0.0.1:8001".to_string(),
                        weight: 1,
                    }],
                    health_check: None,
                    sticky: None,
                },
                scaling: None,
                revisions: vec![],
                rollout: None,
                mirror: None,
                failover: None,
            },
        );
        config
    }

    /// Build a Gateway with live_registry and live_router_table populated
    fn gateway_with_live_data() -> Gateway {
        let config = full_config();
        let gw = Gateway::new(config.clone()).unwrap();

        let rt = RouterTable::from_config(&config.routers).unwrap();
        let reg = ServiceRegistry::from_config(&config.services).unwrap();
        gw.set_live_data(Arc::new(rt), Arc::new(reg));

        gw
    }

    // --- DashboardApi ---

    #[test]
    fn test_dashboard_matches() {
        let api = DashboardApi::new("/api/gateway");
        assert!(api.matches("/api/gateway/health"));
        assert!(api.matches("/api/gateway/metrics"));
        assert!(!api.matches("/other/path"));
    }

    #[test]
    fn test_dashboard_health() {
        let api = DashboardApi::new("/api/gateway");
        let gw = Gateway::new(minimal_config()).unwrap();
        let resp = api.handle("/api/gateway/health", &gw).unwrap();
        assert_eq!(resp.status, 200);
        assert!(resp.content_type.contains("json"));
        assert!(resp.body.contains("Created"));
    }

    #[test]
    fn test_dashboard_metrics() {
        let api = DashboardApi::new("/api/gateway");
        let gw = Gateway::new(minimal_config()).unwrap();
        let resp = api.handle("/api/gateway/metrics", &gw).unwrap();
        assert_eq!(resp.status, 200);
        assert!(resp.content_type.contains("text/plain"));
    }

    #[test]
    fn test_dashboard_config() {
        let api = DashboardApi::new("/api/gateway");
        let gw = Gateway::new(minimal_config()).unwrap();
        let resp = api.handle("/api/gateway/config", &gw).unwrap();
        assert_eq!(resp.status, 200);
        assert!(resp.body.contains("entrypoints"));
    }

    #[test]
    fn test_dashboard_not_found() {
        let api = DashboardApi::new("/api/gateway");
        let gw = Gateway::new(minimal_config()).unwrap();
        let resp = api.handle("/api/gateway/unknown", &gw).unwrap();
        assert_eq!(resp.status, 404);
    }

    #[test]
    fn test_dashboard_no_match() {
        let api = DashboardApi::new("/api/gateway");
        let gw = Gateway::new(minimal_config()).unwrap();
        let resp = api.handle("/other/path", &gw);
        assert!(resp.is_none());
    }

    #[test]
    fn test_dashboard_routes_endpoint() {
        let api = DashboardApi::new("/api/gateway");
        let gw = gateway_with_live_data();
        let resp = api.handle("/api/gateway/routes", &gw).unwrap();
        assert_eq!(resp.status, 200);
        assert!(resp.body.contains("api"));
        assert!(resp.body.contains("PathPrefix"));
    }

    #[test]
    fn test_dashboard_routes_trailing_slash() {
        let api = DashboardApi::new("/api/gateway");
        let gw = gateway_with_live_data();
        let resp = api.handle("/api/gateway/routes/", &gw).unwrap();
        assert_eq!(resp.status, 200);
    }

    #[test]
    fn test_dashboard_route_by_name() {
        let api = DashboardApi::new("/api/gateway");
        let gw = gateway_with_live_data();
        let resp = api.handle("/api/gateway/routes/api", &gw).unwrap();
        assert_eq!(resp.status, 200);
        assert!(resp.body.contains("backend"));
    }

    #[test]
    fn test_dashboard_route_by_name_not_found() {
        let api = DashboardApi::new("/api/gateway");
        let gw = gateway_with_live_data();
        let resp = api.handle("/api/gateway/routes/nonexistent", &gw).unwrap();
        assert_eq!(resp.status, 404);
        assert!(resp.body.contains("Route not found"));
    }

    #[test]
    fn test_dashboard_services_endpoint() {
        let api = DashboardApi::new("/api/gateway");
        let gw = gateway_with_live_data();
        let resp = api.handle("/api/gateway/services", &gw).unwrap();
        assert_eq!(resp.status, 200);
        assert!(resp.body.contains("backend"));
        assert!(resp.body.contains("backends_healthy"));
    }

    #[test]
    fn test_dashboard_service_by_name() {
        let api = DashboardApi::new("/api/gateway");
        let gw = gateway_with_live_data();
        let resp = api.handle("/api/gateway/services/backend", &gw).unwrap();
        assert_eq!(resp.status, 200);
        assert!(resp.body.contains("http://127.0.0.1:8001"));
    }

    #[test]
    fn test_dashboard_service_by_name_not_found() {
        let api = DashboardApi::new("/api/gateway");
        let gw = gateway_with_live_data();
        let resp = api
            .handle("/api/gateway/services/nonexistent", &gw)
            .unwrap();
        assert_eq!(resp.status, 404);
        assert!(resp.body.contains("Service not found"));
    }

    #[test]
    fn test_dashboard_backends_endpoint() {
        let api = DashboardApi::new("/api/gateway");
        let gw = gateway_with_live_data();
        let resp = api.handle("/api/gateway/backends", &gw).unwrap();
        assert_eq!(resp.status, 200);
        assert!(resp.body.contains("http://127.0.0.1:8001"));
        assert!(resp.body.contains("\"service\""));
    }

    #[test]
    fn test_dashboard_version_endpoint() {
        let api = DashboardApi::new("/api/gateway");
        let gw = Gateway::new(minimal_config()).unwrap();
        let resp = api.handle("/api/gateway/version", &gw).unwrap();
        assert_eq!(resp.status, 200);
        assert!(resp.body.contains("a3s-gateway"));
        assert!(resp.body.contains("0.1.0"));
    }

    #[test]
    fn test_version_info() {
        let v = VersionInfo::current();
        assert_eq!(v.name, "a3s-gateway");
        assert!(!v.version.is_empty());
    }

    #[test]
    fn test_dashboard_response_helpers() {
        let resp = DashboardResponse::json(200, "{}".to_string());
        assert_eq!(resp.status, 200);
        assert_eq!(resp.content_type, "application/json");

        let resp = DashboardResponse::not_found("gone");
        assert_eq!(resp.status, 404);
        assert!(resp.body.contains("gone"));
    }
}