add statistics to home page, updates
This commit is contained in:
parent
a6199d84ca
commit
19d96f65dd
@ -6,4 +6,5 @@ DB_PORT=3306
|
||||
SESSION_SECRET=secretkeyhere
|
||||
ADMIN_USERNAME=admin
|
||||
ADMIN_PASSWORD=admin
|
||||
INTERNAL_PORT=3000
|
||||
INTERNAL_PORT=3000
|
||||
MC_API_KEY=mailcowapikeyhere
|
@ -65,3 +65,4 @@ You will now have a fully functioning Node.js Express server, which will be runn
|
||||
# To-Do
|
||||
- [ ] Port to NextJS?
|
||||
- [ ] Hovering effects for buttons
|
||||
- [ ] Statistics animation
|
40
app.js
40
app.js
@ -4,6 +4,9 @@ const path = require('path');
|
||||
const fs = require('fs');
|
||||
const session = require('express-session');
|
||||
const { Sequelize, DataTypes } = require('sequelize');
|
||||
const axios = require('axios');
|
||||
const NodeCache = require('node-cache');
|
||||
const cache = new NodeCache({ stdTTL: 1800 });
|
||||
|
||||
require('dotenv').config();
|
||||
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
|
||||
sequelize.sync();
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.render('index', { currentPage: 'home' });
|
||||
app.get('/', async (req, res) => {
|
||||
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) => {
|
||||
|
@ -10,6 +10,7 @@
|
||||
"author": "ihatenodejs",
|
||||
"license": "CC0-1.0",
|
||||
"dependencies": {
|
||||
"axios": "^1.7.9",
|
||||
"body-parser": "^1.20.3",
|
||||
"dotenv": "^16.4.7",
|
||||
"ejs": "^3.1.10",
|
||||
@ -17,6 +18,7 @@
|
||||
"express-session": "^1.18.1",
|
||||
"mariadb": "^3.4.0",
|
||||
"mysql2": "^3.11.5",
|
||||
"node-cache": "^5.1.2",
|
||||
"sequelize": "^6.37.5",
|
||||
"winston": "^3.17.0"
|
||||
},
|
||||
|
@ -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>
|
||||
</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>
|
||||
<%- include('shards/footer') %>
|
Reference in New Issue
Block a user