Add SQLite visitor IP logging to /server_storage
All checks were successful
Deploy Website / build-and-deploy (push) Successful in 1m9s

Log visitor IPs and paths to a local SQLite database via the existing
analytics API route, persisted at /server_storage/visitors.db in Docker
(falls back to ./visitors.db in dev).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Shivam Patel
2026-02-09 00:04:45 -05:00
parent 73eb9264df
commit 4a01928fe7
6 changed files with 481 additions and 4 deletions

28
lib/db.ts Normal file
View File

@@ -0,0 +1,28 @@
import Database from 'better-sqlite3';
import path from 'path';
import fs from 'fs';
const SERVER_STORAGE = '/server_storage';
const dbPath = fs.existsSync(SERVER_STORAGE)
? path.join(SERVER_STORAGE, 'visitors.db')
: path.join(process.cwd(), 'visitors.db');
const db = new Database(dbPath);
db.pragma('journal_mode = WAL');
db.exec(`
CREATE TABLE IF NOT EXISTS visits (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ip_address TEXT NOT NULL,
path TEXT NOT NULL,
visited_at TEXT NOT NULL DEFAULT (datetime('now'))
);
`);
const insertStmt = db.prepare(
'INSERT INTO visits (ip_address, path) VALUES (?, ?)'
);
export function logVisit(ip: string, visitPath: string) {
insertStmt.run(ip, visitPath);
}