Skip to content
Discussion options

You must be logged in to vote

The easiest way is just to spawn a new async task with tokio::spawn:

use std::time::Duration;

use axum::{routing::get, Router};
use tower_http::timeout::TimeoutLayer;

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/", get(|| async { "Hello, World!" }))
        .layer(TimeoutLayer::new(Duration::from_secs(10)));

    let (close_tx, close_rx) = tokio::sync::oneshot::channel();

    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
        .await
        .unwrap();
    let server_handle = tokio::spawn(async {
        axum::serve(listener, app)
            .with_graceful_shutdown(async move {
                _ = close_rx.await;
            })

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by John-Chan
Comment options

You must be logged in to vote
2 replies
@jplatte
Comment options

@danbachar
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants