-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun.rs
More file actions
462 lines (414 loc) · 16.1 KB
/
Copy pathrun.rs
File metadata and controls
462 lines (414 loc) · 16.1 KB
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
#[cfg(feature = "dashboard")]
use super::helpers::{
infer_route_feature_gates, is_dashboard_replay_eligible, openapi_tags_for_route,
};
use super::types::RustApi;
use crate::error::Result;
use crate::middleware::BodyLimitLayer;
use crate::response::IntoResponse;
use crate::server::Server;
impl RustApi {
async fn prepare_for_serve(&mut self, addr: &str) {
self.maybe_dump_openapi();
self.print_hot_reload_banner(addr);
self.apply_health_endpoints();
self.apply_status_page();
#[cfg(feature = "dashboard")]
self.apply_dashboard();
if let Some(limit) = self.body_limit {
self.layers.prepend(Box::new(BodyLimitLayer::new(limit)));
}
for hook in std::mem::take(&mut self.lifecycle_hooks.on_start) {
hook().await;
}
}
/// Returns `None` when hot-reload is disabled; otherwise whether a watcher was
/// already active before this call updated `RUSTAPI_HOT_RELOAD`.
pub(super) fn print_hot_reload_banner(&self, addr: &str) -> Option<bool> {
if !self.hot_reload {
return None;
}
let is_under_watcher = std::env::var("RUSTAPI_HOT_RELOAD")
.map(|v| v == "1")
.unwrap_or(false);
// Set the env var so the CLI watcher can detect it
std::env::set_var("RUSTAPI_HOT_RELOAD", "1");
tracing::info!("Hot-reload mode enabled");
if is_under_watcher {
tracing::info!(" File watcher active - changes will trigger rebuild + restart");
} else {
tracing::info!(" Tip: Run with `cargo rustapi run --watch` for automatic hot-reload");
}
tracing::info!(" Listening on http://{addr}");
Some(is_under_watcher)
}
async fn run_shutdown_hooks(hooks: Vec<crate::events::LifecycleHook>) {
for hook in hooks {
hook().await;
}
}
pub(super) fn apply_status_page(&mut self) {
if let Some(config) = &self.status_config {
let monitor = std::sync::Arc::new(crate::status::StatusMonitor::new());
// 1. Add middleware layer
self.layers
.push(Box::new(crate::status::StatusLayer::new(monitor.clone())));
// 2. Add status route
use crate::router::MethodRouter;
use std::collections::HashMap;
let monitor = monitor.clone();
let config = config.clone();
let path = config.path.clone(); // Clone path before moving config
let handler: crate::handler::BoxedHandler = std::sync::Arc::new(move |_| {
let monitor = monitor.clone();
let config = config.clone();
Box::pin(async move {
crate::status::status_handler(monitor, config)
.await
.into_response()
})
});
let mut handlers = HashMap::new();
handlers.insert(http::Method::GET, handler);
let method_router = MethodRouter::from_boxed(handlers);
// We need to take the router out to call route() which consumes it
let router = std::mem::take(&mut self.router);
self.router = router.route(&path, method_router);
}
}
#[cfg(feature = "dashboard")]
pub(super) fn apply_dashboard(&mut self) {
use crate::dashboard::{DashboardMetrics, RouteInventoryItem};
use crate::handler::BoxedHandler;
use crate::response::Body;
use crate::router::MethodRouter;
use std::collections::HashMap;
let mut config = match self.dashboard_config.take() {
Some(c) => c,
None => return,
};
config.normalize_paths();
// Build route inventory from currently registered routes. This snapshot
// intentionally happens before dashboard routes are mounted so the UI
// represents application endpoints rather than the dashboard itself.
let mut inventory: Vec<RouteInventoryItem> = self
.router
.registered_routes()
.values()
.map(|info| {
let methods: Vec<String> = info.methods.iter().map(|m| m.to_string()).collect();
let health_eligible = self
.health_endpoint_config
.as_ref()
.map(|health| {
info.path == health.health_path
|| info.path == health.readiness_path
|| info.path == health.liveness_path
})
.unwrap_or(false);
RouteInventoryItem::new(info.path.clone(), methods)
.with_tags(openapi_tags_for_route(
&self.openapi_spec,
&info.path,
&info.methods,
))
.with_feature_gates(infer_route_feature_gates(&info.path))
.health_eligible(health_eligible)
.replay_eligible(is_dashboard_replay_eligible(&info.path, health_eligible))
})
.collect();
inventory.sort_by(|a, b| a.path.cmp(&b.path));
let metrics = std::sync::Arc::new(DashboardMetrics::new_with_replay_admin_path(
inventory,
config.replay_api_path.clone(),
));
// Insert metrics into router state using the public .state() API
let router = std::mem::take(&mut self.router);
self.router = router.state(std::sync::Arc::clone(&metrics));
// Register dashboard routes
let prefix = config.path.trim_end_matches('/').to_owned();
fn not_found() -> crate::response::Response {
http::Response::builder()
.status(404)
.body(Body::Full(http_body_util::Full::new(bytes::Bytes::from(
"Not Found",
))))
.unwrap()
}
// Route 1: GET /__rustapi/dashboard (the SPA page)
{
let metrics_c = std::sync::Arc::clone(&metrics);
let config_c = config.clone();
let handler: BoxedHandler = std::sync::Arc::new(move |req| {
let metrics = std::sync::Arc::clone(&metrics_c);
let cfg = config_c.clone();
Box::pin(async move {
let headers = req.headers().clone();
let method = req.method().to_string();
let path = req.uri().path().to_owned();
crate::dashboard::routes::dispatch(&headers, &method, &path, &metrics, &cfg)
.await
.unwrap_or_else(not_found)
})
});
let mut h = HashMap::new();
h.insert(http::Method::GET, handler);
let router = std::mem::take(&mut self.router);
self.router = router.route(&prefix, MethodRouter::from_boxed(h));
}
// Route 2: GET /__rustapi/dashboard/*path (API sub-paths)
{
let metrics_c = std::sync::Arc::clone(&metrics);
let config_c = config.clone();
let wildcard_path = format!("{}/*path", prefix);
let handler: BoxedHandler = std::sync::Arc::new(move |req| {
let metrics = std::sync::Arc::clone(&metrics_c);
let cfg = config_c.clone();
Box::pin(async move {
let headers = req.headers().clone();
let method = req.method().to_string();
let path = req.uri().path().to_owned();
crate::dashboard::routes::dispatch(&headers, &method, &path, &metrics, &cfg)
.await
.unwrap_or_else(not_found)
})
});
let mut h = HashMap::new();
h.insert(http::Method::GET, handler);
let router = std::mem::take(&mut self.router);
self.router = router.route(&wildcard_path, MethodRouter::from_boxed(h));
}
}
/// Enable the embedded isometric system dashboard.
///
/// Registers a self-contained admin surface at the configured path
/// (default: `/__rustapi/dashboard`).
///
/// # Example
///
/// ```rust,ignore
/// use rustapi_core::dashboard::DashboardConfig;
///
/// RustApi::new()
/// .route("/api/users", get(list_users))
/// .dashboard(
/// DashboardConfig::new()
/// .admin_token("my-secret")
/// )
/// .run("127.0.0.1:8080")
/// .await
/// ```
#[cfg(feature = "dashboard")]
pub fn dashboard(mut self, config: crate::dashboard::DashboardConfig) -> Self {
self.dashboard_config = Some(config);
self
}
pub async fn run(mut self, addr: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
self.prepare_for_serve(addr).await;
let server = Server::new(self.router, self.layers, self.interceptors);
server.run(addr).await
}
/// Run the server with graceful shutdown signal
pub async fn run_with_shutdown<F>(
mut self,
addr: impl AsRef<str>,
signal: F,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
where
F: std::future::Future<Output = ()> + Send + 'static,
{
self.prepare_for_serve(addr.as_ref()).await;
let shutdown_hooks = std::mem::take(&mut self.lifecycle_hooks.on_shutdown);
let server = Server::new(self.router, self.layers, self.interceptors);
server.run_with_shutdown(addr.as_ref(), signal).await?;
Self::run_shutdown_hooks(shutdown_hooks).await;
Ok(())
}
/// Enable HTTP/3 support with TLS certificates
///
/// HTTP/3 requires TLS certificates. For development, you can use
/// self-signed certificates with `run_http3_dev`.
///
/// # Example
///
/// ```rust,ignore
/// RustApi::new()
/// .route("/", get(hello))
/// .run_http3("0.0.0.0:443", "cert.pem", "key.pem")
/// .await
/// ```
/// Run HTTP/3 with TLS certificates and a graceful shutdown signal.
#[cfg(feature = "http3")]
pub async fn run_http3_with_shutdown<F>(
mut self,
config: crate::http3::Http3Config,
signal: F,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
where
F: std::future::Future<Output = ()> + Send + 'static,
{
use std::sync::Arc;
let addr = config.socket_addr();
self.prepare_for_serve(&addr).await;
let shutdown_hooks = std::mem::take(&mut self.lifecycle_hooks.on_shutdown);
let server = crate::http3::Http3Server::new(
&config,
Arc::new(self.router.clone()),
Arc::new(self.layers.clone()),
Arc::new(self.interceptors.clone()),
)
.await?;
server.run_with_shutdown(signal).await?;
Self::run_shutdown_hooks(shutdown_hooks).await;
Ok(())
}
#[cfg(feature = "http3")]
pub async fn run_http3(
self,
config: crate::http3::Http3Config,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
self.run_http3_with_shutdown(config, std::future::pending())
.await
}
/// Run HTTP/3 server with self-signed certificate (development only)
///
/// This is useful for local development and testing.
/// **Do not use in production!**
///
/// # Example
///
/// ```rust,ignore
/// RustApi::new()
/// .route("/", get(hello))
/// .run_http3_dev("0.0.0.0:8443")
/// .await
/// ```
/// Run HTTP/3 (self-signed) with a graceful shutdown signal.
#[cfg(feature = "http3-dev")]
pub async fn run_http3_dev_with_shutdown<F>(
mut self,
addr: &str,
signal: F,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
where
F: std::future::Future<Output = ()> + Send + 'static,
{
use std::sync::Arc;
self.prepare_for_serve(addr).await;
let shutdown_hooks = std::mem::take(&mut self.lifecycle_hooks.on_shutdown);
let server = crate::http3::Http3Server::new_with_self_signed(
addr,
Arc::new(self.router.clone()),
Arc::new(self.layers.clone()),
Arc::new(self.interceptors.clone()),
)
.await?;
server.run_with_shutdown(signal).await?;
Self::run_shutdown_hooks(shutdown_hooks).await;
Ok(())
}
#[cfg(feature = "http3-dev")]
pub async fn run_http3_dev(
self,
addr: &str,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
self.run_http3_dev_with_shutdown(addr, std::future::pending())
.await
}
/// Configure HTTP/3 support for `run_http3` and `run_dual_stack`.
///
/// # Example
///
/// ```rust,ignore
/// RustApi::new()
/// .with_http3("cert.pem", "key.pem")
/// .run_dual_stack("127.0.0.1:8080")
/// .await
/// ```
#[cfg(feature = "http3")]
pub fn with_http3(mut self, cert_path: impl Into<String>, key_path: impl Into<String>) -> Self {
self.http3_config = Some(crate::http3::Http3Config::new(cert_path, key_path));
self
}
/// Run both HTTP/1.1 (TCP) and HTTP/3 (QUIC/UDP) simultaneously.
///
/// The HTTP/3 listener is bound to the same host and port as `http_addr`
/// so clients can upgrade to either protocol on one endpoint.
///
/// # Example
///
/// ```rust,ignore
/// RustApi::new()
/// .route("/", get(hello))
/// .with_http3("cert.pem", "key.pem")
/// .run_dual_stack("0.0.0.0:8080")
/// .await
/// ```
/// Run HTTP/1.1 and HTTP/3 together with a graceful shutdown signal.
#[cfg(feature = "http3")]
pub async fn run_dual_stack_with_shutdown<F>(
mut self,
http_addr: &str,
signal: F,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
where
F: std::future::Future<Output = ()> + Send + 'static,
{
use std::sync::Arc;
let mut config = self
.http3_config
.take()
.ok_or("HTTP/3 config not set. Use .with_http3(...)")?;
let http_socket: std::net::SocketAddr = http_addr.parse()?;
config.bind_addr = if http_socket.ip().is_ipv6() {
format!("[{}]", http_socket.ip())
} else {
http_socket.ip().to_string()
};
config.port = http_socket.port();
let http_addr = http_socket.to_string();
self.prepare_for_serve(&http_addr).await;
let shutdown_hooks = std::mem::take(&mut self.lifecycle_hooks.on_shutdown);
let router = Arc::new(self.router);
let layers = Arc::new(self.layers);
let interceptors = Arc::new(self.interceptors);
let http1_server =
Server::from_shared(router.clone(), layers.clone(), interceptors.clone());
let http3_server =
crate::http3::Http3Server::new(&config, router, layers, interceptors).await?;
tracing::info!(
http1_addr = %http_addr,
http3_addr = %config.socket_addr(),
"Starting dual-stack HTTP/1.1 + HTTP/3 servers"
);
let notify = std::sync::Arc::new(tokio::sync::Notify::new());
let notify_for_signal = notify.clone();
tokio::spawn(async move {
signal.await;
notify_for_signal.notify_waiters();
});
let wait_for_shutdown = {
let notify = notify.clone();
async move {
notify.notified().await;
}
};
let wait_for_shutdown_http3 = async move {
notify.notified().await;
};
tokio::try_join!(
http1_server.run_with_shutdown(&http_addr, wait_for_shutdown),
http3_server.run_with_shutdown(wait_for_shutdown_http3),
)?;
Self::run_shutdown_hooks(shutdown_hooks).await;
Ok(())
}
#[cfg(feature = "http3")]
pub async fn run_dual_stack(
self,
http_addr: &str,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
self.run_dual_stack_with_shutdown(http_addr, std::future::pending())
.await
}
}