feat: visitors and load analytics middleware

This commit is contained in:
Per Stark
2025-01-24 10:51:45 +01:00
parent ce5effc0bf
commit a9e2f67b3f
6 changed files with 39 additions and 7 deletions

View File

@@ -17,6 +17,7 @@ use tracing_subscriber::{fmt, prelude::*, EnvFilter};
use zettle_db::{
ingress::jobqueue::JobQueue,
server::{
middleware_analytics::analytics_middleware,
middleware_api_auth::api_auth,
routes::{
api::{
@@ -122,6 +123,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
session_store,
auth_config,
app_state.surreal_db_client.client.clone(),
&app_state,
),
)
.with_state(app_state);
@@ -148,11 +150,11 @@ fn api_routes_v1(app_state: &AppState) -> Router<AppState> {
}
/// Router for HTML endpoints
///
fn html_routes(
session_store: SessionStore<SessionSurrealPool<Any>>,
auth_config: AuthConfig<String>,
db_client: Surreal<Any>,
app_state: &AppState,
) -> Router<AppState> {
Router::new()
.route("/", get(index_handler))
@@ -178,6 +180,7 @@ fn html_routes(
.route("/documentation", get(show_documentation_index))
.route("/documentation/privacy-policy", get(show_privacy_policy))
.nest_service("/assets", ServeDir::new("assets/"))
.layer(from_fn_with_state(app_state.clone(), analytics_middleware))
.layer(
AuthSessionLayer::<User, String, SessionSurrealPool<Any>, Surreal<Any>>::new(Some(
db_client,

View File

@@ -0,0 +1,33 @@
use axum::{
extract::{Request, State},
middleware::Next,
response::Response,
};
use axum_session_surreal::SessionSurrealPool;
use surrealdb::engine::any::Any;
use crate::storage::types::analytics::Analytics;
use super::AppState;
pub async fn analytics_middleware(
State(state): State<AppState>,
session: axum_session::Session<SessionSurrealPool<Any>>,
request: Request,
next: Next,
) -> Response {
// Get the path from the request
let path = request.uri().path();
// Only count if it's a main page request (not assets or other resources)
if !path.starts_with("/assets") && !path.starts_with("/_next") && !path.contains('.') {
if !session.get::<bool>("counted_visitor").unwrap_or(false) {
let _ = Analytics::increment_visitors(&state.surreal_db_client).await;
session.set("counted_visitor", true);
}
let _ = Analytics::increment_page_loads(&state.surreal_db_client).await;
}
next.run(request).await
}

View File

@@ -4,6 +4,7 @@ use crate::utils::mailer::Mailer;
use minijinja_autoreload::AutoReloader;
use std::sync::Arc;
pub mod middleware_analytics;
pub mod middleware_api_auth;
pub mod routes;

View File

@@ -1,15 +1,13 @@
use axum::{
extract::State,
http::{StatusCode, Uri},
response::{IntoResponse, Redirect},
};
use axum_htmx::HxRedirect;
use axum_session_auth::AuthSession;
use axum_session_surreal::SessionSurrealPool;
use surrealdb::{engine::any::Any, Surreal};
use crate::{
error::{AppError, HtmlError},
error::HtmlError,
page_data,
server::{routes::html::render_template, AppState},
storage::types::{analytics::Analytics, system_settings::SystemSettings, user::User},

View File

@@ -7,10 +7,8 @@ use axum::{
use axum_htmx::{HxBoosted, HxRedirect};
use axum_session_auth::AuthSession;
use axum_session_surreal::SessionSurrealPool;
use chrono::RoundingError;
use serde::{Deserialize, Serialize};
use surrealdb::{engine::any::Any, Surreal};
use tracing::info;
use crate::{error::HtmlError, server::AppState, storage::types::user::User};

View File

@@ -1,6 +1,5 @@
use crate::storage::types::{file_info::deserialize_flexible_id, user::User, StoredObject};
use serde::{Deserialize, Serialize};
use tracing::info;
use crate::{error::AppError, storage::db::SurrealDbClient};