use axum::{ extract::{Request, State}, http::Method, middleware::Next, response::Response, }; use common::storage::{db::ProvidesDb, types::analytics::Analytics}; use crate::SessionType; /// Middleware to count unique visitors and page loads pub async fn analytics_middleware( State(state): State, session: SessionType, request: Request, next: Next, ) -> Response where S: ProvidesDb + Clone + Send + Sync + 'static, { let path = request.uri().path(); // Only count visits/page loads for GET requests to non-asset, non-static paths if request.method() == Method::GET && !path.starts_with("/assets") && !path.contains('.') { if !session.get::("counted_visitor").unwrap_or(false) { let _ = Analytics::increment_visitors(state.db()).await; session.set("counted_visitor", true); } let _ = Analytics::increment_page_loads(state.db()).await; } next.run(request).await }