mirror of
https://github.com/perstarkse/minne.git
synced 2026-04-17 22:49:43 +02:00
33 lines
982 B
Rust
33 lines
982 B
Rust
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<S>(
|
|
State(state): State<S>,
|
|
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::<bool>("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
|
|
}
|