Skip to content

Commit aac3ddd

Browse files
authored
test(#201): exercise real run entrypoints via prepare_for_serve (#204)
1 parent a48eb85 commit aac3ddd

3 files changed

Lines changed: 283 additions & 129 deletions

File tree

crates/rustapi-core/src/app/run.rs

Lines changed: 24 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,33 @@ use crate::middleware::BodyLimitLayer;
88
use crate::response::IntoResponse;
99
use crate::server::Server;
1010
impl RustApi {
11+
async fn prepare_for_serve(&mut self, addr: &str) {
12+
self.maybe_dump_openapi();
13+
self.print_hot_reload_banner(addr);
14+
self.apply_health_endpoints();
15+
self.apply_status_page();
16+
#[cfg(feature = "dashboard")]
17+
self.apply_dashboard();
18+
if let Some(limit) = self.body_limit {
19+
self.layers.prepend(Box::new(BodyLimitLayer::new(limit)));
20+
}
21+
for hook in std::mem::take(&mut self.lifecycle_hooks.on_start) {
22+
hook().await;
23+
}
24+
}
25+
1126
pub(super) fn print_hot_reload_banner(&self, addr: &str) {
1227
if !self.hot_reload {
1328
return;
1429
}
1530

16-
// Set the env var so the CLI watcher can detect it
17-
std::env::set_var("RUSTAPI_HOT_RELOAD", "1");
18-
1931
let is_under_watcher = std::env::var("RUSTAPI_HOT_RELOAD")
2032
.map(|v| v == "1")
2133
.unwrap_or(false);
2234

35+
// Set the env var so the CLI watcher can detect it
36+
std::env::set_var("RUSTAPI_HOT_RELOAD", "1");
37+
2338
tracing::info!("Hot-reload mode enabled");
2439

2540
if is_under_watcher {
@@ -204,31 +219,7 @@ impl RustApi {
204219
self
205220
}
206221
pub async fn run(mut self, addr: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
207-
self.maybe_dump_openapi();
208-
209-
// Hot-reload mode banner
210-
self.print_hot_reload_banner(addr);
211-
212-
// Apply health endpoints if configured
213-
self.apply_health_endpoints();
214-
215-
// Apply status page if configured
216-
self.apply_status_page();
217-
218-
// Apply embedded dashboard if configured
219-
#[cfg(feature = "dashboard")]
220-
self.apply_dashboard();
221-
222-
// Apply body limit layer if configured (should be first in the chain)
223-
if let Some(limit) = self.body_limit {
224-
// Prepend body limit layer so it's the first to process requests
225-
self.layers.prepend(Box::new(BodyLimitLayer::new(limit)));
226-
}
227-
228-
// Run on_start lifecycle hooks before accepting connections
229-
for hook in self.lifecycle_hooks.on_start {
230-
hook().await;
231-
}
222+
self.prepare_for_serve(addr).await;
232223

233224
let server = Server::new(self.router, self.layers, self.interceptors);
234225
server.run(addr).await
@@ -243,29 +234,7 @@ impl RustApi {
243234
where
244235
F: std::future::Future<Output = ()> + Send + 'static,
245236
{
246-
self.maybe_dump_openapi();
247-
248-
// Hot-reload mode banner
249-
self.print_hot_reload_banner(addr.as_ref());
250-
251-
// Apply health endpoints if configured
252-
self.apply_health_endpoints();
253-
254-
// Apply status page if configured
255-
self.apply_status_page();
256-
257-
// Apply embedded dashboard if configured
258-
#[cfg(feature = "dashboard")]
259-
self.apply_dashboard();
260-
261-
if let Some(limit) = self.body_limit {
262-
self.layers.prepend(Box::new(BodyLimitLayer::new(limit)));
263-
}
264-
265-
// Run on_start lifecycle hooks before accepting connections
266-
for hook in self.lifecycle_hooks.on_start {
267-
hook().await;
268-
}
237+
self.prepare_for_serve(addr.as_ref()).await;
269238

270239
// Wrap the shutdown signal to run on_shutdown hooks after signal fires
271240
let shutdown_hooks = self.lifecycle_hooks.on_shutdown;
@@ -303,16 +272,8 @@ impl RustApi {
303272
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
304273
use std::sync::Arc;
305274

306-
// Apply health endpoints if configured
307-
self.apply_health_endpoints();
308-
309-
// Apply status page if configured
310-
self.apply_status_page();
311-
312-
// Apply body limit layer if configured
313-
if let Some(limit) = self.body_limit {
314-
self.layers.prepend(Box::new(BodyLimitLayer::new(limit)));
315-
}
275+
let addr = config.socket_addr();
276+
self.prepare_for_serve(&addr).await;
316277

317278
let server = crate::http3::Http3Server::new(
318279
&config,
@@ -345,16 +306,7 @@ impl RustApi {
345306
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
346307
use std::sync::Arc;
347308

348-
// Apply health endpoints if configured
349-
self.apply_health_endpoints();
350-
351-
// Apply status page if configured
352-
self.apply_status_page();
353-
354-
// Apply body limit layer if configured
355-
if let Some(limit) = self.body_limit {
356-
self.layers.prepend(Box::new(BodyLimitLayer::new(limit)));
357-
}
309+
self.prepare_for_serve(addr).await;
358310

359311
let server = crate::http3::Http3Server::new_with_self_signed(
360312
addr,
@@ -418,16 +370,7 @@ impl RustApi {
418370
config.port = http_socket.port();
419371
let http_addr = http_socket.to_string();
420372

421-
// Apply health endpoints if configured
422-
self.apply_health_endpoints();
423-
424-
// Apply status page if configured
425-
self.apply_status_page();
426-
427-
// Apply body limit layer if configured
428-
if let Some(limit) = self.body_limit {
429-
self.layers.prepend(Box::new(BodyLimitLayer::new(limit)));
430-
}
373+
self.prepare_for_serve(&http_addr).await;
431374

432375
let router = Arc::new(self.router);
433376
let layers = Arc::new(self.layers);

0 commit comments

Comments
 (0)