use crate::http::response::IntoResponse;
use crate::http::StatusCode;
use tower_http::catch_panic::CatchPanicLayer;
use tower_http::cors::{Any, CorsLayer};
use tower_http::trace::TraceLayer;
use tracing_subscriber::EnvFilter;
pub fn init_tracing() {
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "info,tower_http=debug".parse().unwrap()),
)
.init();
}
pub fn default_cors() -> CorsLayer {
CorsLayer::new()
.allow_origin(Any)
.allow_methods(Any)
.allow_headers(Any)
}
pub fn default_trace() -> TraceLayer<tower_http::classify::SharedClassifier<tower_http::classify::ServerErrorsAsFailures>> {
TraceLayer::new_for_http()
}
pub fn catch_panic_layer() -> CatchPanicLayer<fn(Box<dyn std::any::Any + Send>) -> crate::http::Response> {
CatchPanicLayer::custom(panic_handler as fn(_) -> _)
}
fn panic_handler(_err: Box<dyn std::any::Any + Send>) -> crate::http::Response {
let body = serde_json::json!({ "error": "Internal server error" });
(StatusCode::INTERNAL_SERVER_ERROR, crate::http::Json(body)).into_response()
}