Added basic request logging example

This commit is contained in:
someone 2024-04-13 12:14:33 +02:00
parent 065cae6f26
commit 57eb1b08db
3 changed files with 19 additions and 4 deletions

1
Cargo.lock generated
View file

@ -806,6 +806,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"axum", "axum",
"env_logger", "env_logger",
"log",
"tokio", "tokio",
"utoipa", "utoipa",
] ]

View file

@ -10,3 +10,4 @@ env_logger = { workspace = true }
tokio = { workspace = true, features=["full"] } tokio = { workspace = true, features=["full"] }
axum = { workspace = true } axum = { workspace = true }
utoipa = { version = "4", features = ["axum_extras"] } utoipa = { version = "4", features = ["axum_extras"] }
log = "0.4.21"

View file

@ -1,18 +1,31 @@
use axum::{routing::get, Router}; use axum::{extract::Request, routing::{get, post}, Router};
use log;
use std::env;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
env::set_var("RUST_LOG", "trace");
env_logger::init(); env_logger::init();
// build our application with a route // build our application with a route
let app = Router::new() let app = Router::new()
.route("/", get(root)); .route("/", get(root))
.route("/v1/secret/data/foo", post(foo));
// run our app with hyper, listening globally on port 8200 // run our app with hyper, listening globally on port 8200
let listener = tokio::net::TcpListener::bind("[::]:8200").await.unwrap(); let listener = tokio::net::TcpListener::bind("127.0.0.1:8200").await.unwrap();
axum::serve(listener, app).await.unwrap(); axum::serve(listener, app).await.unwrap();
} }
async fn foo( req: Request) -> String {
log::debug!("`{:?}`", req);
return String::from("RoutingTest successful");
}
// basic handler that responds with a static string // basic handler that responds with a static string
async fn root() -> &'static str { async fn root() -> &'static str {
"Hello, World!" "Hello, World!"