mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-29 00:00:18 +02:00
Move all examples to their own crates (#201)
This makes it much clearer which dependencies each example has.
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
//! Run with
|
||||
//!
|
||||
//! ```not_rust
|
||||
//! cargo run -p example-tracing-aka-logging
|
||||
//! ```
|
||||
|
||||
use axum::{handler::get, response::Html, route, routing::RoutingDsl};
|
||||
use std::net::SocketAddr;
|
||||
use tower_http::trace::TraceLayer;
|
||||
|
||||
#[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",
|
||||
"example_tracing_aka_logging=debug,tower_http=debug",
|
||||
)
|
||||
}
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
// build our application with a route
|
||||
let app = route("/", get(handler))
|
||||
// `TraceLayer` is provided by tower-http so you have to add that as a dependency.
|
||||
// It provides good defaults but is also very customizable.
|
||||
// See https://docs.rs/tower-http/0.1.1/tower_http/trace/index.html for more details.
|
||||
.layer(TraceLayer::new_for_http());
|
||||
|
||||
// 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>")
|
||||
}
|
||||
Reference in New Issue
Block a user