diff --git a/Cargo.toml b/Cargo.toml index 7c949e5..7b1c052 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,11 +22,16 @@ hyper = { version = "0.14", features = ["server", "tcp", "http1"] } async-std = { version = "1", features = ["attributes"] } smol = "1" anyhow = "1" -serial_test = "0.5" +serial_test = "3" +axum = "0.7" [[example]] name = "simple" +[[example]] +name = "axum" +required-features = ["tokio"] + [[example]] name = "tokio" required-features = ["tokio"] diff --git a/README.md b/README.md index ba0644c..6b72e53 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ elegant-departure = { version = "0.2", features = "tokio" } Examples can be found in the [example](./examples/) directory: - [Simple](./examples/simple.rs): simple example without tokio integration +- [Simple](./examples/axum.rs): Axum integration example - [Tokio](./examples/tokio.rs): Tokio integration example - [Hyper](./examples/hyper.rs): a shutdown example using the Hyper webserver - [Worker](./examples/worker.rs): example implementation of a worker using `select!` diff --git a/examples/axum.rs b/examples/axum.rs new file mode 100644 index 0000000..4df4198 --- /dev/null +++ b/examples/axum.rs @@ -0,0 +1,15 @@ +use axum::{routing::get, Router}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let app = Router::new().route("/", get(|| async { "Hello, World!" })); + + println!("Listening on port 3000!"); + let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?; + + axum::serve(listener, app) + .with_graceful_shutdown(elegant_departure::tokio::depart().on_termination()) + .await?; + + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs index ee67c4d..396eb84 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,28 @@ //! elegant-departure = { version = "0.2", features = "tokio" } //! ``` //! +//! # Example: Axum +//! +//! Axum is easily integrated through the `tokio` integration. +//! +//! ```no_run +//! use axum::{routing::get, Router}; +//! +//! #[tokio::main] +//! async fn main() -> Result<(), Box> { +//! let app = Router::new().route("/", get(|| async { "Hello, World!" })); +//! +//! println!("Listening on port 3000!"); +//! let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?; +//! +//! axum::serve(listener, app) +//! .with_graceful_shutdown(elegant_departure::tokio::depart().on_termination()) +//! .await?; +//! +//! Ok(()) +//! } +//! ``` +//! //! # Example: Simple worker //! //! A minimal example with multiple workers getting notified on shutdown. @@ -110,6 +132,7 @@ //! More examples can be found in the [examples] directory of the source code repository: //! //! - [Simple]: the full simple example from above +//! - [Axum]: the full axum example from above //! - [Tokio]: the full tokio example from above //! - [Hyper]: a shutdown example using the Hyper webserver //! - [Worker]: example implementation of a worker using `select!` @@ -118,6 +141,7 @@ //! //! [examples]: https://github.com/Dav1dde/elegant-departure/tree/master/examples //! [Simple]: https://github.com/Dav1dde/elegant-departure/tree/master/examples/simple.rs +//! [Axum]: https://github.com/Dav1dde/elegant-departure/tree/master/examples/axum.rs //! [Tokio]: https://github.com/Dav1dde/elegant-departure/tree/master/examples/tokio.rs //! [Hyper]: https://github.com/Dav1dde/elegant-departure/tree/master/examples/hyper.rs //! [Worker]: https://github.com/Dav1dde/elegant-departure/tree/master/examples/worker.rs