Add example

This commit is contained in:
Benjamin Sparks 2024-10-20 13:41:00 +02:00
parent 62a4ea16a2
commit 64fb7329c1
2 changed files with 53 additions and 0 deletions

View 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"] }

View 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
}