diff --git a/analytics.js b/analytics.js new file mode 100644 index 0000000..b239b16 --- /dev/null +++ b/analytics.js @@ -0,0 +1,16 @@ +const pool = global.db_pool + +module.exports = (req, res, next) => { + next(); + const { path, method } = req; + + pool.query( + 'INSERT INTO analytics (method, endpoint, count) VALUES (?, ?, 1) ON DUPLICATE KEY UPDATE count = count + 1', + [method, path], + (error, results) => { + if (error) { + console.error('Error logging analytics:', error); + } + } + ); +} \ No newline at end of file diff --git a/index.js b/index.js index a8d3737..acfecab 100644 --- a/index.js +++ b/index.js @@ -77,6 +77,9 @@ app.use((req, res, next) => { next() }); +const analytics = require('./analytics.js'); +app.use(analytics); + // Rate Limit middleware from scratch const rateLimit = require('./rateLimit.js'); diff --git a/migrations/009_init_analytics_table.sql b/migrations/009_init_analytics_table.sql new file mode 100644 index 0000000..0c175b8 --- /dev/null +++ b/migrations/009_init_analytics_table.sql @@ -0,0 +1,7 @@ +CREATE TABLE analytics ( + id INT AUTO_INCREMENT PRIMARY KEY, + method VARCHAR(255) NOT NULL, + endpoint VARCHAR(255) NOT NULL, + UNIQUE (method, endpoint), + count INT NOT NULL DEFAULT 0 +); \ No newline at end of file diff --git a/routes/admin.js b/routes/admin.js index d155bdc..834ab2c 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -22,7 +22,6 @@ router.use(express.json()); router.use(express.urlencoded({ extended: true })); - router.use(expressSession({ store: expressSession.MemoryStore(), secret: process.env.SESSION_SECRET || 'default_secret',