feat: enhance logging and dependency management with dotenvy and tracing-subscriber
This commit is contained in:
@@ -19,3 +19,5 @@ strum.workspace = true
|
||||
strum_macros.workspace = true
|
||||
uuid.workspace = true
|
||||
tracing.workspace = true
|
||||
dotenvy = "0.15"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
use tracing::{info};
|
||||
use surrealdb::engine::any;
|
||||
use surrealdb::method::Query;
|
||||
|
||||
/// Binds a filter value to the query under the key "filter".
|
||||
pub fn bind_filter_value(
|
||||
query: Query<'_, any::Any>,
|
||||
val: String,
|
||||
) -> Query<'_, any::Any> {
|
||||
query.bind(("filter", val))
|
||||
info!(?val, "bind_filter_value called with arguments");
|
||||
let result = query.bind(("filter", val.clone()));
|
||||
info!(?val, "bind_filter_value returning query with bound filter");
|
||||
result
|
||||
}
|
||||
|
||||
@@ -1,17 +1,55 @@
|
||||
use tracing::{info, error};
|
||||
use crate::decode_access_token;
|
||||
use axum::http::{HeaderMap, header::AUTHORIZATION};
|
||||
|
||||
/// Extracts the email from the Authorization header, if present and valid.
|
||||
pub fn extract_email(headers: &HeaderMap) -> Option<String> {
|
||||
let auth_header = headers.get(AUTHORIZATION)?.to_str().ok()?;
|
||||
let token = auth_header.strip_prefix("Bearer ")?;
|
||||
|
||||
match decode_access_token(token) {
|
||||
Ok(data) => Some(data.claims.sub),
|
||||
Err(_e) => None,
|
||||
}
|
||||
info!(?headers, "extract_email called with headers");
|
||||
let auth_header = match headers.get(AUTHORIZATION) {
|
||||
Some(h) => h,
|
||||
None => {
|
||||
error!("Authorization header missing in extract_email");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let auth_str = match auth_header.to_str() {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
error!(error = ?e, "Failed to convert Authorization header to str in extract_email");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let token = match auth_str.strip_prefix("Bearer ") {
|
||||
Some(t) => t,
|
||||
None => {
|
||||
error!(auth_str, "Authorization header does not start with 'Bearer ' in extract_email");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
info!(token, "Extracted bearer token in extract_email");
|
||||
match decode_access_token(token) {
|
||||
Ok(data) => {
|
||||
info!(email = %data.claims.sub, "Successfully decoded access token in extract_email");
|
||||
Some(data.claims.sub)
|
||||
}
|
||||
Err(e) => {
|
||||
error!(error = ?e, "Failed to decode access token in extract_email");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Extracts the email from a JWT token string.
|
||||
pub fn extract_email_token(token: String) -> Option<String> {
|
||||
let token_data = decode_access_token(&token).ok()?;
|
||||
Some(token_data.claims.sub)
|
||||
info!(token = %token, "extract_email_token called with token");
|
||||
match decode_access_token(&token) {
|
||||
Ok(data) => {
|
||||
info!(email = %data.claims.sub, "Successfully decoded token in extract_email_token");
|
||||
Some(data.claims.sub)
|
||||
}
|
||||
Err(e) => {
|
||||
error!(error = ?e, "Failed to decode token in extract_email_token");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
use tracing::{info};
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
/// Returns the current UTC date/time as an RFC3339 string.
|
||||
pub fn get_iso_date() -> String {
|
||||
let now: DateTime<Utc> = Utc::now();
|
||||
now.to_rfc3339()
|
||||
info!("get_iso_date called");
|
||||
let now: DateTime<Utc> = Utc::now();
|
||||
let date_str = now.to_rfc3339();
|
||||
info!(date_str = %date_str, "get_iso_date returning RFC3339 date string");
|
||||
date_str
|
||||
}
|
||||
|
||||
@@ -1,15 +1,29 @@
|
||||
use tracing::{info, error};
|
||||
use anyhow::{Result, bail};
|
||||
use surrealdb::sql::Thing;
|
||||
|
||||
/// Extracts the table and id from a Thing, returning (&str, &str).
|
||||
pub fn get_id(thing: &Thing) -> Result<(&str, &str)> {
|
||||
let table = thing.tb.as_str();
|
||||
let id = match &thing.id {
|
||||
surrealdb::sql::Id::String(s) => s.as_str(),
|
||||
_ => bail!("Unsupported ID type"),
|
||||
};
|
||||
Ok((table, id))
|
||||
info!(?thing, "get_id called with argument");
|
||||
let table = thing.tb.as_str();
|
||||
let id = match &thing.id {
|
||||
surrealdb::sql::Id::String(s) => {
|
||||
info!(id = %s, "ID extracted as string in get_id");
|
||||
s.as_str()
|
||||
}
|
||||
other => {
|
||||
error!(?other, "Unsupported ID type in get_id");
|
||||
bail!("Unsupported ID type");
|
||||
}
|
||||
};
|
||||
info!(table = %table, id = %id, "get_id returning table and id");
|
||||
Ok((table, id))
|
||||
}
|
||||
|
||||
/// Extracts the raw id string from a Thing.
|
||||
pub fn extract_id(thing: &Thing) -> String {
|
||||
thing.id.to_raw()
|
||||
info!(?thing, "extract_id called with argument");
|
||||
let raw_id = thing.id.to_raw();
|
||||
info!(raw_id = %raw_id, "extract_id returning raw id string");
|
||||
raw_id
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
mod logger;
|
||||
pub mod logger;
|
||||
pub mod bind_filter;
|
||||
pub mod extract_email;
|
||||
pub mod generate_date;
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
//! Logger initialization using tracing, tracing-subscriber, and dotenvy.
|
||||
use std::env;
|
||||
use dotenvy::dotenv;
|
||||
use tracing_subscriber::{EnvFilter, fmt};
|
||||
|
||||
/// Initializes the logger using tracing and tracing-subscriber.
|
||||
/// Loads environment variables from `.env` and sets log level from `RUST_LOG`.
|
||||
pub fn init_logger() {
|
||||
// Load .env file if present
|
||||
dotenv().ok();
|
||||
|
||||
|
||||
// Set up the tracing subscriber with EnvFilter from RUST_LOG
|
||||
let filter = EnvFilter::try_from_default_env()
|
||||
.or_else(|_| EnvFilter::try_new("warn"))
|
||||
|
||||
Reference in New Issue
Block a user