Update configuration and enhance system stats route
- Updated database credentials and application name in config.json - Refactored database connection to use config values - Improved CPU usage calculation in systemStats route using node-os-utils - Added new actions route with user permission checks and config retrieval - Introduced example_config.json for reference
This commit is contained in:
parent
837ea87540
commit
51a73350ae
@ -2,10 +2,11 @@
|
||||
"serverPort": 8080,
|
||||
"databaseHost": "localhost",
|
||||
"databaseUser": "root",
|
||||
"databasePassword": "",
|
||||
"database": "test",
|
||||
"databasePassword": "1234",
|
||||
"database": "app",
|
||||
"databasePort": 3306,
|
||||
"developerMode": true,
|
||||
"developerLogin": true,
|
||||
"forceDevLogin": true
|
||||
}
|
||||
"forceDevLogin": true,
|
||||
"accentColor": "#00000"
|
||||
}
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
const mysql = require('mysql2');
|
||||
const config = require('../config.json');
|
||||
|
||||
const db = mysql.createConnection({
|
||||
host: 'localhost',
|
||||
user: 'root',
|
||||
password: '1234',
|
||||
database: 'app'
|
||||
host: config.databaseHost,
|
||||
user: config.databaseUser,
|
||||
password: config.databasePassword,
|
||||
database: config.database
|
||||
});
|
||||
|
||||
db.connect((err) => {
|
||||
|
||||
11
example_config.json
Normal file
11
example_config.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"serverPort": 8080,
|
||||
"databaseHost": "localhost",
|
||||
"databaseUser": "root",
|
||||
"databasePassword": "1234",
|
||||
"database": "app",
|
||||
"databasePort": 3306,
|
||||
"developerMode": true,
|
||||
"developerLogin": true,
|
||||
"forceDevLogin": true
|
||||
}
|
||||
57
routes/actions.js
Normal file
57
routes/actions.js
Normal file
@ -0,0 +1,57 @@
|
||||
const express = require('express');
|
||||
const db = require('../db/connection');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Define an action
|
||||
router.use('/userAction', (req, res) => {
|
||||
|
||||
const result = req.headers['x-username'];
|
||||
|
||||
console.log(req.headers['x-username']);
|
||||
if (!result) {
|
||||
return res.status(400).json({ error: "x-username header is required" });
|
||||
}
|
||||
const pre_username = JSON.parse(result);
|
||||
const username = pre_username.token;
|
||||
// Check if user exists and has permissions
|
||||
console.log(username);
|
||||
const query = 'SELECT perms FROM users WHERE username = ?';
|
||||
db.query(query, [username], (err, results) => {
|
||||
if (err) {
|
||||
console.error('Database error:', err);
|
||||
return res.status(500).json({ error: "Internal server error" });
|
||||
}
|
||||
|
||||
if (results.length === 0) {
|
||||
return res.status(401).json({ error: "Unauthorized" });
|
||||
}
|
||||
|
||||
const perms = results[0].perms;
|
||||
if (perms !== 'admin') {
|
||||
return res.status(403).json({ error: "Insufficient permissions" });
|
||||
}
|
||||
|
||||
if (req.method === 'GET') {
|
||||
try {
|
||||
const configData = fs.readFileSync('config.json', 'utf8');
|
||||
if (!configData) {
|
||||
return res.status(500).json({ error: "Config file is empty" });
|
||||
}
|
||||
const config = JSON.parse(configData);
|
||||
res.json(config);
|
||||
} catch (error) {
|
||||
console.error('Error reading config:', error);
|
||||
res.status(500).json({ error: "Failed to read config" });
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
// Define a root action
|
||||
router.get('/rootAction', (req, res) => {
|
||||
res.send('This is the root action');
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@ -25,7 +25,7 @@ router.post('/', (req, res) => {
|
||||
}
|
||||
|
||||
res.json({
|
||||
token: username,
|
||||
token: user.token,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,44 +1,32 @@
|
||||
const express = require('express');
|
||||
const os = require('os');
|
||||
const osu = require('node-os-utils')
|
||||
const cpu = osu.cpu
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', (req, res) => {
|
||||
// Get CPU usage
|
||||
const cpus = os.cpus();
|
||||
let totalIdle = 0;
|
||||
let totalTick = 0;
|
||||
const count = cpu.count()
|
||||
|
||||
cpus.forEach(cpu => {
|
||||
// Calculate idle time and total time for each CPU core
|
||||
const idle = cpu.times.idle;
|
||||
const user = cpu.times.user;
|
||||
const nice = cpu.times.nice;
|
||||
const sys = cpu.times.sys;
|
||||
const irq = cpu.times.irq;
|
||||
|
||||
// Calculate total time by summing all CPU states
|
||||
const total = user + nice + sys + idle + irq;
|
||||
|
||||
// Add to running totals to get average across all cores
|
||||
totalTick += total;
|
||||
totalIdle += idle;
|
||||
});
|
||||
cpu.usage()
|
||||
.then(cpuPercentage => {
|
||||
|
||||
// Get memory usage
|
||||
const totalMemory = os.totalmem() / 1024 / 1024 / 1024;
|
||||
const freeMemory = os.freemem() / 1024 / 1024 / 1024;
|
||||
const usedMemory = totalMemory - freeMemory;
|
||||
const memoryUsage = (usedMemory / totalMemory * 100);
|
||||
|
||||
const cpuUsage = 100 - (totalIdle / totalTick * 100);
|
||||
|
||||
// Get memory usage
|
||||
const totalMemory = os.totalmem() / 1024 / 1024 / 1024;
|
||||
|
||||
const freeMemory = os.freemem() / 1024 / 1024 / 1024;
|
||||
const usedMemory = totalMemory - freeMemory;
|
||||
const memoryUsage = (usedMemory / totalMemory * 100);
|
||||
|
||||
res.json({
|
||||
cpu: Math.round(cpuUsage * 100) / 100,
|
||||
memory: Math.round(memoryUsage * 100) / 100,
|
||||
totalMemory: totalMemory.toFixed(2),
|
||||
freeMemory: freeMemory.toFixed(2)
|
||||
});
|
||||
res.json({
|
||||
cpu: cpuPercentage,
|
||||
memory: Math.round(memoryUsage * 100) / 100,
|
||||
totalMemory: totalMemory.toFixed(2),
|
||||
freeMemory: freeMemory.toFixed(2)
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
res.status(500).json({ error: 'Failed to get CPU usage' });
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@ -7,6 +7,7 @@ const port = config.serverPort;
|
||||
const systemStatsRouter = require('./routes/systemStats');
|
||||
const loginRouter = require('./routes/login');
|
||||
const configRouter = require('./routes/config');
|
||||
const actionsRouter = require('./routes/actions');
|
||||
|
||||
const app = express();
|
||||
|
||||
@ -16,7 +17,8 @@ app.use(express.json());
|
||||
app.use('/system-stats', systemStatsRouter);
|
||||
app.use('/login', loginRouter);
|
||||
app.use('/config', configRouter);
|
||||
app.use('/actions', actionsRouter);
|
||||
|
||||
app.listen(port, () => {
|
||||
app.listen(port, () => {
|
||||
console.log(`Server is running on port ${port}`);
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user