add statistics to home page, updates

This commit is contained in:
Aidan 2024-12-31 20:11:41 -05:00
parent a6199d84ca
commit 19d96f65dd
5 changed files with 59 additions and 3 deletions

View File

@ -7,3 +7,4 @@ SESSION_SECRET=secretkeyhere
ADMIN_USERNAME=admin ADMIN_USERNAME=admin
ADMIN_PASSWORD=admin ADMIN_PASSWORD=admin
INTERNAL_PORT=3000 INTERNAL_PORT=3000
MC_API_KEY=mailcowapikeyhere

View File

@ -65,3 +65,4 @@ You will now have a fully functioning Node.js Express server, which will be runn
# To-Do # To-Do
- [ ] Port to NextJS? - [ ] Port to NextJS?
- [ ] Hovering effects for buttons - [ ] Hovering effects for buttons
- [ ] Statistics animation

40
app.js
View File

@ -4,6 +4,9 @@ const path = require('path');
const fs = require('fs'); const fs = require('fs');
const session = require('express-session'); const session = require('express-session');
const { Sequelize, DataTypes } = require('sequelize'); const { Sequelize, DataTypes } = require('sequelize');
const axios = require('axios');
const NodeCache = require('node-cache');
const cache = new NodeCache({ stdTTL: 1800 });
require('dotenv').config(); require('dotenv').config();
const app = express(); const app = express();
@ -40,11 +43,44 @@ const Request = sequelize.define('Request', {
} }
}); });
async function fetchDomainData() {
const cachedData = cache.get('domainData');
if (cachedData) {
console.log(domainData);
return cachedData;
}
try {
const response = await axios.get('https://user.p0ntus.com/api/v1/get/domain/all', {
headers: {
'accept': 'application/json',
'X-API-Key': process.env.MC_API_KEY
}
});
const domainData = response.data;
cache.set('domainData', domainData);
return domainData;
} catch (error) {
console.error('Error fetching domain data:', error);
return [];
}
}
// Sync DB models // Sync DB models
sequelize.sync(); sequelize.sync();
app.get('/', (req, res) => { app.get('/', async (req, res) => {
res.render('index', { currentPage: 'home' }); const domainData = await fetchDomainData();
const domainCount = Array.isArray(domainData) ? domainData.length : 0;
const accountCount = Array.isArray(domainData) ? domainData.reduce((acc, domain) => acc + domain.mboxes_in_domain, 0) : 0;
const totalData = Array.isArray(domainData) ? domainData.reduce((acc, domain) => acc + parseInt(domain.bytes_total), 0) / (1024 * 1024) : 0;
res.render('index', {
currentPage: 'home',
domainCount,
accountCount,
totalData: totalData.toFixed(2) // Round to 2 decimal places
});
}); });
app.get('/services', (req, res) => { app.get('/services', (req, res) => {

View File

@ -10,6 +10,7 @@
"author": "ihatenodejs", "author": "ihatenodejs",
"license": "CC0-1.0", "license": "CC0-1.0",
"dependencies": { "dependencies": {
"axios": "^1.7.9",
"body-parser": "^1.20.3", "body-parser": "^1.20.3",
"dotenv": "^16.4.7", "dotenv": "^16.4.7",
"ejs": "^3.1.10", "ejs": "^3.1.10",
@ -17,6 +18,7 @@
"express-session": "^1.18.1", "express-session": "^1.18.1",
"mariadb": "^3.4.0", "mariadb": "^3.4.0",
"mysql2": "^3.11.5", "mysql2": "^3.11.5",
"node-cache": "^5.1.2",
"sequelize": "^6.37.5", "sequelize": "^6.37.5",
"winston": "^3.17.0" "winston": "^3.17.0"
}, },

View File

@ -21,5 +21,21 @@
<a href="/guides" class="btn bg-dark text-white mt-2"><i class="fa-solid fa-book ico-sm"></i> Guides</a> <a href="/guides" class="btn bg-dark text-white mt-2"><i class="fa-solid fa-book ico-sm"></i> Guides</a>
</div> </div>
</div> </div>
<i class="il mt-5">Statistics</i>
<hr>
<div class="row mt-5">
<div class="col-md-4 text-center">
<h3><%= domainCount %></h3>
<p>Domains Hosted</p>
</div>
<div class="col-md-4 text-center">
<h3><%= accountCount %></h3>
<p>Accounts</p>
</div>
<div class="col-md-4 text-center">
<h3><%= totalData %></h3>
<p>Total Data (MB)</p>
</div>
</div>
</div> </div>
<%- include('shards/footer') %> <%- include('shards/footer') %>