Skip to content

Commit

Permalink
examples: adds axum example
Browse files Browse the repository at this point in the history
  • Loading branch information
Dav1dde committed Apr 23, 2024
1 parent 4576eb3 commit dff152b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!`
Expand Down
15 changes: 15 additions & 0 deletions examples/axum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use axum::{routing::get, Router};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
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(())
}
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
//! 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.
Expand Down Expand Up @@ -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!`
Expand All @@ -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
Expand Down

0 comments on commit dff152b

Please sign in to comment.