Add SQLite visitor IP logging to /server_storage
All checks were successful
Deploy Website / build-and-deploy (push) Successful in 1m9s
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:
28
lib/db.ts
Normal file
28
lib/db.ts
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user