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"]
[package.metadata.playground]
features = ["ws"]
features = ["ws", "multipart", "headers"]
[[example]]
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
//!
//! ```
//! ```not_rust
//! 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
//! dependency injection using trait objects.
//!
//! Run with
//!
//! ```not_rust
//! cargo run --example error_handling_and_dependency_injection
//! ```
#![allow(dead_code)]

View file

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

View file

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

View file

@ -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::{

View file

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

View file

@ -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;

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 futures::stream::{self, Stream};
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 http::StatusCode;
use std::net::SocketAddr;

View file

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

View file

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

View file

@ -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};

View file

@ -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},

View file

@ -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;

View file

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

View file

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

View file

@ -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<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 {
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;
}
}