diff --git a/src/config/loader.js b/src/config/loader.js index 372a869..11ffc69 100644 --- a/src/config/loader.js +++ b/src/config/loader.js @@ -8,6 +8,8 @@ const address = c?.network?.address || process.env.ADDRESS || "0.0.0.0"; const port = c?.network?.port || process.env.SERVER_PORT || 3400; const logDir = c?.logging?.log_dir || process.env.LOG_DIR; + const dbPath = c?.logging?.db_path || process.env.LOGS_DB_PATH; + if (logDir == undefined) { throw new Error("Log dir is undefined"); } @@ -22,7 +24,8 @@ logging: { logDir, logLevel: c?.logging?.log_level || process.env.LOG_LEVEL || "info", - logsDbPath: c?.logging?.db_path || process.env.LOGS_DB_PATH, + dbPath, + getDBFile: (file) => path.join(dbPath, file), }, public: { schema: c?.public?.schema || process.env.SERVER_SCHEMA || schema, diff --git a/src/controllers/secured/logsController.js b/src/controllers/secured/logsController.js index 6f0aa5d..edf634b 100644 --- a/src/controllers/secured/logsController.js +++ b/src/controllers/secured/logsController.js @@ -1,4 +1,3 @@ -const path = require("path"); const fs = require("fs"); const Database = require("better-sqlite3"); const { winstonLogger } = require("../../utils/logging"); @@ -16,13 +15,13 @@ "functions", "notice", ]; -const { logsDbPath } = logging; +const dbFile = logging.getDBFile("logs.sqlite3"); -if (!fs.existsSync(logsDbPath)) { - fs.closeSync(fs.openSync(logsDbPath, "w")); +if (!fs.existsSync(dbFile)) { + fs.closeSync(fs.openSync(dbFile, "w")); } -const logsDb = new Database(logsDbPath, { readonly: true }); +const logsDb = new Database(dbFile, { readonly: true }); exports.renderLogsPage = (req, res) => { res.renderWithBaseContext("admin-pages/logs", { diff --git a/src/utils/SQLiteTransport.js b/src/utils/SQLiteTransport.js index f382fb7..f805637 100644 --- a/src/utils/SQLiteTransport.js +++ b/src/utils/SQLiteTransport.js @@ -1,11 +1,12 @@ const Transport = require("winston-transport"); const Database = require("better-sqlite3"); -const path = require("path"); +const { logging } = require("../config/loader"); +const dbFile = logging.getDBFile("logs.sqlite3"); class SQLiteTransport extends Transport { constructor(opts) { super(opts); - this.db = new Database(path.resolve(__dirname, "../../data/logs.sqlite3")); + this.db = new Database(dbFile); this.db.exec(` CREATE TABLE IF NOT EXISTS logs ( @@ -25,7 +26,7 @@ FOREIGN KEY(log_id) REFERENCES logs(id) ON DELETE CASCADE, FOREIGN KEY(key_id) REFERENCES keys(id) ON DELETE CASCADE ); - + -- CRITICAL: These indexes will make your queries 100x faster CREATE INDEX IF NOT EXISTS idx_logs_timestamp_desc ON logs(timestamp DESC); CREATE INDEX IF NOT EXISTS idx_logs_level_timestamp ON logs(level, timestamp DESC); @@ -39,12 +40,12 @@ this.db.pragma("cache_size = 10000"); this.insertLog = this.db.prepare( - `INSERT INTO logs (timestamp, level) VALUES (?, ?)` + `INSERT INTO logs (timestamp, level) VALUES (?, ?)`, ); this.getKeyId = this.db.prepare(`SELECT id FROM keys WHERE key = ?`); this.insertKey = this.db.prepare(`INSERT INTO keys (key) VALUES (?)`); this.insertMetadata = this.db.prepare( - `INSERT INTO log_metadata (log_id, key_id, value) VALUES (?, ?, ?)` + `INSERT INTO log_metadata (log_id, key_id, value) VALUES (?, ?, ?)`, ); } @@ -93,7 +94,7 @@ this.insertMetadata.run( logId, messageKeyId, - this.safeStringify(message) + this.safeStringify(message), ); } diff --git a/src/utils/sqlite3.js b/src/utils/sqlite3.js index 411ad04..54abb4f 100644 --- a/src/utils/sqlite3.js +++ b/src/utils/sqlite3.js @@ -1,6 +1,8 @@ const sqlite3 = require("sqlite3").verbose(); +const { logging } = require("../../config/loader"); +const dbFile = logging.getDBFile("analytic.sqlite3"); -const db = new sqlite3.Database("./data/analytic2.sqlite3"); +const db = new sqlite3.Database(dbFile); db.run(` CREATE TABLE IF NOT EXISTS analytics ( id INTEGER PRIMARY KEY AUTOINCREMENT,