mirror of
https://github.com/tokio-rs/axum.git
synced 2024-12-11 17:30:44 +01:00
Add example
This commit is contained in:
parent
62a4ea16a2
commit
64fb7329c1
2 changed files with 53 additions and 0 deletions
13
examples/spoofable-scheme/Cargo.toml
Normal file
13
examples/spoofable-scheme/Cargo.toml
Normal file
|
@ -0,0 +1,13 @@
|
|||
[package]
|
||||
name = "spoofable-scheme"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
axum = { path = "../../axum" }
|
||||
axum-extra = { path = "../../axum-extra", features = ["scheme"] }
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
|
40
examples/spoofable-scheme/src/main.rs
Normal file
40
examples/spoofable-scheme/src/main.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
//! Example of application using spoofable extractors
|
||||
//!
|
||||
//! Run with
|
||||
//!
|
||||
//! ```not_rust
|
||||
//! cargo run -p spoofable-scheme
|
||||
//! ```
|
||||
//!
|
||||
//! Test with curl:
|
||||
//!
|
||||
//! ```not_rust
|
||||
//! curl -i http://localhost:3000/ -H "X-Forwarded-Proto: http"
|
||||
//! ```
|
||||
|
||||
use axum::{routing::get, Router};
|
||||
use axum_extra::extract::{Scheme, Spoofable};
|
||||
use tokio::net::TcpListener;
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
tracing_subscriber::registry()
|
||||
.with(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
|
||||
)
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
|
||||
// build our application with some routes
|
||||
let app = Router::new().route("/", get(f));
|
||||
|
||||
let listener = TcpListener::bind("127.0.0.1:3000").await.unwrap();
|
||||
tracing::debug!("listening on {}", listener.local_addr().unwrap());
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
async fn f(Spoofable(Scheme(scheme)): Spoofable<Scheme>) -> String {
|
||||
scheme
|
||||
}
|
Loading…
Reference in a new issue