feat: added the sqlite logging main logic

added sqlite logic which is being used across the files to log whitelist requests
This commit is contained in:
2026-05-04 08:11:16 +03:30
parent 8af9958743
commit 4e949912d6

44
sqlite_logger.go Normal file
View File

@@ -0,0 +1,44 @@
package main
import (
"database/sql"
"log/slog"
"time"
_ "modernc.org/sqlite"
)
func initDB(dbPath string) (*sql.DB, error) {
db, err := sql.Open("sqlite", dbPath)
if err != nil {
return nil, err
}
query := `
CREATE TABLE IF NOT EXISTS whitelist_audit (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp DATETIME NOT NULL,
action TEXT NOT NULL,
ip TEXT NOT NULL,
ttl_seconds INTEGER DEFAULT 0,
reason TEXT,
api_user TEXT
);
CREATE INDEX IF NOT EXISTS idx_timestamp ON whitelist_audit(timestamp);
`
_, err = db.Exec(query)
if err != nil {
return nil, err
}
return db, nil
}
func logWhitelistEvent(db *sql.DB, action, ip string, ttl int, reason, apiUser string) {
_, err := db.Exec(
`INSERT INTO whitelist_audit(timestamp, action, ip, ttl_seconds, reason, api_user)
VALUES(?, ?, ?, ?, ?, ?)`,
time.Now(), action, ip, ttl, reason, apiUser,
)
if err != nil {
slog.Error("failed to log whitelist event", "error", err)
}
}