skip trcking robots and tracking paths

This commit is contained in:
JurajKubrican
2025-08-18 14:26:49 +02:00
parent 3249df5802
commit 1e00e24e5d
2 changed files with 30 additions and 6 deletions

View File

@@ -2,6 +2,7 @@ package tracking
import (
"fmt"
"strings"
"github.com/labstack/echo/v4"
"knet.sk/src/util"
@@ -34,10 +35,33 @@ type PageStat struct {
var ts *TrackingService
var ignorePaths = []string{
"/tracking",
"/metrics",
}
var ignoreUserAgents = []string{
"Prometheus",
"UptimeRobot",
}
func Echo(next echo.HandlerFunc) echo.HandlerFunc {
ts = StartTrackingService()
return func(c echo.Context) error {
fmt.Printf("Tracking visit: %s %s\n", c.RealIP(), c.Request().URL.Path)
// Check if the path is in the ignore list
for _, path := range ignorePaths {
if c.Request().URL.Path == path {
return next(c) // Skip tracking for ignored paths
}
}
// Check if the user agent is in the ignore list
for _, ua := range ignoreUserAgents {
if strings.Contains(c.Request().UserAgent(), ua) {
return next(c) // Skip tracking for ignored user agents
}
}
go ts.AddVisit(c)
return next(c)
}

View File

@@ -24,13 +24,13 @@ func StartTrackingService() *TrackingService {
// Apply SQLite settings explicitly with PRAGMA statements
pragmas := []string{
"PRAGMA busy_timeout = 10000", // 10 second busy timeout
"PRAGMA journal_mode = WAL", // Write-Ahead Logging mode
"PRAGMA synchronous = NORMAL", // Normal synchronization
"PRAGMA foreign_keys = ON", // Enable foreign key constraints
"PRAGMA temp_store = MEMORY", // Store temp tables in memory
"PRAGMA busy_timeout = 10000", // 10 second busy timeout
"PRAGMA journal_mode = WAL", // Write-Ahead Logging mode
"PRAGMA synchronous = NORMAL", // Normal synchronization
"PRAGMA foreign_keys = ON", // Enable foreign key constraints
"PRAGMA temp_store = MEMORY", // Store temp tables in memory
}
for _, pragma := range pragmas {
if _, err := db.Exec(pragma); err != nil {
println("Warning: Failed to set pragma:", pragma, "Error:", err.Error())