initial commit
This commit is contained in:
138
backend/src/routes/logs.js
Normal file
138
backend/src/routes/logs.js
Normal file
@@ -0,0 +1,138 @@
|
||||
const express = require('express');
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const { SERVER_DIR } = require('../server');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
function isAuthenticated(req, res, next) {
|
||||
if (req.session.user) {
|
||||
next();
|
||||
} else {
|
||||
res.status(401).json({ error: 'Non authentifié' });
|
||||
}
|
||||
}
|
||||
|
||||
router.get('/', isAuthenticated, async (req, res) => {
|
||||
try {
|
||||
const { lines = 100 } = req.query;
|
||||
const logsDir = SERVER_DIR;
|
||||
|
||||
let logFile = path.join(logsDir, 'latest.log');
|
||||
|
||||
if (!await fs.pathExists(logFile)) {
|
||||
logFile = path.join(logsDir, 'ForgeModLoader-server-0.log');
|
||||
}
|
||||
|
||||
if (!await fs.pathExists(logFile)) {
|
||||
return res.json({ logs: [], message: 'Aucun fichier log trouvé' });
|
||||
}
|
||||
|
||||
const content = await fs.readFile(logFile, 'utf-8');
|
||||
const logLines = content.split('\n');
|
||||
const lastLines = logLines.slice(-parseInt(lines));
|
||||
|
||||
res.json({
|
||||
logs: lastLines,
|
||||
file: path.basename(logFile),
|
||||
totalLines: logLines.length
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Erreur logs:', error);
|
||||
res.status(500).json({ error: 'Erreur serveur' });
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/files', isAuthenticated, async (req, res) => {
|
||||
try {
|
||||
const logsDir = SERVER_DIR;
|
||||
const files = await fs.readdir(logsDir);
|
||||
|
||||
const logFiles = files.filter(f =>
|
||||
f.includes('log') || f.includes('Log')
|
||||
);
|
||||
|
||||
const filesWithStats = await Promise.all(
|
||||
logFiles.map(async (f) => {
|
||||
const stats = await fs.stat(path.join(logsDir, f));
|
||||
return {
|
||||
name: f,
|
||||
size: (stats.size / 1024).toFixed(2) + ' KB',
|
||||
modified: stats.mtime
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
res.json(filesWithStats.sort((a, b) => new Date(b.modified) - new Date(a.modified)));
|
||||
|
||||
} catch (error) {
|
||||
console.error('Erreur liste logs:', error);
|
||||
res.status(500).json({ error: 'Erreur serveur' });
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/file/:filename', isAuthenticated, async (req, res) => {
|
||||
try {
|
||||
const { filename } = req.params;
|
||||
const { lines = 100 } = req.query;
|
||||
|
||||
if (filename.includes('..')) {
|
||||
return res.status(400).json({ error: 'Accès non autorisé' });
|
||||
}
|
||||
|
||||
const filePath = path.join(SERVER_DIR, filename);
|
||||
|
||||
if (!await fs.pathExists(filePath)) {
|
||||
return res.status(404).json({ error: 'Fichier non trouvé' });
|
||||
}
|
||||
|
||||
const content = await fs.readFile(filePath, 'utf-8');
|
||||
const fileLines = content.split('\n');
|
||||
const lastLines = fileLines.slice(-parseInt(lines));
|
||||
|
||||
res.json({
|
||||
logs: lastLines,
|
||||
file: filename,
|
||||
totalLines: fileLines.length
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Erreur lecture log:', error);
|
||||
res.status(500).json({ error: 'Erreur serveur' });
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/search', isAuthenticated, async (req, res) => {
|
||||
try {
|
||||
const { query } = req.query;
|
||||
|
||||
if (!query || query.length < 2) {
|
||||
return res.status(400).json({ error: 'Requête trop courte' });
|
||||
}
|
||||
|
||||
let logFile = path.join(SERVER_DIR, 'latest.log');
|
||||
if (!await fs.pathExists(logFile)) {
|
||||
logFile = path.join(SERVER_DIR, 'ForgeModLoader-server-0.log');
|
||||
}
|
||||
|
||||
if (!await fs.pathExists(logFile)) {
|
||||
return res.json({ results: [] });
|
||||
}
|
||||
|
||||
const content = await fs.readFile(logFile, 'utf-8');
|
||||
const lines = content.split('\n');
|
||||
const results = lines
|
||||
.map((line, index) => ({ line, index }))
|
||||
.filter(({ line }) => line.toLowerCase().includes(query.toLowerCase()))
|
||||
.slice(-50);
|
||||
|
||||
res.json({ results });
|
||||
|
||||
} catch (error) {
|
||||
console.error('Erreur recherche:', error);
|
||||
res.status(500).json({ error: 'Erreur serveur' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user