From 6c9651c14af9d0e3fafda5f5187a7747f748003e Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Wed, 18 Aug 2021 00:49:01 +0200 Subject: [PATCH] Move all examples to their own crates (#201) This makes it much clearer which dependencies each example has. --- Cargo.toml | 45 ++----------------- examples/README.md | 25 ++--------- examples/async-graphql/Cargo.toml | 13 ++++++ examples/async-graphql/{ => src}/main.rs | 0 .../async-graphql/{ => src}/starwars/mod.rs | 0 .../async-graphql/{ => src}/starwars/model.rs | 0 examples/chat/Cargo.toml | 13 ++++++ examples/{chat.rs => chat/src/main.rs} | 23 ++++------ .../Cargo.toml | 15 +++++++ .../src/main.rs} | 16 ++++--- examples/form/Cargo.toml | 12 +++++ examples/{form.rs => form/src/main.rs} | 4 +- examples/global-404-handler/Cargo.toml | 12 +++++ .../src/main.rs} | 6 +-- examples/hello-world/Cargo.toml | 9 ++++ examples/hello-world/src/main.rs | 26 +++++++++++ examples/hello_world.rs | 32 ------------- examples/key-value-store/Cargo.toml | 13 ++++++ .../src/main.rs} | 10 ++--- examples/multipart-form/Cargo.toml | 12 +++++ .../src/main.rs} | 4 +- examples/oauth/Cargo.toml | 16 +++++++ examples/{oauth.rs => oauth/src/main.rs} | 8 ++-- examples/sessions/Cargo.toml | 14 ++++++ .../{sessions.rs => sessions/src/main.rs} | 11 +++-- examples/sse/Cargo.toml | 15 +++++++ examples/sse/{ => assets}/index.html | 0 examples/sse/{ => assets}/script.js | 0 examples/{sse.rs => sse/src/main.rs} | 23 +++++----- examples/static-file-server/Cargo.toml | 12 +++++ .../src/main.rs} | 13 ++++-- examples/templates/Cargo.toml | 12 +++++ .../{templates.rs => templates/src/main.rs} | 9 ++-- examples/templates/{ => templates}/hello.html | 0 examples/testing/Cargo.toml | 17 +++++++ examples/{testing.rs => testing/src/main.rs} | 6 +-- examples/tls-rustls/Cargo.toml | 13 ++++++ .../self_signed_certs/cert.pem | 0 .../self_signed_certs/key.pem | 0 .../{tls_rustls.rs => tls-rustls/src/main.rs} | 8 ++-- examples/todos/Cargo.toml | 15 +++++++ examples/{todos.rs => todos/src/main.rs} | 6 +-- examples/tokio-postgres/Cargo.toml | 14 ++++++ .../src/main.rs} | 6 +-- examples/tracing-aka-logging/Cargo.toml | 12 +++++ .../src/main.rs} | 7 ++- examples/unix-domain-socket/Cargo.toml | 14 ++++++ .../src/main.rs} | 5 +-- examples/versioning/Cargo.toml | 11 +++++ .../{versioning.rs => versioning/src/main.rs} | 10 ++--- examples/websockets/Cargo.toml | 13 ++++++ .../assets}/index.html | 0 .../assets}/script.js | 0 .../{websocket.rs => websockets/src/main.rs} | 8 ++-- 54 files changed, 403 insertions(+), 185 deletions(-) create mode 100644 examples/async-graphql/Cargo.toml rename examples/async-graphql/{ => src}/main.rs (100%) rename examples/async-graphql/{ => src}/starwars/mod.rs (100%) rename examples/async-graphql/{ => src}/starwars/model.rs (100%) create mode 100644 examples/chat/Cargo.toml rename examples/{chat.rs => chat/src/main.rs} (95%) create mode 100644 examples/error-handling-and-dependency-injection/Cargo.toml rename examples/{error_handling_and_dependency_injection.rs => error-handling-and-dependency-injection/src/main.rs} (93%) create mode 100644 examples/form/Cargo.toml rename examples/{form.rs => form/src/main.rs} (94%) create mode 100644 examples/global-404-handler/Cargo.toml rename examples/{404.rs => global-404-handler/src/main.rs} (89%) create mode 100644 examples/hello-world/Cargo.toml create mode 100644 examples/hello-world/src/main.rs delete mode 100644 examples/hello_world.rs create mode 100644 examples/key-value-store/Cargo.toml rename examples/{key_value_store.rs => key-value-store/src/main.rs} (94%) create mode 100644 examples/multipart-form/Cargo.toml rename examples/{multipart_form.rs => multipart-form/src/main.rs} (92%) create mode 100644 examples/oauth/Cargo.toml rename examples/{oauth.rs => oauth/src/main.rs} (96%) create mode 100644 examples/sessions/Cargo.toml rename examples/{sessions.rs => sessions/src/main.rs} (94%) create mode 100644 examples/sse/Cargo.toml rename examples/sse/{ => assets}/index.html (100%) rename examples/sse/{ => assets}/script.js (100%) rename examples/{sse.rs => sse/src/main.rs} (71%) create mode 100644 examples/static-file-server/Cargo.toml rename examples/{static_file_server.rs => static-file-server/src/main.rs} (78%) create mode 100644 examples/templates/Cargo.toml rename examples/{templates.rs => templates/src/main.rs} (90%) rename examples/templates/{ => templates}/hello.html (100%) create mode 100644 examples/testing/Cargo.toml rename examples/{testing.rs => testing/src/main.rs} (96%) create mode 100644 examples/tls-rustls/Cargo.toml rename examples/{ => tls-rustls}/self_signed_certs/cert.pem (100%) rename examples/{ => tls-rustls}/self_signed_certs/key.pem (100%) rename examples/{tls_rustls.rs => tls-rustls/src/main.rs} (88%) create mode 100644 examples/todos/Cargo.toml rename examples/{todos.rs => todos/src/main.rs} (96%) create mode 100644 examples/tokio-postgres/Cargo.toml rename examples/{tokio_postgres.rs => tokio-postgres/src/main.rs} (95%) create mode 100644 examples/tracing-aka-logging/Cargo.toml rename examples/{tracing_aka_logging.rs => tracing-aka-logging/src/main.rs} (85%) create mode 100644 examples/unix-domain-socket/Cargo.toml rename examples/{unix_domain_socket.rs => unix-domain-socket/src/main.rs} (97%) create mode 100644 examples/versioning/Cargo.toml rename examples/{versioning.rs => versioning/src/main.rs} (90%) create mode 100644 examples/websockets/Cargo.toml rename examples/{websocket => websockets/assets}/index.html (100%) rename examples/{websocket => websockets/assets}/script.js (100%) rename examples/{websocket.rs => websockets/src/main.rs} (90%) diff --git a/Cargo.toml b/Cargo.toml index 853a3d7b..4a54b2ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,9 @@ readme = "README.md" repository = "https://github.com/tokio-rs/axum" version = "0.1.3" +[workspace] +members = ["examples/*"] + [features] default = [] ws = ["tokio-tungstenite", "sha-1", "base64"] @@ -44,28 +47,10 @@ multer = { optional = true, version = "2.0.0" } mime = { optional = true, version = "0.3" } [dev-dependencies] -askama = "0.10.5" -bb8 = "0.7.1" -bb8-postgres = "0.7.0" futures = "0.3" -hyper = { version = "0.14", features = ["full"] } reqwest = { version = "0.11", features = ["json", "stream"] } serde = { version = "1.0", features = ["derive"] } tokio = { version = "1.6.1", features = ["macros", "rt", "rt-multi-thread", "net"] } -tokio-rustls = "0.22.0" -tokio-postgres = "0.7.2" -tracing = "0.1" -tracing-subscriber = "0.2" -uuid = { version = "0.8", features = ["serde", "v4"] } -async-session = "3.0.0" -tokio-stream = "0.1.7" - -# for oauth example -oauth2 = "4.1" - -# for async-graphql example -async-graphql = "2.9.9" -slab = "0.4.3" [dev-dependencies.tower] version = "0.4" @@ -88,27 +73,3 @@ rustdoc-args = ["--cfg", "docsrs"] [package.metadata.playground] features = ["ws", "multipart", "headers"] - -[[example]] -name = "chat" -required-features = ["ws"] - -[[example]] -name = "multipart_form" -required-features = ["multipart"] - -[[example]] -name = "sse" -required-features = ["headers"] - -[[example]] -name = "websocket" -required-features = ["ws", "headers"] - -[[example]] -name = "async-graphql" -required-features = ["headers"] - -[[example]] -name = "oauth" -required-features = ["headers"] diff --git a/examples/README.md b/examples/README.md index 3f35a6fb..4ab27b33 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,27 +1,8 @@ # Examples -- [`hello_world`](../examples/hello_world.rs) - Very small getting started app. -- [`todos`](../examples/todos.rs) - Provides a RESTful web server managing some Todos. -- [`key_value_store`](../examples/key_value_store.rs) - Slightly larger app with an in-memory key/value store. -- [`form`](../examples/form.rs) - Receiving data from an HTML `
`. -- [`multipart_form`](../examples/multipart_form.rs) - How to parse `multipart/form-data` forms. -- [`static_file_server`](../examples/static_file_server.rs) - Serving static files from a directory. Could for example be the baseline for a single page app. -- [`templates`](../examples/templates.rs) - Rending HTML templates using [askama](https://crates.io/crates/askama). -- [`testing`](../examples/testing.rs) - How to test axum apps. -- [`versioning`](../examples/versioning.rs) - How one might version an API. -- [`websocket`](../examples/websocket.rs) - How to build an app that handles WebSocket connections. -- [`error_handling_and_dependency_injection`](../examples/error_handling_and_dependency_injection.rs) - How to handle errors and dependency injection using trait objects. -- [`tokio_postgres`](../examples/tokio_postgres.rs) - How to use a tokio-postgres and bb8 to query a database. -- [`unix_domain_socket`](../examples/unix_domain_socket.rs) - How to run an Axum server over unix domain sockets. -- [`sessions`](../examples/sessions.rs) - Sessions and cookies using [`async-session`](https://crates.io/crates/async-session). -- [`tls_rustls`](../examples/tls_rustls.rs) - TLS with [`tokio-rustls`](https://crates.io/crates/tokio-rustls). -- [`chat`](../examples/chat.rs) - Chat application example. -- [`404`](../examples/404.rs) - Custom 404 page. -- [`async-graphql`](../examples/async-graphql) - GraphQL example using [`async-graphql`](https://crates.io/crates/async-graphql). -- [`tracing_aka_logging`](../examples/tracing_aka_logging.rs) - How to setup and configure tracing/logging. -- [`oauth`](../examples/oauth.rs) - Implementing authentication using [`oauth2`](https://crates.io/crates/oauth2) and [`async-session`](https://crates.io/crates/async-session). +This folder contains numerous example showing how to use axum. Each example is +setup as its own crate so its dependencies are clear. - -# Community showcase +## Community showcase - [Houseflow](https://github.com/gbaranski/houseflow): House automation platform written in Rust. diff --git a/examples/async-graphql/Cargo.toml b/examples/async-graphql/Cargo.toml new file mode 100644 index 00000000..f652ece3 --- /dev/null +++ b/examples/async-graphql/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "example-async-graphql" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../.." } +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = "0.2" +async-graphql = "2.9.9" +slab = "0.4.3" diff --git a/examples/async-graphql/main.rs b/examples/async-graphql/src/main.rs similarity index 100% rename from examples/async-graphql/main.rs rename to examples/async-graphql/src/main.rs diff --git a/examples/async-graphql/starwars/mod.rs b/examples/async-graphql/src/starwars/mod.rs similarity index 100% rename from examples/async-graphql/starwars/mod.rs rename to examples/async-graphql/src/starwars/mod.rs diff --git a/examples/async-graphql/starwars/model.rs b/examples/async-graphql/src/starwars/model.rs similarity index 100% rename from examples/async-graphql/starwars/model.rs rename to examples/async-graphql/src/starwars/model.rs diff --git a/examples/chat/Cargo.toml b/examples/chat/Cargo.toml new file mode 100644 index 00000000..ffaf1002 --- /dev/null +++ b/examples/chat/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "example-chat" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../..", features = ["ws"] } +tokio = { version = "1.0", features = ["full"] } +tower = { version = "0.4", features = ["util"] } +tracing = "0.1" +tracing-subscriber = "0.2" +futures = "0.3" diff --git a/examples/chat.rs b/examples/chat/src/main.rs similarity index 95% rename from examples/chat.rs rename to examples/chat/src/main.rs index a0440685..da06f578 100644 --- a/examples/chat.rs +++ b/examples/chat/src/main.rs @@ -3,26 +3,21 @@ //! Run with //! //! ```not_rust -//! cargo run --features=ws --example chat +//! cargo run -p example-chat //! ``` -use std::collections::HashSet; -use std::net::SocketAddr; -use std::sync::{Arc, Mutex}; - -use futures::{sink::SinkExt, stream::StreamExt}; - -use tokio::sync::broadcast; - -use axum::extract::{ - ws::{Message, WebSocket, WebSocketUpgrade}, - Extension, -}; +use axum::extract::ws::{Message, WebSocket, WebSocketUpgrade}; +use axum::extract::Extension; use axum::handler::get; use axum::response::{Html, IntoResponse}; use axum::route; use axum::routing::RoutingDsl; use axum::AddExtensionLayer; +use futures::{sink::SinkExt, stream::StreamExt}; +use std::collections::HashSet; +use std::net::SocketAddr; +use std::sync::{Arc, Mutex}; +use tokio::sync::broadcast; // Our shared state struct AppState { @@ -138,5 +133,5 @@ fn check_username(state: &AppState, string: &mut String, name: &str) { // Include utf-8 file at **compile** time. async fn index() -> Html<&'static str> { - Html(std::include_str!("chat/chat.html")) + Html(std::include_str!("../chat.html")) } diff --git a/examples/error-handling-and-dependency-injection/Cargo.toml b/examples/error-handling-and-dependency-injection/Cargo.toml new file mode 100644 index 00000000..2e4aba38 --- /dev/null +++ b/examples/error-handling-and-dependency-injection/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "example-error-handling-and-dependency-injection" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../.." } +tokio = { version = "1.0", features = ["full"] } +tower = { version = "0.4", features = ["util"] } +tracing = "0.1" +tracing-subscriber = "0.2" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +uuid = { version = "0.8", features = ["v4", "serde"] } diff --git a/examples/error_handling_and_dependency_injection.rs b/examples/error-handling-and-dependency-injection/src/main.rs similarity index 93% rename from examples/error_handling_and_dependency_injection.rs rename to examples/error-handling-and-dependency-injection/src/main.rs index 0b1b3fef..d4dc4c1d 100644 --- a/examples/error_handling_and_dependency_injection.rs +++ b/examples/error-handling-and-dependency-injection/src/main.rs @@ -4,23 +4,20 @@ //! Run with //! //! ```not_rust -//! cargo run --example error_handling_and_dependency_injection +//! cargo run -p example-error-handling-and-dependency-injection //! ``` -#![allow(dead_code)] - use axum::{ async_trait, + body::{Bytes, Full}, extract::{Extension, Path}, handler::{get, post}, + http::{Response, StatusCode}, response::IntoResponse, route, routing::RoutingDsl, AddExtensionLayer, Json, }; -use bytes::Bytes; -use http::{Response, StatusCode}; -use http_body::Full; use serde::{Deserialize, Serialize}; use serde_json::json; use std::{convert::Infallible, net::SocketAddr, sync::Arc}; @@ -30,7 +27,10 @@ use uuid::Uuid; async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "error_handling_and_dependency_injection=debug") + std::env::set_var( + "RUST_LOG", + "example_error_handling_and_dependency_injection=debug", + ) } tracing_subscriber::fmt::init(); @@ -158,6 +158,8 @@ struct CreateUser { /// Errors that can happen when using the user repo. #[derive(Debug)] enum UserRepoError { + #[allow(dead_code)] NotFound, + #[allow(dead_code)] InvalidUsername, } diff --git a/examples/form/Cargo.toml b/examples/form/Cargo.toml new file mode 100644 index 00000000..bb59f213 --- /dev/null +++ b/examples/form/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "example-form" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../.." } +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = "0.2" +serde = { version = "1.0", features = ["derive"] } diff --git a/examples/form.rs b/examples/form/src/main.rs similarity index 94% rename from examples/form.rs rename to examples/form/src/main.rs index 79f63681..f35db2a3 100644 --- a/examples/form.rs +++ b/examples/form/src/main.rs @@ -1,7 +1,7 @@ //! Run with //! //! ```not_rust -//! cargo run --example form +//! cargo run -p example-form //! ``` use axum::{extract::Form, handler::get, response::Html, route, routing::RoutingDsl}; @@ -12,7 +12,7 @@ use std::net::SocketAddr; async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "form=debug") + std::env::set_var("RUST_LOG", "example_form=debug") } tracing_subscriber::fmt::init(); diff --git a/examples/global-404-handler/Cargo.toml b/examples/global-404-handler/Cargo.toml new file mode 100644 index 00000000..d4165388 --- /dev/null +++ b/examples/global-404-handler/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "example-global-404-handler" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../.." } +tokio = { version = "1.0", features = ["full"] } +tower = { version = "0.4", features = ["util"] } +tracing = "0.1" +tracing-subscriber = "0.2" diff --git a/examples/404.rs b/examples/global-404-handler/src/main.rs similarity index 89% rename from examples/404.rs rename to examples/global-404-handler/src/main.rs index 7d2ba9a0..3f94a803 100644 --- a/examples/404.rs +++ b/examples/global-404-handler/src/main.rs @@ -1,17 +1,17 @@ //! Run with //! //! ```not_rust -//! cargo run --example 404 +//! cargo run -p example-global-404-handler //! ``` use axum::{ body::{box_body, Body, BoxBody}, handler::get, + http::{Response, StatusCode}, response::Html, route, routing::RoutingDsl, }; -use http::{Response, StatusCode}; use std::net::SocketAddr; use tower::util::MapResponseLayer; @@ -19,7 +19,7 @@ use tower::util::MapResponseLayer; async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "404=debug") + std::env::set_var("RUST_LOG", "example_global_404_handler=debug") } tracing_subscriber::fmt::init(); diff --git a/examples/hello-world/Cargo.toml b/examples/hello-world/Cargo.toml new file mode 100644 index 00000000..ab8f44dd --- /dev/null +++ b/examples/hello-world/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "example-hello-world" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../.." } +tokio = { version = "1.0", features = ["full"] } diff --git a/examples/hello-world/src/main.rs b/examples/hello-world/src/main.rs new file mode 100644 index 00000000..5bb9d567 --- /dev/null +++ b/examples/hello-world/src/main.rs @@ -0,0 +1,26 @@ +//! Run with +//! +//! ```not_rust +//! cargo run -p example-hello-world +//! ``` + +use axum::{handler::get, route, routing::RoutingDsl}; +use std::net::SocketAddr; + +#[tokio::main] +async fn main() { + // build our application with a route + let app = route("/foo", get(handler)); + + // run it + let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + println!("listening on {}", addr); + axum::Server::bind(&addr) + .serve(app.into_make_service()) + .await + .unwrap(); +} + +async fn handler() -> &'static str { + "

Hello, World!

" +} diff --git a/examples/hello_world.rs b/examples/hello_world.rs deleted file mode 100644 index a1050c3f..00000000 --- a/examples/hello_world.rs +++ /dev/null @@ -1,32 +0,0 @@ -//! Run with -//! -//! ```not_rust -//! cargo run --example hello_world -//! ``` - -use axum::{handler::get, response::Html, route, routing::RoutingDsl}; -use std::net::SocketAddr; - -#[tokio::main] -async fn main() { - // Set the RUST_LOG, if it hasn't been explicitly defined - if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "hello_world=debug") - } - tracing_subscriber::fmt::init(); - - // build our application with a route - let app = route("/foo", get(handler)).or(route("/bar", get(handler))); - - // run it - let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); - tracing::debug!("listening on {}", addr); - axum::Server::bind(&addr) - .serve(app.into_make_service()) - .await - .unwrap(); -} - -async fn handler() -> Html<&'static str> { - Html("

Hello, World!

") -} diff --git a/examples/key-value-store/Cargo.toml b/examples/key-value-store/Cargo.toml new file mode 100644 index 00000000..0f936de5 --- /dev/null +++ b/examples/key-value-store/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "example-key-value-store" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../.." } +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = "0.2" +tower = { version = "0.4", features = ["util", "timeout", "load-shed", "limit"] } +tower-http = { version = "0.1", features = ["add-extension", "auth", "compression-full", "trace" ] } diff --git a/examples/key_value_store.rs b/examples/key-value-store/src/main.rs similarity index 94% rename from examples/key_value_store.rs rename to examples/key-value-store/src/main.rs index 5c5b2411..debf3bc2 100644 --- a/examples/key_value_store.rs +++ b/examples/key-value-store/src/main.rs @@ -3,18 +3,18 @@ //! Run with: //! //! ```not_rust -//! cargo run --example key_value_store +//! cargo run -p example-key-value-store //! ``` use axum::{ + body::{Body, Bytes}, extract::{ContentLengthLimit, Extension, Path}, handler::{delete, get, Handler}, + http::StatusCode, response::IntoResponse, route, routing::{BoxRoute, RoutingDsl}, }; -use bytes::Bytes; -use http::StatusCode; use std::{ borrow::Cow, collections::HashMap, @@ -33,7 +33,7 @@ use tower_http::{ async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "key_value_store=debug,tower_http=debug") + std::env::set_var("RUST_LOG", "example_key_value_store=debug,tower_http=debug") } tracing_subscriber::fmt::init(); @@ -108,7 +108,7 @@ async fn list_keys(Extension(state): Extension) -> String { .join("\n") } -fn admin_routes() -> BoxRoute { +fn admin_routes() -> BoxRoute { async fn delete_all_keys(Extension(state): Extension) { state.write().unwrap().db.clear(); } diff --git a/examples/multipart-form/Cargo.toml b/examples/multipart-form/Cargo.toml new file mode 100644 index 00000000..5716047d --- /dev/null +++ b/examples/multipart-form/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "example-multipart-form" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../..", features = ["multipart"] } +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = "0.2" +tower-http = { version = "0.1", features = ["trace" ] } diff --git a/examples/multipart_form.rs b/examples/multipart-form/src/main.rs similarity index 92% rename from examples/multipart_form.rs rename to examples/multipart-form/src/main.rs index a2d94676..fd5f75ca 100644 --- a/examples/multipart_form.rs +++ b/examples/multipart-form/src/main.rs @@ -1,7 +1,7 @@ //! Run with //! //! ```not_rust -//! cargo run --example multipart_form --features=multipart +//! cargo run -p example-multipart-form //! ``` use axum::{ @@ -17,7 +17,7 @@ use std::net::SocketAddr; async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "multipart_form=debug,tower_http=debug") + std::env::set_var("RUST_LOG", "example_multipart_form=debug,tower_http=debug") } tracing_subscriber::fmt::init(); diff --git a/examples/oauth/Cargo.toml b/examples/oauth/Cargo.toml new file mode 100644 index 00000000..44e68cd4 --- /dev/null +++ b/examples/oauth/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "example-oauth" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../..", features = ["headers"] } +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = "0.2" +oauth2 = "4.1" +async-session = "3.0.0" +serde = { version = "1.0", features = ["derive"] } +reqwest = { version = "0.11", features = ["json"] } +headers = "0.3" diff --git a/examples/oauth.rs b/examples/oauth/src/main.rs similarity index 96% rename from examples/oauth.rs rename to examples/oauth/src/main.rs index 9ab8aa2a..a226192c 100644 --- a/examples/oauth.rs +++ b/examples/oauth/src/main.rs @@ -3,7 +3,7 @@ //! Run with //! //! ```not_rust -//! CLIENT_ID=123 CLIENT_SECRET=secret cargo run --example oauth --features=headers +//! CLIENT_ID=123 CLIENT_SECRET=secret cargo run -p example-oauth //! ``` use async_session::{MemoryStore, Session, SessionStore}; @@ -12,12 +12,12 @@ use axum::{ body::{Bytes, Empty}, extract::{Extension, FromRequest, Query, RequestParts, TypedHeader}, handler::get, + http::{header::SET_COOKIE, HeaderMap, Response}, response::{IntoResponse, Redirect}, route, routing::RoutingDsl, AddExtensionLayer, }; -use http::{header::SET_COOKIE, HeaderMap}; use oauth2::{ basic::BasicClient, reqwest::async_http_client, AuthUrl, AuthorizationCode, ClientId, ClientSecret, CsrfToken, RedirectUrl, Scope, TokenResponse, TokenUrl, @@ -38,7 +38,7 @@ static COOKIE_NAME: &str = "SESSION"; async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "hello_world=debug") + std::env::set_var("RUST_LOG", "example_oauth=debug") } tracing_subscriber::fmt::init(); @@ -200,7 +200,7 @@ impl IntoResponse for AuthRedirect { type Body = Empty; type BodyError = ::Error; - fn into_response(self) -> http::Response { + fn into_response(self) -> Response { Redirect::found("/auth/discord".parse().unwrap()).into_response() } } diff --git a/examples/sessions/Cargo.toml b/examples/sessions/Cargo.toml new file mode 100644 index 00000000..967306a4 --- /dev/null +++ b/examples/sessions/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "example-sessions" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../.." } +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = "0.2" +serde = { version = "1.0", features = ["derive"] } +uuid = { version = "0.8", features = ["v4", "serde"] } +async-session = "3.0.0" diff --git a/examples/sessions.rs b/examples/sessions/src/main.rs similarity index 94% rename from examples/sessions.rs rename to examples/sessions/src/main.rs index ad909060..638b91c5 100644 --- a/examples/sessions.rs +++ b/examples/sessions/src/main.rs @@ -1,7 +1,7 @@ //! Run with //! //! ```not_rust -//! cargo run --example sessions +//! cargo run -p example-sessions //! ``` use async_session::{MemoryStore, Session, SessionStore as _}; @@ -9,13 +9,16 @@ use axum::{ async_trait, extract::{Extension, FromRequest, RequestParts}, handler::get, + http::{ + self, + header::{HeaderMap, HeaderValue}, + StatusCode, + }, response::IntoResponse, route, routing::RoutingDsl, AddExtensionLayer, }; -use http::header::{HeaderMap, HeaderValue}; -use http::StatusCode; use serde::{Deserialize, Serialize}; use std::net::SocketAddr; use uuid::Uuid; @@ -24,7 +27,7 @@ use uuid::Uuid; async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "sessions=debug") + std::env::set_var("RUST_LOG", "example_sessions=debug") } tracing_subscriber::fmt::init(); diff --git a/examples/sse/Cargo.toml b/examples/sse/Cargo.toml new file mode 100644 index 00000000..48c0996d --- /dev/null +++ b/examples/sse/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "example-sse" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../..", features = ["headers"] } +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = "0.2" +tower-http = { version = "0.1", features = ["fs", "trace"] } +futures = "0.3" +tokio-stream = "0.1" +headers = "0.3" diff --git a/examples/sse/index.html b/examples/sse/assets/index.html similarity index 100% rename from examples/sse/index.html rename to examples/sse/assets/index.html diff --git a/examples/sse/script.js b/examples/sse/assets/script.js similarity index 100% rename from examples/sse/script.js rename to examples/sse/assets/script.js diff --git a/examples/sse.rs b/examples/sse/src/main.rs similarity index 71% rename from examples/sse.rs rename to examples/sse/src/main.rs index c40ade3d..9b5f9ff4 100644 --- a/examples/sse.rs +++ b/examples/sse/src/main.rs @@ -1,17 +1,17 @@ //! Run with //! //! ```not_rust -//! cargo run --example sse --features=headers +//! cargo run -p example-sse //! ``` use axum::{ extract::TypedHeader, handler::get, + http::StatusCode, response::sse::{sse, Event, Sse}, routing::{nest, RoutingDsl}, }; use futures::stream::{self, Stream}; -use http::StatusCode; use std::{convert::Infallible, net::SocketAddr, time::Duration}; use tokio_stream::StreamExt as _; use tower_http::{services::ServeDir, trace::TraceLayer}; @@ -20,18 +20,19 @@ use tower_http::{services::ServeDir, trace::TraceLayer}; async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "sse=debug,tower_http=debug") + std::env::set_var("RUST_LOG", "example_sse=debug,tower_http=debug") } tracing_subscriber::fmt::init(); - let static_files_service = - axum::service::get(ServeDir::new("examples/sse").append_index_html_on_directories(true)) - .handle_error(|error: std::io::Error| { - Ok::<_, std::convert::Infallible>(( - StatusCode::INTERNAL_SERVER_ERROR, - format!("Unhandled internal error: {}", error), - )) - }); + let static_files_service = axum::service::get( + ServeDir::new("examples/sse/assets").append_index_html_on_directories(true), + ) + .handle_error(|error: std::io::Error| { + Ok::<_, std::convert::Infallible>(( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Unhandled internal error: {}", error), + )) + }); // build our application with a route let app = nest("/", static_files_service) diff --git a/examples/static-file-server/Cargo.toml b/examples/static-file-server/Cargo.toml new file mode 100644 index 00000000..b9175002 --- /dev/null +++ b/examples/static-file-server/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "example-static-file-server" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../.." } +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = "0.2" +tower-http = { version = "0.1", features = ["fs", "trace"] } diff --git a/examples/static_file_server.rs b/examples/static-file-server/src/main.rs similarity index 78% rename from examples/static_file_server.rs rename to examples/static-file-server/src/main.rs index 3d4b5a8f..069ffd5f 100644 --- a/examples/static_file_server.rs +++ b/examples/static-file-server/src/main.rs @@ -1,11 +1,13 @@ //! Run with //! //! ```not_rust -//! cargo run --example static_file_server +//! cargo run -p example-static-file-server //! ``` -use axum::routing::{nest, RoutingDsl}; -use http::StatusCode; +use axum::{ + http::StatusCode, + routing::{nest, RoutingDsl}, +}; use std::net::SocketAddr; use tower_http::{services::ServeDir, trace::TraceLayer}; @@ -13,7 +15,10 @@ use tower_http::{services::ServeDir, trace::TraceLayer}; async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "static_file_server=debug,tower_http=debug") + std::env::set_var( + "RUST_LOG", + "example_static_file_server=debug,tower_http=debug", + ) } tracing_subscriber::fmt::init(); diff --git a/examples/templates/Cargo.toml b/examples/templates/Cargo.toml new file mode 100644 index 00000000..332faaef --- /dev/null +++ b/examples/templates/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "example-templates" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../.." } +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = "0.2" +askama = "0.10" diff --git a/examples/templates.rs b/examples/templates/src/main.rs similarity index 90% rename from examples/templates.rs rename to examples/templates/src/main.rs index 4d6d3bdc..707b67a3 100644 --- a/examples/templates.rs +++ b/examples/templates/src/main.rs @@ -1,27 +1,26 @@ //! Run with //! //! ```not_rust -//! cargo run --example templates +//! cargo run -p example-templates //! ``` use askama::Template; use axum::{ + body::{Bytes, Full}, extract, handler::get, + http::{Response, StatusCode}, response::{Html, IntoResponse}, route, routing::RoutingDsl, }; -use bytes::Bytes; -use http::{Response, StatusCode}; -use http_body::Full; use std::{convert::Infallible, net::SocketAddr}; #[tokio::main] async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "templates=debug") + std::env::set_var("RUST_LOG", "example_templates=debug") } tracing_subscriber::fmt::init(); diff --git a/examples/templates/hello.html b/examples/templates/templates/hello.html similarity index 100% rename from examples/templates/hello.html rename to examples/templates/templates/hello.html diff --git a/examples/testing/Cargo.toml b/examples/testing/Cargo.toml new file mode 100644 index 00000000..7ed1a252 --- /dev/null +++ b/examples/testing/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "example-testing" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../.." } +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = "0.2" +tower-http = { version = "0.1", features = ["trace"] } +serde_json = "1.0" +hyper = { version = "0.14", features = ["full"] } + +[dev-dependencies] +tower = { version = "0.4", features = ["util"] } diff --git a/examples/testing.rs b/examples/testing/src/main.rs similarity index 96% rename from examples/testing.rs rename to examples/testing/src/main.rs index 830b4fdf..05c567cc 100644 --- a/examples/testing.rs +++ b/examples/testing/src/main.rs @@ -1,7 +1,7 @@ //! Run with //! //! ```not_rust -//! cargo test --example testing +//! cargo test -p example-testing //! ``` use axum::{ @@ -17,7 +17,7 @@ use tower_http::trace::TraceLayer; async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "testing=debug,tower_http=debug") + std::env::set_var("RUST_LOG", "example_testing=debug,tower_http=debug") } tracing_subscriber::fmt::init(); @@ -50,7 +50,7 @@ fn app() -> BoxRoute { #[cfg(test)] mod tests { use super::*; - use http::{Request, StatusCode}; + use axum::http::{self, Request, StatusCode}; use serde_json::{json, Value}; use std::net::{SocketAddr, TcpListener}; use tower::ServiceExt; // for `app.oneshot()` diff --git a/examples/tls-rustls/Cargo.toml b/examples/tls-rustls/Cargo.toml new file mode 100644 index 00000000..e0a9353e --- /dev/null +++ b/examples/tls-rustls/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "example-tls-rustls" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../.." } +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = "0.2" +tokio-rustls = "0.22.0" +hyper = { version = "0.14", features = ["full"] } diff --git a/examples/self_signed_certs/cert.pem b/examples/tls-rustls/self_signed_certs/cert.pem similarity index 100% rename from examples/self_signed_certs/cert.pem rename to examples/tls-rustls/self_signed_certs/cert.pem diff --git a/examples/self_signed_certs/key.pem b/examples/tls-rustls/self_signed_certs/key.pem similarity index 100% rename from examples/self_signed_certs/key.pem rename to examples/tls-rustls/self_signed_certs/key.pem diff --git a/examples/tls_rustls.rs b/examples/tls-rustls/src/main.rs similarity index 88% rename from examples/tls_rustls.rs rename to examples/tls-rustls/src/main.rs index 3ea81e3b..b7a4b035 100644 --- a/examples/tls_rustls.rs +++ b/examples/tls-rustls/src/main.rs @@ -1,7 +1,7 @@ //! Run with //! //! ```not_rust -//! cargo run --example tls_rustls +//! cargo run -p example-tls-rustls //! ``` use axum::{handler::get, route}; @@ -19,13 +19,13 @@ use tokio_rustls::{ async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "rustls=debug") + std::env::set_var("RUST_LOG", "example_tls_rustls=debug") } tracing_subscriber::fmt::init(); let rustls_config = rustls_server_config( - "examples/self_signed_certs/key.pem", - "examples/self_signed_certs/cert.pem", + "examples/tls-rustls/self_signed_certs/key.pem", + "examples/tls-rustls/self_signed_certs/cert.pem", ); let acceptor = TlsAcceptor::from(rustls_config); diff --git a/examples/todos/Cargo.toml b/examples/todos/Cargo.toml new file mode 100644 index 00000000..c12acae9 --- /dev/null +++ b/examples/todos/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "example-todos" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../.." } +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = "0.2" +tower = { version = "0.4", features = ["util", "timeout"] } +tower-http = { version = "0.1", features = ["add-extension", "trace"] } +uuid = { version = "0.8", features = ["serde", "v4"] } +serde = { version = "1.0", features = ["derive"] } diff --git a/examples/todos.rs b/examples/todos/src/main.rs similarity index 96% rename from examples/todos.rs rename to examples/todos/src/main.rs index 700fb5ee..828828ed 100644 --- a/examples/todos.rs +++ b/examples/todos/src/main.rs @@ -10,18 +10,18 @@ //! Run with //! //! ```not_rust -//! cargo run --example todos +//! cargo run -p example-todos //! ``` use axum::{ extract::{Extension, Path, Query}, handler::{get, patch}, + http::StatusCode, response::IntoResponse, route, routing::RoutingDsl, Json, }; -use http::StatusCode; use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, @@ -38,7 +38,7 @@ use uuid::Uuid; async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "todos=debug,tower_http=debug") + std::env::set_var("RUST_LOG", "example_todos=debug,tower_http=debug") } tracing_subscriber::fmt::init(); diff --git a/examples/tokio-postgres/Cargo.toml b/examples/tokio-postgres/Cargo.toml new file mode 100644 index 00000000..b70476e9 --- /dev/null +++ b/examples/tokio-postgres/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "example-tokio-postgres" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../.." } +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = "0.2" +bb8 = "0.7.1" +bb8-postgres = "0.7.0" +tokio-postgres = "0.7.2" diff --git a/examples/tokio_postgres.rs b/examples/tokio-postgres/src/main.rs similarity index 95% rename from examples/tokio_postgres.rs rename to examples/tokio-postgres/src/main.rs index 09c119ba..27271776 100644 --- a/examples/tokio_postgres.rs +++ b/examples/tokio-postgres/src/main.rs @@ -1,20 +1,20 @@ //! Run with //! //! ```not_rust -//! cargo run --example tokio_postgres +//! cargo run -p example-tokio-postgres //! ``` use axum::{ async_trait, extract::{Extension, FromRequest, RequestParts}, handler::get, + http::StatusCode, route, routing::RoutingDsl, AddExtensionLayer, }; use bb8::{Pool, PooledConnection}; use bb8_postgres::PostgresConnectionManager; -use http::StatusCode; use std::net::SocketAddr; use tokio_postgres::NoTls; @@ -22,7 +22,7 @@ use tokio_postgres::NoTls; async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "tokio_postgres=debug") + std::env::set_var("RUST_LOG", "example_tokio_postgres=debug") } tracing_subscriber::fmt::init(); diff --git a/examples/tracing-aka-logging/Cargo.toml b/examples/tracing-aka-logging/Cargo.toml new file mode 100644 index 00000000..b2811c0f --- /dev/null +++ b/examples/tracing-aka-logging/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "example-tracing-aka-logging" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../.." } +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = "0.2" +tower-http = { version = "0.1", features = ["trace"] } diff --git a/examples/tracing_aka_logging.rs b/examples/tracing-aka-logging/src/main.rs similarity index 85% rename from examples/tracing_aka_logging.rs rename to examples/tracing-aka-logging/src/main.rs index 546c93af..e509565a 100644 --- a/examples/tracing_aka_logging.rs +++ b/examples/tracing-aka-logging/src/main.rs @@ -1,7 +1,7 @@ //! Run with //! //! ```not_rust -//! cargo run --example tracing_aka_logging +//! cargo run -p example-tracing-aka-logging //! ``` use axum::{handler::get, response::Html, route, routing::RoutingDsl}; @@ -12,7 +12,10 @@ use tower_http::trace::TraceLayer; async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "tracing_aka_logging=debug,tower_http=debug") + std::env::set_var( + "RUST_LOG", + "example_tracing_aka_logging=debug,tower_http=debug", + ) } tracing_subscriber::fmt::init(); diff --git a/examples/unix-domain-socket/Cargo.toml b/examples/unix-domain-socket/Cargo.toml new file mode 100644 index 00000000..45efd623 --- /dev/null +++ b/examples/unix-domain-socket/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "example-unix-domain-socket" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../.." } +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = "0.2" +hyper = { version = "0.14", features = ["full"] } +tower = { version = "0.4", features = ["util"] } +futures = "0.3" diff --git a/examples/unix_domain_socket.rs b/examples/unix-domain-socket/src/main.rs similarity index 97% rename from examples/unix_domain_socket.rs rename to examples/unix-domain-socket/src/main.rs index 6cd0dbc0..c0730af0 100644 --- a/examples/unix_domain_socket.rs +++ b/examples/unix-domain-socket/src/main.rs @@ -1,19 +1,18 @@ //! Run with //! //! ```not_rust -//! cargo run --example unix_domain_socket +//! cargo run -p example-unix-domain-socket //! ``` use axum::{ body::Body, extract::connect_info::{self, ConnectInfo}, handler::get, - http::Request, + http::{Method, Request, StatusCode, Uri}, route, routing::RoutingDsl, }; use futures::ready; -use http::{Method, StatusCode, Uri}; use hyper::{ client::connect::{Connected, Connection}, server::accept::Accept, diff --git a/examples/versioning/Cargo.toml b/examples/versioning/Cargo.toml new file mode 100644 index 00000000..389416d3 --- /dev/null +++ b/examples/versioning/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "example-versioning" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../.." } +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = "0.2" diff --git a/examples/versioning.rs b/examples/versioning/src/main.rs similarity index 90% rename from examples/versioning.rs rename to examples/versioning/src/main.rs index 4a591869..3ca175b2 100644 --- a/examples/versioning.rs +++ b/examples/versioning/src/main.rs @@ -1,21 +1,19 @@ //! Run with //! //! ```not_rust -//! cargo run --example versioning +//! cargo run -p example-versioning //! ``` use axum::{ async_trait, + body::{Bytes, Full}, extract::{FromRequest, Path, RequestParts}, handler::get, + http::{Response, StatusCode}, response::IntoResponse, route, routing::RoutingDsl, }; -use bytes::Bytes; -use http::Response; -use http::StatusCode; -use http_body::Full; use std::collections::HashMap; use std::net::SocketAddr; @@ -23,7 +21,7 @@ use std::net::SocketAddr; async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "versioning=debug") + std::env::set_var("RUST_LOG", "example_versioning=debug") } tracing_subscriber::fmt::init(); diff --git a/examples/websockets/Cargo.toml b/examples/websockets/Cargo.toml new file mode 100644 index 00000000..08f51e4d --- /dev/null +++ b/examples/websockets/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "example-websockets" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +axum = { path = "../..", features = ["ws", "headers"] } +tokio = { version = "1.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = "0.2" +tower-http = { version = "0.1", features = ["fs", "trace"] } +headers = "0.3" diff --git a/examples/websocket/index.html b/examples/websockets/assets/index.html similarity index 100% rename from examples/websocket/index.html rename to examples/websockets/assets/index.html diff --git a/examples/websocket/script.js b/examples/websockets/assets/script.js similarity index 100% rename from examples/websocket/script.js rename to examples/websockets/assets/script.js diff --git a/examples/websocket.rs b/examples/websockets/src/main.rs similarity index 90% rename from examples/websocket.rs rename to examples/websockets/src/main.rs index a41aaf38..938adec2 100644 --- a/examples/websocket.rs +++ b/examples/websockets/src/main.rs @@ -3,7 +3,7 @@ //! Run with //! //! ```not_rust -//! cargo run --features=ws,headers --example websocket +//! cargo run -p example-websockets //! ``` use axum::{ @@ -12,10 +12,10 @@ use axum::{ TypedHeader, }, handler::get, + http::StatusCode, response::IntoResponse, routing::{nest, RoutingDsl}, }; -use http::StatusCode; use std::net::SocketAddr; use tower_http::{ services::ServeDir, @@ -26,7 +26,7 @@ use tower_http::{ async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "websocket=debug,tower_http=debug") + std::env::set_var("RUST_LOG", "example_websockets=debug,tower_http=debug") } tracing_subscriber::fmt::init(); @@ -34,7 +34,7 @@ async fn main() { let app = nest( "/", axum::service::get( - ServeDir::new("examples/websocket").append_index_html_on_directories(true), + ServeDir::new("examples/websockets/assets").append_index_html_on_directories(true), ) .handle_error(|error: std::io::Error| { Ok::<_, std::convert::Infallible>((