From 719a957f60e1a65a6e4a84434190fe4069237337 Mon Sep 17 00:00:00 2001 From: lou Date: Tue, 15 Oct 2024 16:35:07 -0400 Subject: [PATCH] add support for analytics --- app.js | 111 +++++++++++++++++++++++++++++++++++---------- views/about.ejs | 3 ++ views/contact.ejs | 4 +- views/index.ejs | 2 +- views/projects.ejs | 3 ++ views/status.ejs | 4 +- views/verify.ejs | 4 +- 7 files changed, 103 insertions(+), 28 deletions(-) diff --git a/app.js b/app.js index 1041367..57a99ad 100644 --- a/app.js +++ b/app.js @@ -1,43 +1,106 @@ const express = require('express'); const path = require('path'); +const mysql = require('mysql2'); +const fs = require('fs'); +require('dotenv').config(); const app = express(); - app.set('view engine', 'ejs'); - app.set('views', path.join(__dirname, 'views')); - app.use(express.static(path.join(__dirname, 'public'))); +app.use(express.json()); -app.get('/', (req, res) => { - res.render('index', { req: req }); +const db = mysql.createConnection({ + host: process.env.DB_HOST, + user: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_NAME, }); -app.get('/about', (req, res) => { - res.render('about', { req: req }); +const dbInit = () => { + if (!fs.existsSync('.db_init')) { + console.log('.db_init not found, initializing db...'); + + const createVT = ` + CREATE TABLE page_views ( + page VARCHAR(255) PRIMARY KEY, + views INT DEFAULT 0 + )`; + + db.query(createVT, (error) => { + if (error) throw error; + console.log('Views table created successfully.'); + insertPages(); + fs.writeFileSync('.db_init', 'complete'); + }); + + db.query(`SHOW TABLES LIKE 'page_views'`, (err, results) => { + if (err) throw err; + + if (results.length === 0) { + const createVT = ` + CREATE TABLE page_views ( + page VARCHAR(255) PRIMARY KEY, + views INT DEFAULT 0 + )`; + + db.query(createVT, (error) => { + if (error) throw error; + console.log('Views table created successfully.'); + insertPages(); + fs.writeFileSync('.db_init', 'complete'); + }); + } else { + console.log('Views table already exists, skipping.'); + insertPages(); + } + }); + } else { + console.log('.db_init file found, skipping database initialization.'); + } +}; + +const insertPages = () => { + const pages = ['/', '/about', '/contact', '/verify', '/status', '/design', '/projects']; + pages.forEach(page => { + const sql = `INSERT IGNORE INTO page_views (page, views) VALUES (?, 0)`; + db.query(sql, [page], (error) => { + if (error) throw error; + }); + }); +}; + +db.connect((err) => { + if (err) throw err; + console.log('Connected to database.'); + dbInit(); }); -app.get('/contact', (req, res) => { - res.render('contact', { req: req }); +app.post('/api/log-view', (req, res) => { + const { page } = req.body; + const sql = `UPDATE page_views SET views = views + 1 WHERE page = ?`; + db.query(sql, [page], (error) => { + if (error) return res.status(500).json({ error }); + + const selectSql = `SELECT views FROM page_views WHERE page = ?`; + db.query(selectSql, [page], (error, results) => { + if (error) return res.status(500).json({ error }); + res.json({ views: results[0].views }); + }); + }); }); -app.get('/verify', (req, res) => { - res.render('verify', { req: req }); -}); - -app.get('/status', (req, res) => { - res.render('status', { req: req }); -}); - -app.get('/design', (req, res) => { - res.render('design', { req: req }); -}); - -app.get('/projects', (req, res) => { - res.render('projects', { req: req }); +const routes = ['/', '/about', '/contact', '/verify', '/status', '/design', '/projects']; +routes.forEach(route => { + app.get(route, (req, res) => { + res.render(route === '/' ? 'index' : route.slice(1), { req }); + }); }); const PORT = process.env.PORT || 3000; app.listen(PORT, () => { + const now = new Date(); + const fT = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`; + console.log(`Running at: ${fT}`); console.log(`Server running on http://localhost:${PORT}`); -}); \ No newline at end of file +}); diff --git a/views/about.ejs b/views/about.ejs index ce47c4f..f3cd654 100644 --- a/views/about.ejs +++ b/views/about.ejs @@ -69,5 +69,8 @@

You can learn more about my opinions on cloud platforms on this page.

+
+ <%- include('shards/views.ejs') %> +
<%- include('shards/footer.ejs') %> \ No newline at end of file diff --git a/views/contact.ejs b/views/contact.ejs index e4f01ed..1bb1f6b 100644 --- a/views/contact.ejs +++ b/views/contact.ejs @@ -44,6 +44,8 @@ - +
+ <%- include('shards/views.ejs') %> +
<%- include('shards/footer.ejs') %> \ No newline at end of file diff --git a/views/index.ejs b/views/index.ejs index 2809a3a..d9605bc 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -47,7 +47,7 @@

Some of my favorite artists include tobi lou, Skizzy Mars, and Kali Uchis. Some of my classics are Mike Stud (before his name change to "mike."), Skizzy Mars, tobi lou, The Neighbourhood, and Ryan Caraveo.

- +<%- include('shards/views.ejs') %> <%- include('shards/footer.ejs') %> \ No newline at end of file diff --git a/views/projects.ejs b/views/projects.ejs index 7d59bfc..6bcb97f 100644 --- a/views/projects.ejs +++ b/views/projects.ejs @@ -29,5 +29,8 @@

Finished projects

I currently have only one public project which is in finished state. It's my old website, which is now being hosted at old.aidxn.fun. It features a 2000s design and flashy graphics!

+
+ <%- include('shards/views.ejs') %> +
<%- include('shards/footer.ejs') %> \ No newline at end of file diff --git a/views/status.ejs b/views/status.ejs index b48a2bd..a66339a 100644 --- a/views/status.ejs +++ b/views/status.ejs @@ -28,7 +28,9 @@

[UNKNOWN - LOADING ms] kantor.aidxn.fun (Oracle Cloud - Germany - backend server)

[UNKNOWN - LOADING ms] api.aidxn.fun (Oracle Cloud - Germany - backend server)

- +
+ <%- include('shards/views.ejs') %> +
<%- include('shards/footer.ejs') %> \ No newline at end of file diff --git a/views/verify.ejs b/views/verify.ejs index f2dd2db..2decbc2 100644 --- a/views/verify.ejs +++ b/views/verify.ejs @@ -26,6 +26,8 @@

Check Message

THIS FORM IS NOT CURRENTLY AVAILABLE

- +
+ <%- include('shards/views.ejs') %> +
<%- include('shards/footer.ejs') %> \ No newline at end of file