| | const http = require('http'); |
| | const fs = require('fs'); |
| | const path = require('path'); |
| |
|
| | |
| | const PORT = 8081; |
| |
|
| | |
| | const MIME_TYPES = { |
| | '.html': 'text/html', |
| | '.css': 'text/css', |
| | '.js': 'text/javascript', |
| | '.json': 'application/json', |
| | '.png': 'image/png', |
| | '.jpg': 'image/jpeg', |
| | '.gif': 'image/gif', |
| | '.svg': 'image/svg+xml', |
| | '.ico': 'image/x-icon' |
| | }; |
| |
|
| | |
| | function serveStaticFile(res, pathname, contentType) { |
| | |
| | if (pathname === '/') { |
| | pathname = '/index.html'; |
| | } |
| | |
| | let filePath; |
| | |
| | |
| | if (pathname.startsWith('/comic_')) { |
| | |
| | filePath = path.join(process.cwd(), pathname); |
| | } else { |
| | |
| | filePath = path.join(process.cwd(), 'web', pathname); |
| | } |
| | |
| | |
| | fs.access(filePath, fs.constants.F_OK, (err) => { |
| | if (err) { |
| | |
| | res.writeHead(404, { 'Content-Type': 'text/html' }); |
| | res.end('<h1>404 Not Found</h1><p>O arquivo solicitado n茫o foi encontrado.</p>'); |
| | return; |
| | } |
| | |
| | |
| | fs.readFile(filePath, (err, data) => { |
| | if (err) { |
| | |
| | res.writeHead(500, { 'Content-Type': 'text/html' }); |
| | res.end('<h1>500 Internal Server Error</h1><p>Ocorreu um erro ao ler o arquivo.</p>'); |
| | return; |
| | } |
| | |
| | |
| | res.writeHead(200, { 'Content-Type': contentType }); |
| | res.end(data); |
| | }); |
| | }); |
| | } |
| |
|
| | |
| | function generateComicStory(theme, title, res) { |
| | const { spawn } = require('child_process'); |
| | |
| | |
| | const pythonProcess = spawn('python3', ['comic_story_generator.py', theme, title], { |
| | cwd: process.cwd() |
| | }); |
| | |
| | let output = ''; |
| | let errorOutput = ''; |
| | |
| | |
| | pythonProcess.stdout.on('data', (data) => { |
| | output += data.toString(); |
| | }); |
| | |
| | |
| | pythonProcess.stderr.on('data', (data) => { |
| | errorOutput += data.toString(); |
| | }); |
| | |
| | |
| | pythonProcess.on('close', (code) => { |
| | if (code === 0) { |
| | |
| | |
| | const directoryMatch = output.match(/Saving to directory: (comic_\d+)/); |
| | if (directoryMatch) { |
| | const directory = directoryMatch[1]; |
| | |
| | |
| | const viewerProcess = spawn('python3', ['comic_viewer.py', directory], { |
| | cwd: process.cwd() |
| | }); |
| | |
| | viewerProcess.on('close', (viewerCode) => { |
| | if (viewerCode === 0) { |
| | |
| | res.writeHead(200, { 'Content-Type': 'application/json' }); |
| | res.end(JSON.stringify({ |
| | success: true, |
| | directory: directory, |
| | message: 'Hist贸ria em quadrinhos gerada com sucesso!' |
| | })); |
| | } else { |
| | |
| | res.writeHead(500, { 'Content-Type': 'application/json' }); |
| | res.end(JSON.stringify({ |
| | success: false, |
| | error: 'Erro ao gerar o visualizador HTML', |
| | details: errorOutput |
| | })); |
| | } |
| | }); |
| | } else { |
| | |
| | res.writeHead(500, { 'Content-Type': 'application/json' }); |
| | res.end(JSON.stringify({ |
| | success: false, |
| | error: 'N茫o foi poss铆vel encontrar o diret贸rio da hist贸ria gerada', |
| | output: output |
| | })); |
| | } |
| | } else { |
| | |
| | res.writeHead(500, { 'Content-Type': 'application/json' }); |
| | res.end(JSON.stringify({ |
| | success: false, |
| | error: 'Erro ao gerar a hist贸ria em quadrinhos', |
| | details: errorOutput |
| | })); |
| | } |
| | }); |
| | } |
| |
|
| | |
| | const server = http.createServer((req, res) => { |
| | console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`); |
| | |
| | |
| | if (req.method === 'POST' && req.url === '/api/generate-comic') { |
| | let body = ''; |
| | |
| | |
| | req.on('data', chunk => { |
| | body += chunk.toString(); |
| | }); |
| | |
| | |
| | req.on('end', () => { |
| | try { |
| | const data = JSON.parse(body); |
| | const { theme, title } = data; |
| | |
| | |
| | if (!theme || !title) { |
| | res.writeHead(400, { 'Content-Type': 'application/json' }); |
| | res.end(JSON.stringify({ |
| | success: false, |
| | error: 'Tema e t铆tulo s茫o obrigat贸rios' |
| | })); |
| | return; |
| | } |
| | |
| | |
| | generateComicStory(theme, title, res); |
| | } catch (error) { |
| | res.writeHead(400, { 'Content-Type': 'application/json' }); |
| | res.end(JSON.stringify({ |
| | success: false, |
| | error: 'Dados inv谩lidos', |
| | details: error.message |
| | })); |
| | } |
| | }); |
| | |
| | return; |
| | } |
| | |
| | |
| | const extname = path.extname(req.url); |
| | const contentType = MIME_TYPES[extname] || 'text/html'; |
| | |
| | |
| | serveStaticFile(res, req.url, contentType); |
| | }); |
| |
|
| | |
| | server.listen(PORT, () => { |
| | console.log(`Servidor HD French Comic Generator rodando em http://localhost:${PORT}`); |
| | console.log('Pressione Ctrl+C para parar o servidor'); |
| | }); |
| |
|