Make it clear how to run all examples (#92)

* Handle errors in websocket example

* Make it clear how to run all examples
This commit is contained in:
David Pedersen 2021-08-02 23:09:09 +02:00 committed by GitHub
parent 6a078ddb71
commit 9fbababc3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 117 additions and 15 deletions

View File

@ -79,8 +79,20 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"] rustdoc-args = ["--cfg", "docsrs"]
[package.metadata.playground] [package.metadata.playground]
features = ["ws"] features = ["ws", "multipart", "headers"]
[[example]] [[example]]
name = "chat" name = "chat"
required-features = ["ws"] required-features = ["ws"]
[[example]]
name = "multipart_form"
required-features = ["multipart"]
[[example]]
name = "sse"
required-features = ["headers"]
[[example]]
name = "websocket"
required-features = ["ws", "headers"]

View File

@ -2,7 +2,7 @@
//! //!
//! Run with //! Run with
//! //!
//! ``` //! ```not_rust
//! cargo run --features=ws --example chat //! cargo run --features=ws --example chat
//! ``` //! ```

View File

@ -1,5 +1,11 @@
//! Example showing how to convert errors into responses and how one might do //! Example showing how to convert errors into responses and how one might do
//! dependency injection using trait objects. //! dependency injection using trait objects.
//!
//! Run with
//!
//! ```not_rust
//! cargo run --example error_handling_and_dependency_injection
//! ```
#![allow(dead_code)] #![allow(dead_code)]

View File

@ -1,3 +1,9 @@
//! Run with
//!
//! ```not_rust
//! cargo run --example form
//! ```
use axum::prelude::*; use axum::prelude::*;
use serde::Deserialize; use serde::Deserialize;
use std::net::SocketAddr; use std::net::SocketAddr;

View File

@ -1,3 +1,9 @@
//! Run with
//!
//! ```not_rust
//! cargo run --example hello_world
//! ```
use axum::prelude::*; use axum::prelude::*;
use std::net::SocketAddr; use std::net::SocketAddr;

View File

@ -3,7 +3,7 @@
//! Run with: //! Run with:
//! //!
//! ```not_rust //! ```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::{ use axum::{

View File

@ -1,3 +1,9 @@
//! Run with
//!
//! ```not_rust
//! cargo run --example multipart_form --features=multipart
//! ```
use axum::{ use axum::{
extract::{ContentLengthLimit, Multipart}, extract::{ContentLengthLimit, Multipart},
prelude::*, prelude::*,

View File

@ -1,3 +1,9 @@
//! Run with
//!
//! ```not_rust
//! cargo run --example sessions
//! ```
use async_session::{MemoryStore, Session, SessionStore as _}; use async_session::{MemoryStore, Session, SessionStore as _};
use axum::{ use axum::{
async_trait, async_trait,
@ -6,7 +12,7 @@ use axum::{
response::IntoResponse, response::IntoResponse,
AddExtensionLayer, AddExtensionLayer,
}; };
use headers::{HeaderMap, HeaderValue}; use http::header::{HeaderMap, HeaderValue};
use http::StatusCode; use http::StatusCode;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::net::SocketAddr; use std::net::SocketAddr;

View File

@ -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 axum::{extract::TypedHeader, prelude::*, routing::nest, service::ServiceExt, sse::Event};
use futures::stream::{self, Stream}; use futures::stream::{self, Stream};
use http::StatusCode; use http::StatusCode;

View File

@ -1,3 +1,9 @@
//! Run with
//!
//! ```not_rust
//! cargo run --example static_file_server
//! ```
use axum::{prelude::*, routing::nest, service::ServiceExt}; use axum::{prelude::*, routing::nest, service::ServiceExt};
use http::StatusCode; use http::StatusCode;
use std::net::SocketAddr; use std::net::SocketAddr;

View File

@ -1,3 +1,9 @@
//! Run with
//!
//! ```not_rust
//! cargo run --example templates
//! ```
use askama::Template; use askama::Template;
use axum::{prelude::*, response::IntoResponse}; use axum::{prelude::*, response::IntoResponse};
use http::{Response, StatusCode}; use http::{Response, StatusCode};

View File

@ -1,3 +1,9 @@
//! Run with
//!
//! ```not_rust
//! cargo test --example testing
//! ```
use axum::{prelude::*, routing::BoxRoute}; use axum::{prelude::*, routing::BoxRoute};
use tower_http::trace::TraceLayer; use tower_http::trace::TraceLayer;

View File

@ -1,3 +1,9 @@
//! Run with
//!
//! ```not_rust
//! cargo run --example tls_rustls
//! ```
use axum::prelude::*; use axum::prelude::*;
use hyper::server::conn::Http; use hyper::server::conn::Http;
use std::{fs::File, io::BufReader, sync::Arc}; use std::{fs::File, io::BufReader, sync::Arc};

View File

@ -6,6 +6,12 @@
//! - `POST /todos`: create a new Todo. //! - `POST /todos`: create a new Todo.
//! - `PUT /todos/:id`: update a specific Todo. //! - `PUT /todos/:id`: update a specific Todo.
//! - `DELETE /todos/:id`: delete a specific Todo. //! - `DELETE /todos/:id`: delete a specific Todo.
//!
//! Run with
//!
//! ```not_rust
//! cargo run --example todos
//! ```
use axum::{ use axum::{
extract::{Extension, Json, Query, UrlParams}, extract::{Extension, Json, Query, UrlParams},

View File

@ -1,3 +1,9 @@
//! Run with
//!
//! ```not_rust
//! cargo run --example tokio_postgres
//! ```
use axum::{extract::Extension, prelude::*, AddExtensionLayer}; use axum::{extract::Extension, prelude::*, AddExtensionLayer};
use bb8::Pool; use bb8::Pool;
use bb8_postgres::PostgresConnectionManager; use bb8_postgres::PostgresConnectionManager;

View File

@ -1,3 +1,9 @@
//! Run with
//!
//! ```not_rust
//! cargo run --example unix_domain_socket
//! ```
use axum::{ use axum::{
extract::connect_info::{self, ConnectInfo}, extract::connect_info::{self, ConnectInfo},
prelude::*, prelude::*,

View File

@ -1,3 +1,9 @@
//! Run with
//!
//! ```not_rust
//! cargo run --example versioning
//! ```
use axum::response::IntoResponse; use axum::response::IntoResponse;
use axum::{ use axum::{
async_trait, async_trait,

View File

@ -2,11 +2,8 @@
//! //!
//! Run with //! Run with
//! //!
//! ``` //! ```not_rust
//! RUST_LOG=tower_http=debug,key_value_store=trace \ //! RUST_LOG=tower_http=debug,key_value_store=trace cargo run --features=ws,headers --example websocket
//! cargo run \
//! --all-features \
//! --example websocket
//! ``` //! ```
use axum::{ use axum::{
@ -61,17 +58,26 @@ async fn main() {
async fn handle_socket( async fn handle_socket(
mut socket: WebSocket, mut socket: WebSocket,
// websocket handlers can also use extractors // websocket handlers can also use extractors
TypedHeader(user_agent): TypedHeader<headers::UserAgent>, user_agent: Option<TypedHeader<headers::UserAgent>>,
) { ) {
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 { if let Some(msg) = socket.recv().await {
let msg = msg.unwrap(); if let Ok(msg) = msg {
println!("Client says: {:?}", msg); println!("Client says: {:?}", msg);
} else {
println!("client disconnected");
return;
}
} }
loop { 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; tokio::time::sleep(std::time::Duration::from_secs(3)).await;
} }
} }