import express from 'express' import cors from 'cors' import fs from 'fs' import path from 'path' import { fileURLToPath } from 'url' const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) const app = express() const PORT = 4000 const SESSIONS_PATH = path.join(__dirname, 'data') app.use(cors()) app.use(express.json()) if (!fs.existsSync(SESSIONS_PATH)) fs.mkdirSync(SESSIONS_PATH) app.get('/sessions', (req, res) => { const files = fs.readdirSync(SESSIONS_PATH) const sessions = files.map(file => { const content = fs.readFileSync(path.join(SESSIONS_PATH, file), 'utf-8') return { id: file.replace('.json', ''), messages: JSON.parse(content) } }) res.json(sessions) }) app.post('/sessions/:id', (req, res) => { const sessionId = req.params.id const messages = req.body.messages fs.writeFileSync(path.join(SESSIONS_PATH, `${sessionId}.json`), JSON.stringify(messages, null, 2)) res.json({ status: 'ok' }) }) app.listen(PORT, () => console.log(`✅ Sessões API rodando em http://localhost:${PORT}`))