Skip to content

Commit ff26732

Browse files
Preslav LeConvex, Inc.
authored andcommitted
Make serve_http() more generic (#24714)
I am going to reuse serve_http() to start a metrics/debug only handler. GitOrigin-RevId: 90269b4b1744a5ffb1be7ec848bcf3f4d54420a2
1 parent 4e463a3 commit ff26732

File tree

5 files changed

+29
-32
lines changed

5 files changed

+29
-32
lines changed

crates/common/src/http/mod.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use axum::{
2929
},
3030
error_handling::HandleErrorLayer,
3131
extract::{
32+
connect_info::IntoMakeServiceWithConnectInfo,
3233
FromRequestParts,
3334
State,
3435
},
@@ -583,6 +584,17 @@ impl ConvexHttpService {
583584
Self { router }
584585
}
585586

587+
pub async fn serve<F: Future<Output = ()>>(
588+
self,
589+
addr: SocketAddr,
590+
shutdown: F,
591+
) -> anyhow::Result<()> {
592+
let make_svc = self
593+
.router
594+
.into_make_service_with_connect_info::<SocketAddr>();
595+
serve_http(make_svc, addr, shutdown).await
596+
}
597+
586598
#[cfg(any(test, feature = "testing"))]
587599
pub fn new_for_test(router: Router<(), Body>) -> Self {
588600
Self { router }
@@ -628,7 +640,7 @@ where
628640

629641
/// Serves an HTTP server using the given service.
630642
pub async fn serve_http<F>(
631-
service: ConvexHttpService,
643+
service: IntoMakeServiceWithConnectInfo<Router, SocketAddr>,
632644
addr: SocketAddr,
633645
shutdown: F,
634646
) -> anyhow::Result<()>
@@ -652,13 +664,10 @@ where
652664
incoming_sockets.set_sleep_on_errors(true);
653665
let addr = incoming_sockets.local_addr();
654666

655-
let make_svc = service
656-
.router
657-
.into_make_service_with_connect_info::<SocketAddr>();
658667
tracing::info!("Listening on http://{}", addr);
659668
hyper::Server::builder(incoming_sockets)
660669
.http2_max_concurrent_streams(MAX_HTTP2_STREAMS)
661-
.serve(make_svc)
670+
.serve(service)
662671
.with_graceful_shutdown(shutdown)
663672
.await?;
664673
tracing::info!("HTTP server shutdown complete");

crates/isolate/src/tests/fetch.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use axum::{
2121
use common::{
2222
assert_obj,
2323
http::{
24-
serve_http,
2524
ConvexHttpService,
2625
NoopRouteMapper,
2726
},
@@ -64,14 +63,14 @@ async fn test_fetch_not_allowed_in_queries(rt: TestRuntime) -> anyhow::Result<()
6463

6564
async fn serve(router: Router, port: u16) {
6665
let (_shutdown_tx, mut shutdown_rx) = async_broadcast::broadcast::<()>(1);
67-
_ = serve_http(
68-
ConvexHttpService::new(
69-
router,
70-
"0.0.1".to_owned(),
71-
1,
72-
Duration::from_secs(125),
73-
NoopRouteMapper,
74-
),
66+
_ = ConvexHttpService::new(
67+
router,
68+
"0.0.1".to_owned(),
69+
1,
70+
Duration::from_secs(125),
71+
NoopRouteMapper,
72+
)
73+
.serve(
7574
SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), port).into(),
7675
async move {
7776
let _ = shutdown_rx.recv().await;

crates/local_backend/src/main.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ use clap::Parser;
1010
use cmd_util::env::config_service;
1111
use common::{
1212
errors::MainError,
13-
http::{
14-
serve_http,
15-
ConvexHttpService,
16-
},
13+
http::ConvexHttpService,
1714
runtime::Runtime,
1815
version::SERVER_VERSION_STR,
1916
};
@@ -96,13 +93,9 @@ async fn run_server_inner(runtime: ProdRuntime, config: LocalConfig) -> anyhow::
9693
Duration::from_secs(125),
9794
BackendRouteMapper,
9895
);
99-
let serve_http_future = serve_http(
100-
http_service,
101-
config.http_bind_address().into(),
102-
async move {
103-
let _ = shutdown_rx_.recv().await;
104-
},
105-
);
96+
let serve_http_future = http_service.serve(config.http_bind_address().into(), async move {
97+
let _ = shutdown_rx_.recv().await;
98+
});
10699
let proxy_future = dev_site_proxy(
107100
config.site_bind_address(),
108101
config.convex_origin_url(),

crates/local_backend/src/proxy.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use axum::{
1111
};
1212
use common::{
1313
http::{
14-
serve_http,
1514
ConvexHttpService,
1615
HttpResponseError,
1716
NoopRouteMapper,
@@ -63,7 +62,7 @@ pub async fn dev_site_proxy(
6362
Duration::from_secs(125),
6463
NoopRouteMapper,
6564
);
66-
let proxy_server = serve_http(service, addr.into(), async move {
65+
let proxy_server = service.serve(addr.into(), async move {
6766
let _ = shutdown_rx.recv().await;
6867
tracing::info!("Shut down proxy");
6968
});

crates/local_backend/src/subs/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,7 @@ mod tests {
375375
routing::get,
376376
Router,
377377
};
378-
use common::http::{
379-
serve_http,
380-
ConvexHttpService,
381-
};
378+
use common::http::ConvexHttpService;
382379
use futures::{
383380
SinkExt,
384381
StreamExt,
@@ -419,7 +416,7 @@ mod tests {
419416
let port = portpicker::pick_unused_port().expect("No ports free");
420417
let addr = format!("127.0.0.1:{port}").parse()?;
421418
let (shutdown_tx, shutdown_rx) = futures::channel::oneshot::channel();
422-
let proxy_server = tokio::spawn(serve_http(app, addr, async move {
419+
let proxy_server = tokio::spawn(app.serve(addr, async move {
423420
shutdown_rx.await.unwrap();
424421
}));
425422

0 commit comments

Comments
 (0)