// /media/BC_01C/@Docker_man/@Questionful_node/questionful/list_server.js
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
const PORT = 4002;

app.get('/api/folders', (req, res) => {
    const dir = req.query.dir || 'codes';
    const targetPath = path.join(__dirname, 'public', dir);

    if (!fs.existsSync(targetPath)) {
        return res.status(404).json({ error: `Le dossier "${dir}" n'existe pas !` });
    }

    const items = fs.readdirSync(targetPath);
    const folders = items.filter(item => {
        return fs.statSync(path.join(targetPath, item)).isDirectory();
    });

    const files = items.filter(item => {
        return fs.statSync(path.join(targetPath, item)).isFile();
    });

    res.json({ folders, files, currentDir: dir });
});

app.listen(PORT, () => {
    console.log(`Serveur de liste démarré sur http://localhost:${PORT}`);
});
