mirror of
https://github.com/tokio-rs/axum.git
synced 2025-01-11 12:31:25 +01:00
Move all examples to their own crates (#201)
This makes it much clearer which dependencies each example has.
This commit is contained in:
parent
1ae0dee53b
commit
6c9651c14a
54 changed files with 403 additions and 185 deletions
45
Cargo.toml
45
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"]
|
||||
|
|
|
@ -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 `<form>`.
|
||||
- [`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.
|
||||
|
|
13
examples/async-graphql/Cargo.toml
Normal file
13
examples/async-graphql/Cargo.toml
Normal file
|
@ -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"
|
13
examples/chat/Cargo.toml
Normal file
13
examples/chat/Cargo.toml
Normal file
|
@ -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"
|
|
@ -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"))
|
||||
}
|
15
examples/error-handling-and-dependency-injection/Cargo.toml
Normal file
15
examples/error-handling-and-dependency-injection/Cargo.toml
Normal file
|
@ -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"] }
|
|
@ -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,
|
||||
}
|
12
examples/form/Cargo.toml
Normal file
12
examples/form/Cargo.toml
Normal file
|
@ -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"] }
|
|
@ -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();
|
||||
|
12
examples/global-404-handler/Cargo.toml
Normal file
12
examples/global-404-handler/Cargo.toml
Normal file
|
@ -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"
|
|
@ -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();
|
||||
|
9
examples/hello-world/Cargo.toml
Normal file
9
examples/hello-world/Cargo.toml
Normal file
|
@ -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"] }
|
26
examples/hello-world/src/main.rs
Normal file
26
examples/hello-world/src/main.rs
Normal file
|
@ -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 {
|
||||
"<h1>Hello, World!</h1>"
|
||||
}
|
|
@ -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("<h1>Hello, World!</h1>")
|
||||
}
|
13
examples/key-value-store/Cargo.toml
Normal file
13
examples/key-value-store/Cargo.toml
Normal file
|
@ -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" ] }
|
|
@ -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<SharedState>) -> String {
|
|||
.join("\n")
|
||||
}
|
||||
|
||||
fn admin_routes() -> BoxRoute<hyper::Body> {
|
||||
fn admin_routes() -> BoxRoute<Body> {
|
||||
async fn delete_all_keys(Extension(state): Extension<SharedState>) {
|
||||
state.write().unwrap().db.clear();
|
||||
}
|
12
examples/multipart-form/Cargo.toml
Normal file
12
examples/multipart-form/Cargo.toml
Normal file
|
@ -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" ] }
|
|
@ -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();
|
||||
|
16
examples/oauth/Cargo.toml
Normal file
16
examples/oauth/Cargo.toml
Normal file
|
@ -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"
|
|
@ -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<Bytes>;
|
||||
type BodyError = <Self::Body as axum::body::HttpBody>::Error;
|
||||
|
||||
fn into_response(self) -> http::Response<Self::Body> {
|
||||
fn into_response(self) -> Response<Self::Body> {
|
||||
Redirect::found("/auth/discord".parse().unwrap()).into_response()
|
||||
}
|
||||
}
|
14
examples/sessions/Cargo.toml
Normal file
14
examples/sessions/Cargo.toml
Normal file
|
@ -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"
|
|
@ -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();
|
||||
|
15
examples/sse/Cargo.toml
Normal file
15
examples/sse/Cargo.toml
Normal file
|
@ -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"
|
|
@ -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)
|
12
examples/static-file-server/Cargo.toml
Normal file
12
examples/static-file-server/Cargo.toml
Normal file
|
@ -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"] }
|
|
@ -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();
|
||||
|
12
examples/templates/Cargo.toml
Normal file
12
examples/templates/Cargo.toml
Normal file
|
@ -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"
|
|
@ -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();
|
||||
|
17
examples/testing/Cargo.toml
Normal file
17
examples/testing/Cargo.toml
Normal file
|
@ -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"] }
|
|
@ -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<Body> {
|
|||
#[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()`
|
13
examples/tls-rustls/Cargo.toml
Normal file
13
examples/tls-rustls/Cargo.toml
Normal file
|
@ -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"] }
|
|
@ -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);
|
15
examples/todos/Cargo.toml
Normal file
15
examples/todos/Cargo.toml
Normal file
|
@ -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"] }
|
|
@ -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();
|
||||
|
14
examples/tokio-postgres/Cargo.toml
Normal file
14
examples/tokio-postgres/Cargo.toml
Normal file
|
@ -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"
|
|
@ -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();
|
||||
|
12
examples/tracing-aka-logging/Cargo.toml
Normal file
12
examples/tracing-aka-logging/Cargo.toml
Normal file
|
@ -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"] }
|
|
@ -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();
|
||||
|
14
examples/unix-domain-socket/Cargo.toml
Normal file
14
examples/unix-domain-socket/Cargo.toml
Normal file
|
@ -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"
|
|
@ -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,
|
11
examples/versioning/Cargo.toml
Normal file
11
examples/versioning/Cargo.toml
Normal file
|
@ -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"
|
|
@ -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();
|
||||
|
13
examples/websockets/Cargo.toml
Normal file
13
examples/websockets/Cargo.toml
Normal file
|
@ -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"
|
|
@ -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>((
|
Loading…
Reference in a new issue