Skip to content

Commit dff152b

Browse files
committed
examples: adds axum example
1 parent 4576eb3 commit dff152b

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

Cargo.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@ hyper = { version = "0.14", features = ["server", "tcp", "http1"] }
2222
async-std = { version = "1", features = ["attributes"] }
2323
smol = "1"
2424
anyhow = "1"
25-
serial_test = "0.5"
25+
serial_test = "3"
26+
axum = "0.7"
2627

2728
[[example]]
2829
name = "simple"
2930

31+
[[example]]
32+
name = "axum"
33+
required-features = ["tokio"]
34+
3035
[[example]]
3136
name = "tokio"
3237
required-features = ["tokio"]

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ elegant-departure = { version = "0.2", features = "tokio" }
4343
Examples can be found in the [example](./examples/) directory:
4444

4545
- [Simple](./examples/simple.rs): simple example without tokio integration
46+
- [Simple](./examples/axum.rs): Axum integration example
4647
- [Tokio](./examples/tokio.rs): Tokio integration example
4748
- [Hyper](./examples/hyper.rs): a shutdown example using the Hyper webserver
4849
- [Worker](./examples/worker.rs): example implementation of a worker using `select!`

examples/axum.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use axum::{routing::get, Router};
2+
3+
#[tokio::main]
4+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
5+
let app = Router::new().route("/", get(|| async { "Hello, World!" }));
6+
7+
println!("Listening on port 3000!");
8+
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?;
9+
10+
axum::serve(listener, app)
11+
.with_graceful_shutdown(elegant_departure::tokio::depart().on_termination())
12+
.await?;
13+
14+
Ok(())
15+
}

src/lib.rs

+24
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,28 @@
2525
//! elegant-departure = { version = "0.2", features = "tokio" }
2626
//! ```
2727
//!
28+
//! # Example: Axum
29+
//!
30+
//! Axum is easily integrated through the `tokio` integration.
31+
//!
32+
//! ```no_run
33+
//! use axum::{routing::get, Router};
34+
//!
35+
//! #[tokio::main]
36+
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
37+
//! let app = Router::new().route("/", get(|| async { "Hello, World!" }));
38+
//!
39+
//! println!("Listening on port 3000!");
40+
//! let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?;
41+
//!
42+
//! axum::serve(listener, app)
43+
//! .with_graceful_shutdown(elegant_departure::tokio::depart().on_termination())
44+
//! .await?;
45+
//!
46+
//! Ok(())
47+
//! }
48+
//! ```
49+
//!
2850
//! # Example: Simple worker
2951
//!
3052
//! A minimal example with multiple workers getting notified on shutdown.
@@ -110,6 +132,7 @@
110132
//! More examples can be found in the [examples] directory of the source code repository:
111133
//!
112134
//! - [Simple]: the full simple example from above
135+
//! - [Axum]: the full axum example from above
113136
//! - [Tokio]: the full tokio example from above
114137
//! - [Hyper]: a shutdown example using the Hyper webserver
115138
//! - [Worker]: example implementation of a worker using `select!`
@@ -118,6 +141,7 @@
118141
//!
119142
//! [examples]: https://github.com/Dav1dde/elegant-departure/tree/master/examples
120143
//! [Simple]: https://github.com/Dav1dde/elegant-departure/tree/master/examples/simple.rs
144+
//! [Axum]: https://github.com/Dav1dde/elegant-departure/tree/master/examples/axum.rs
121145
//! [Tokio]: https://github.com/Dav1dde/elegant-departure/tree/master/examples/tokio.rs
122146
//! [Hyper]: https://github.com/Dav1dde/elegant-departure/tree/master/examples/hyper.rs
123147
//! [Worker]: https://github.com/Dav1dde/elegant-departure/tree/master/examples/worker.rs

0 commit comments

Comments
 (0)