Added basic request logging example
This commit is contained in:
parent
065cae6f26
commit
57eb1b08db
3 changed files with 19 additions and 4 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -806,6 +806,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"axum",
|
||||
"env_logger",
|
||||
"log",
|
||||
"tokio",
|
||||
"utoipa",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -10,3 +10,4 @@ env_logger = { workspace = true }
|
|||
tokio = { workspace = true, features=["full"] }
|
||||
axum = { workspace = true }
|
||||
utoipa = { version = "4", features = ["axum_extras"] }
|
||||
log = "0.4.21"
|
||||
|
|
@ -1,18 +1,31 @@
|
|||
use axum::{routing::get, Router};
|
||||
use axum::{extract::Request, routing::{get, post}, Router};
|
||||
|
||||
use log;
|
||||
use std::env;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
env::set_var("RUST_LOG", "trace");
|
||||
env_logger::init();
|
||||
|
||||
// build our application with a route
|
||||
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
|
||||
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();
|
||||
}
|
||||
|
||||
async fn foo( req: Request) -> String {
|
||||
log::debug!("`{:?}`", req);
|
||||
return String::from("RoutingTest successful");
|
||||
|
||||
}
|
||||
|
||||
// basic handler that responds with a static string
|
||||
async fn root() -> &'static str {
|
||||
"Hello, World!"
|
||||
|
|
|
|||
Loading…
Reference in a new issue