Add a trace layer
This commit is contained in:
parent
980c5884a4
commit
1f03c01d1d
5 changed files with 26 additions and 21 deletions
|
|
@ -11,7 +11,8 @@ use axum_extra::extract::cookie::CookieJar;
|
|||
use chrono::prelude::*;
|
||||
use clap::Parser;
|
||||
use config::Config;
|
||||
use tower_http::services::ServeDir;
|
||||
use tower_http::{services::ServeDir, trace::TraceLayer};
|
||||
use tracing::{event, Level};
|
||||
|
||||
use data::db;
|
||||
|
||||
|
|
@ -42,6 +43,12 @@ impl FromRef<AppState> for db::Connection {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
const TRACING_LEVEL: tracing::Level = tracing::Level::DEBUG;
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
const TRACING_LEVEL: tracing::Level = tracing::Level::INFO;
|
||||
|
||||
// TODO: Should main returns 'Result'?
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
|
@ -49,14 +56,16 @@ async fn main() {
|
|||
return;
|
||||
}
|
||||
|
||||
println!("Starting Recipes as web server...");
|
||||
tracing_subscriber::fmt()
|
||||
.with_max_level(TRACING_LEVEL)
|
||||
.init();
|
||||
|
||||
event!(Level::INFO, "Starting Recipes as web server...");
|
||||
|
||||
let config = config::load();
|
||||
let port = config.port;
|
||||
|
||||
println!("Configuration: {:?}", config);
|
||||
|
||||
tracing_subscriber::fmt::init();
|
||||
event!(Level::INFO, "Configuration: {:?}", config);
|
||||
|
||||
let db_connection = db::Connection::new().await.unwrap();
|
||||
|
||||
|
|
@ -78,6 +87,7 @@ async fn main() {
|
|||
get(services::sign_in_get).post(services::sign_in_post),
|
||||
)
|
||||
.route("/signout", get(services::sign_out))
|
||||
.layer(TraceLayer::new_for_http())
|
||||
.route_layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
user_authentication,
|
||||
|
|
@ -117,23 +127,18 @@ async fn get_current_user(
|
|||
.authentication(token_cookie.value(), &client_ip, &client_user_agent)
|
||||
.await
|
||||
{
|
||||
Ok(db::AuthenticationResult::NotValidToken) => {
|
||||
// TODO: remove cookie?
|
||||
None
|
||||
}
|
||||
Ok(db::AuthenticationResult::NotValidToken) => None,
|
||||
Ok(db::AuthenticationResult::Ok(user_id)) => {
|
||||
match connection.load_user(user_id).await {
|
||||
Ok(user) => user,
|
||||
Err(error) => {
|
||||
// TODO: Return 'Result'?
|
||||
println!("Error during authentication: {}", error);
|
||||
event!(Level::WARN, "Error during authentication: {}", error);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
// TODO: Return 'Result'?
|
||||
println!("Error during authentication: {}", error);
|
||||
event!(Level::WARN, "Error during authentication: {}", error);
|
||||
None
|
||||
}
|
||||
},
|
||||
|
|
@ -176,7 +181,7 @@ async fn process_args() -> bool {
|
|||
match db::Connection::new().await {
|
||||
Ok(con) => {
|
||||
if let Err(error) = con.execute_file("sql/data_test.sql").await {
|
||||
eprintln!("{}", error);
|
||||
event!(Level::ERROR, "{}", error);
|
||||
}
|
||||
// Set the creation datetime to 'now'.
|
||||
con.execute_sql(
|
||||
|
|
@ -188,7 +193,7 @@ async fn process_args() -> bool {
|
|||
.unwrap();
|
||||
}
|
||||
Err(error) => {
|
||||
eprintln!("{}", error);
|
||||
event!(Level::ERROR, "{}", error);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue