use super::{LOG_TARGET, Session, SessionParams};
use axum::extract::Request;
use axum::extract::State;
use axum::middleware::Next;
use axum::response::Response;
use scopeguard::defer;
use std::sync::Arc;
use tibba_cache::RedisCache;
use tracing::debug;
type Result<T, E = tibba_error::Error> = std::result::Result<T, E>;
pub async fn session(
State((cache, params)): State<(&'static RedisCache, Arc<SessionParams>)>,
mut req: Request,
next: Next,
) -> Result<Response> {
debug!(target: LOG_TARGET, "--> session");
defer!(debug!(target: LOG_TARGET, "<-- session"););
req.extensions_mut().insert(Session::new(cache, params));
Ok(next.run(req).await)
}