httpd: Use `TraceLayer` on uppermost router
This allows prefix of each route e.g. `/api/*` to be visible in logs. Signed-off-by: xphoniex <dj.2dixx@gmail.com>
This commit is contained in:
parent
94be0ca392
commit
bd1b4ac5c1
|
|
@ -2,19 +2,15 @@ use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use axum::body::{Body, BoxBody};
|
|
||||||
use axum::http::header::{AUTHORIZATION, CONTENT_TYPE};
|
use axum::http::header::{AUTHORIZATION, CONTENT_TYPE};
|
||||||
use axum::http::Method;
|
use axum::http::Method;
|
||||||
use axum::response::{IntoResponse, Json};
|
use axum::response::{IntoResponse, Json};
|
||||||
use axum::routing::get;
|
use axum::routing::get;
|
||||||
use axum::{Extension, Router};
|
use axum::{Extension, Router};
|
||||||
use hyper::http::{Request, Response};
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
use tower_http::cors::{self, CorsLayer};
|
use tower_http::cors::{self, CorsLayer};
|
||||||
use tower_http::trace::TraceLayer;
|
|
||||||
use tracing::Span;
|
|
||||||
|
|
||||||
use radicle::cob::issue::Issues;
|
use radicle::cob::issue::Issues;
|
||||||
use radicle::identity::Id;
|
use radicle::identity::Id;
|
||||||
|
|
@ -77,26 +73,6 @@ pub fn router(ctx: Context) -> Router {
|
||||||
.allow_methods([Method::GET, Method::POST, Method::PUT])
|
.allow_methods([Method::GET, Method::POST, Method::PUT])
|
||||||
.allow_headers([CONTENT_TYPE, AUTHORIZATION]),
|
.allow_headers([CONTENT_TYPE, AUTHORIZATION]),
|
||||||
)
|
)
|
||||||
.layer(
|
|
||||||
TraceLayer::new_for_http()
|
|
||||||
.make_span_with(|request: &Request<Body>| {
|
|
||||||
tracing::info_span!(
|
|
||||||
"request",
|
|
||||||
method = %request.method(),
|
|
||||||
uri = %request.uri(),
|
|
||||||
status = tracing::field::Empty,
|
|
||||||
latency = tracing::field::Empty,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.on_response(
|
|
||||||
|response: &Response<BoxBody>, latency: Duration, span: &Span| {
|
|
||||||
span.record("status", &tracing::field::debug(response.status()));
|
|
||||||
span.record("latency", &tracing::field::debug(latency));
|
|
||||||
|
|
||||||
tracing::info!("Processed");
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn root_handler(Extension(ctx): Extension<Context>) -> impl IntoResponse {
|
async fn root_handler(Extension(ctx): Extension<Context>) -> impl IntoResponse {
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,16 @@ pub async fn run(options: Options) -> anyhow::Result<()> {
|
||||||
|
|
||||||
let git_router = Router::new()
|
let git_router = Router::new()
|
||||||
.route("/:project/*request", any(git_handler))
|
.route("/:project/*request", any(git_handler))
|
||||||
.layer(Extension(profile.clone()))
|
.layer(Extension(profile.clone()));
|
||||||
|
|
||||||
|
let ctx = api::Context::new(profile);
|
||||||
|
let api_router = api::router(ctx);
|
||||||
|
|
||||||
|
tracing::info!("listening on http://{}", options.listen);
|
||||||
|
|
||||||
|
let app = Router::new()
|
||||||
|
.merge(git_router)
|
||||||
|
.nest("/api", api_router)
|
||||||
.layer(
|
.layer(
|
||||||
TraceLayer::new_for_http()
|
TraceLayer::new_for_http()
|
||||||
.make_span_with(|request: &Request<Body>| {
|
.make_span_with(|request: &Request<Body>| {
|
||||||
|
|
@ -75,16 +84,7 @@ pub async fn run(options: Options) -> anyhow::Result<()> {
|
||||||
tracing::info!("Processed");
|
tracing::info!("Processed");
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
)
|
||||||
|
|
||||||
let ctx = api::Context::new(profile);
|
|
||||||
let api_router = api::router(ctx);
|
|
||||||
|
|
||||||
tracing::info!("listening on http://{}", options.listen);
|
|
||||||
|
|
||||||
let app = Router::new()
|
|
||||||
.merge(git_router)
|
|
||||||
.nest("/api", api_router)
|
|
||||||
.into_make_service_with_connect_info::<SocketAddr>();
|
.into_make_service_with_connect_info::<SocketAddr>();
|
||||||
|
|
||||||
axum::Server::bind(&options.listen)
|
axum::Server::bind(&options.listen)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue