From 9fbababc3a1d28603350814e072369533badc2b1 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Mon, 2 Aug 2021 23:09:09 +0200 Subject: [PATCH] Make it clear how to run all examples (#92) * Handle errors in websocket example * Make it clear how to run all examples --- Cargo.toml | 16 ++++++++++-- examples/chat.rs | 2 +- ...error_handling_and_dependency_injection.rs | 6 +++++ examples/form.rs | 6 +++++ examples/hello_world.rs | 6 +++++ examples/key_value_store.rs | 2 +- examples/multipart_form.rs | 6 +++++ examples/sessions.rs | 8 +++++- examples/sse.rs | 6 +++++ examples/static_file_server.rs | 6 +++++ examples/templates.rs | 6 +++++ examples/testing.rs | 6 +++++ examples/tls_rustls.rs | 6 +++++ examples/todos.rs | 6 +++++ examples/tokio_postgres.rs | 6 +++++ examples/unix_domain_socket.rs | 6 +++++ examples/versioning.rs | 6 +++++ examples/websocket.rs | 26 ++++++++++++------- 18 files changed, 117 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f32b9ef8..bcf17d4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -79,8 +79,20 @@ all-features = true rustdoc-args = ["--cfg", "docsrs"] [package.metadata.playground] -features = ["ws"] +features = ["ws", "multipart", "headers"] [[example]] name = "chat" -required-features = ["ws"] \ No newline at end of file +required-features = ["ws"] + +[[example]] +name = "multipart_form" +required-features = ["multipart"] + +[[example]] +name = "sse" +required-features = ["headers"] + +[[example]] +name = "websocket" +required-features = ["ws", "headers"] diff --git a/examples/chat.rs b/examples/chat.rs index d1358bc5..fdf7246b 100644 --- a/examples/chat.rs +++ b/examples/chat.rs @@ -2,7 +2,7 @@ //! //! Run with //! -//! ``` +//! ```not_rust //! cargo run --features=ws --example chat //! ``` diff --git a/examples/error_handling_and_dependency_injection.rs b/examples/error_handling_and_dependency_injection.rs index fb42e336..dbc3fa28 100644 --- a/examples/error_handling_and_dependency_injection.rs +++ b/examples/error_handling_and_dependency_injection.rs @@ -1,5 +1,11 @@ //! Example showing how to convert errors into responses and how one might do //! dependency injection using trait objects. +//! +//! Run with +//! +//! ```not_rust +//! cargo run --example error_handling_and_dependency_injection +//! ``` #![allow(dead_code)] diff --git a/examples/form.rs b/examples/form.rs index af93e3e8..ec04869b 100644 --- a/examples/form.rs +++ b/examples/form.rs @@ -1,3 +1,9 @@ +//! Run with +//! +//! ```not_rust +//! cargo run --example form +//! ``` + use axum::prelude::*; use serde::Deserialize; use std::net::SocketAddr; diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 08f1bc5e..d49f0692 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -1,3 +1,9 @@ +//! Run with +//! +//! ```not_rust +//! cargo run --example hello_world +//! ``` + use axum::prelude::*; use std::net::SocketAddr; diff --git a/examples/key_value_store.rs b/examples/key_value_store.rs index 9d83b910..506c44be 100644 --- a/examples/key_value_store.rs +++ b/examples/key_value_store.rs @@ -3,7 +3,7 @@ //! Run with: //! //! ```not_rust -//! RUST_LOG=tower_http=debug,key_value_store=trace cargo run --example key_value_store +//! cargo run --example key_value_store //! ``` use axum::{ diff --git a/examples/multipart_form.rs b/examples/multipart_form.rs index 4dcbd6d1..d8f70ba7 100644 --- a/examples/multipart_form.rs +++ b/examples/multipart_form.rs @@ -1,3 +1,9 @@ +//! Run with +//! +//! ```not_rust +//! cargo run --example multipart_form --features=multipart +//! ``` + use axum::{ extract::{ContentLengthLimit, Multipart}, prelude::*, diff --git a/examples/sessions.rs b/examples/sessions.rs index 6677b939..f865f6d0 100644 --- a/examples/sessions.rs +++ b/examples/sessions.rs @@ -1,3 +1,9 @@ +//! Run with +//! +//! ```not_rust +//! cargo run --example sessions +//! ``` + use async_session::{MemoryStore, Session, SessionStore as _}; use axum::{ async_trait, @@ -6,7 +12,7 @@ use axum::{ response::IntoResponse, AddExtensionLayer, }; -use headers::{HeaderMap, HeaderValue}; +use http::header::{HeaderMap, HeaderValue}; use http::StatusCode; use serde::{Deserialize, Serialize}; use std::net::SocketAddr; diff --git a/examples/sse.rs b/examples/sse.rs index 760ed2f4..c30951db 100644 --- a/examples/sse.rs +++ b/examples/sse.rs @@ -1,3 +1,9 @@ +//! Run with +//! +//! ```not_rust +//! cargo run --example sse --features=headers +//! ``` + use axum::{extract::TypedHeader, prelude::*, routing::nest, service::ServiceExt, sse::Event}; use futures::stream::{self, Stream}; use http::StatusCode; diff --git a/examples/static_file_server.rs b/examples/static_file_server.rs index 07e1137a..cc435cf4 100644 --- a/examples/static_file_server.rs +++ b/examples/static_file_server.rs @@ -1,3 +1,9 @@ +//! Run with +//! +//! ```not_rust +//! cargo run --example static_file_server +//! ``` + use axum::{prelude::*, routing::nest, service::ServiceExt}; use http::StatusCode; use std::net::SocketAddr; diff --git a/examples/templates.rs b/examples/templates.rs index 5a42e4b4..f7e47be5 100644 --- a/examples/templates.rs +++ b/examples/templates.rs @@ -1,3 +1,9 @@ +//! Run with +//! +//! ```not_rust +//! cargo run --example templates +//! ``` + use askama::Template; use axum::{prelude::*, response::IntoResponse}; use http::{Response, StatusCode}; diff --git a/examples/testing.rs b/examples/testing.rs index ca28d25e..3501fe10 100644 --- a/examples/testing.rs +++ b/examples/testing.rs @@ -1,3 +1,9 @@ +//! Run with +//! +//! ```not_rust +//! cargo test --example testing +//! ``` + use axum::{prelude::*, routing::BoxRoute}; use tower_http::trace::TraceLayer; diff --git a/examples/tls_rustls.rs b/examples/tls_rustls.rs index 833012a9..fa36ccee 100644 --- a/examples/tls_rustls.rs +++ b/examples/tls_rustls.rs @@ -1,3 +1,9 @@ +//! Run with +//! +//! ```not_rust +//! cargo run --example tls_rustls +//! ``` + use axum::prelude::*; use hyper::server::conn::Http; use std::{fs::File, io::BufReader, sync::Arc}; diff --git a/examples/todos.rs b/examples/todos.rs index ac74f174..5ad1f847 100644 --- a/examples/todos.rs +++ b/examples/todos.rs @@ -6,6 +6,12 @@ //! - `POST /todos`: create a new Todo. //! - `PUT /todos/:id`: update a specific Todo. //! - `DELETE /todos/:id`: delete a specific Todo. +//! +//! Run with +//! +//! ```not_rust +//! cargo run --example todos +//! ``` use axum::{ extract::{Extension, Json, Query, UrlParams}, diff --git a/examples/tokio_postgres.rs b/examples/tokio_postgres.rs index 9a807ab5..429b8c83 100644 --- a/examples/tokio_postgres.rs +++ b/examples/tokio_postgres.rs @@ -1,3 +1,9 @@ +//! Run with +//! +//! ```not_rust +//! cargo run --example tokio_postgres +//! ``` + use axum::{extract::Extension, prelude::*, AddExtensionLayer}; use bb8::Pool; use bb8_postgres::PostgresConnectionManager; diff --git a/examples/unix_domain_socket.rs b/examples/unix_domain_socket.rs index 4a1520f2..d8fcd8df 100644 --- a/examples/unix_domain_socket.rs +++ b/examples/unix_domain_socket.rs @@ -1,3 +1,9 @@ +//! Run with +//! +//! ```not_rust +//! cargo run --example unix_domain_socket +//! ``` + use axum::{ extract::connect_info::{self, ConnectInfo}, prelude::*, diff --git a/examples/versioning.rs b/examples/versioning.rs index ca37498b..406bec55 100644 --- a/examples/versioning.rs +++ b/examples/versioning.rs @@ -1,3 +1,9 @@ +//! Run with +//! +//! ```not_rust +//! cargo run --example versioning +//! ``` + use axum::response::IntoResponse; use axum::{ async_trait, diff --git a/examples/websocket.rs b/examples/websocket.rs index 2478cc91..26cf3e25 100644 --- a/examples/websocket.rs +++ b/examples/websocket.rs @@ -2,11 +2,8 @@ //! //! Run with //! -//! ``` -//! RUST_LOG=tower_http=debug,key_value_store=trace \ -//! cargo run \ -//! --all-features \ -//! --example websocket +//! ```not_rust +//! RUST_LOG=tower_http=debug,key_value_store=trace cargo run --features=ws,headers --example websocket //! ``` use axum::{ @@ -61,17 +58,26 @@ async fn main() { async fn handle_socket( mut socket: WebSocket, // websocket handlers can also use extractors - TypedHeader(user_agent): TypedHeader, + user_agent: Option>, ) { - println!("`{}` connected", user_agent.as_str()); + if let Some(TypedHeader(user_agent)) = user_agent { + println!("`{}` connected", user_agent.as_str()); + } if let Some(msg) = socket.recv().await { - let msg = msg.unwrap(); - println!("Client says: {:?}", msg); + if let Ok(msg) = msg { + println!("Client says: {:?}", msg); + } else { + println!("client disconnected"); + return; + } } loop { - socket.send(Message::text("Hi!")).await.unwrap(); + if socket.send(Message::text("Hi!")).await.is_err() { + println!("client disconnected"); + return; + } tokio::time::sleep(std::time::Duration::from_secs(3)).await; } }