mirror of
https://github.com/thanipro/Axum-Rust-Rest-Api-Template
synced 2024-11-10 06:04:16 +00:00
Add tracing subscriber and remove redundant logger middleware
This commit is contained in:
parent
c59be1b374
commit
1fab7958b3
6 changed files with 70 additions and 9 deletions
68
Cargo.lock
generated
68
Cargo.lock
generated
|
@ -181,6 +181,7 @@ dependencies = [
|
|||
"tokio",
|
||||
"tower",
|
||||
"tower-http",
|
||||
"tracing-subscriber",
|
||||
"validator",
|
||||
]
|
||||
|
||||
|
@ -1564,6 +1565,16 @@ version = "0.3.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be"
|
||||
|
||||
[[package]]
|
||||
name = "nu-ansi-term"
|
||||
version = "0.46.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
|
||||
dependencies = [
|
||||
"overload",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.4.3"
|
||||
|
@ -1689,6 +1700,12 @@ version = "6.5.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ceedf44fb00f2d1984b0bc98102627ce622e083e49a5bacdb3e514fa4238e267"
|
||||
|
||||
[[package]]
|
||||
name = "overload"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.11.2"
|
||||
|
@ -2247,6 +2264,15 @@ dependencies = [
|
|||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sharded-slab"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.4.1"
|
||||
|
@ -2568,6 +2594,16 @@ dependencies = [
|
|||
"syn 2.0.15",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "1.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.1.45"
|
||||
|
@ -2768,6 +2804,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"valuable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-log"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"log",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-subscriber"
|
||||
version = "0.3.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77"
|
||||
dependencies = [
|
||||
"nu-ansi-term",
|
||||
"sharded-slab",
|
||||
"smallvec",
|
||||
"thread_local",
|
||||
"tracing-core",
|
||||
"tracing-log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -2904,6 +2966,12 @@ dependencies = [
|
|||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.15"
|
||||
|
|
|
@ -21,4 +21,5 @@ sqlx = { version = "0.6.3", features = ["runtime-tokio-native-tls", "migrate", "
|
|||
validator = { version = "0.16.0", features = ["derive"] }
|
||||
async-trait = "0.1.68"
|
||||
sqlx-cli = "0.6.3"
|
||||
tracing-subscriber = "0.3.17"
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@ async fn main() {
|
|||
.unwrap_or_else(|e| panic!("Database error: {}", e.to_string()));
|
||||
|
||||
let host = format!("0.0.0.0:{}", parameter::get("PORT"));
|
||||
tracing_subscriber::fmt::init();
|
||||
axum::Server::bind(&host.parse().unwrap())
|
||||
.serve(routes::root::routes(Arc::new(connection)))
|
||||
.await
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
use axum::{http::Request, middleware::Next, response::IntoResponse};
|
||||
|
||||
pub async fn logger<B>(req: Request<B>, next: Next<B>) -> impl IntoResponse {
|
||||
println!("{} {}", req.method(), req.uri());
|
||||
next.run(req).await
|
||||
}
|
|
@ -1,2 +1 @@
|
|||
pub mod auth;
|
||||
pub mod logger;
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use super::auth;
|
||||
use crate::config::database::Database;
|
||||
use crate::middleware::auth as auth_middleware;
|
||||
use crate::middleware::logger;
|
||||
use crate::routes::{profile, register};
|
||||
use crate::state::auth_state::AuthState;
|
||||
use crate::state::token_state::TokenState;
|
||||
|
@ -29,7 +28,6 @@ pub fn routes(db_conn: Arc<Database>) -> IntoMakeService<Router> {
|
|||
|
||||
let app_router = Router::new()
|
||||
.nest("/api", merged_router)
|
||||
.layer(ServiceBuilder::new().layer(middleware::from_fn(logger::logger)))
|
||||
.layer(TraceLayer::new_for_http());
|
||||
|
||||
app_router.into_make_service()
|
||||
|
|
Loading…
Reference in a new issue