diff --git a/backend/server.js b/backend/server.js index c410997..aaaff29 100644 --- a/backend/server.js +++ b/backend/server.js @@ -36,39 +36,84 @@ app.post('/api/config/hostinger-key', (req, res) => { res.json({ ok: true }); }); -// GET listar subdomínios via Hostinger API +// GET listar domínios via Hostinger API (Corrigido para v1/portfolio) app.get('/api/subdomains', async (req, res) => { const cfg = loadConfig(); + console.log('[BACKEND] Buscando domínios na Hostinger...'); if (!cfg.hostinger_api_key) return res.status(400).json({ error: 'API key não configurada' }); try { - const r = await fetch('https://developers.hostinger.com/api/dns/v1/domains', { - headers: { Authorization: `Bearer ${cfg.hostinger_api_key}`, 'Content-Type': 'application/json' } + const r = await fetch('https://developers.hostinger.com/api/domains/v1/portfolio', { + headers: { + 'Authorization': `Bearer ${cfg.hostinger_api_key}`, + 'Accept': 'application/json' + } }); + + console.log('[BACKEND] Status Hostinger:', r.status); + + if (!r.ok) { + const errText = await r.text(); + console.error('[BACKEND] Erro Hostinger:', errText); + return res.status(r.status).json({ error: `Hostinger Error: ${r.status}` }); + } + const data = await r.json(); + console.log('[BACKEND] Domínios encontrados:', Array.isArray(data) ? data.length : 'Não é array'); res.json(data); } catch (e) { + console.error('[BACKEND] Crash fetch:', e.message); res.status(500).json({ error: e.message }); } }); -// POST criar subdomínio no DNS da Hostinger +// POST criar subdomínio no DNS da Hostinger (Corrigido para v1/zones/{domain}) app.post('/api/subdomains', async (req, res) => { const cfg = loadConfig(); if (!cfg.hostinger_api_key) return res.status(400).json({ error: 'API key não configurada' }); const { domain, subdomain, type = 'A', value } = req.body; + console.log(`[BACKEND] Criando subdomínio ${subdomain}.${domain} -> ${value}`); + if (!domain || !subdomain || !value) return res.status(400).json({ error: 'domain, subdomain e value obrigatórios' }); try { - const r = await fetch(`https://developers.hostinger.com/api/dns/v1/domains/${domain}/records`, { - method: 'POST', - headers: { Authorization: `Bearer ${cfg.hostinger_api_key}`, 'Content-Type': 'application/json' }, - body: JSON.stringify({ type, name: subdomain, content: value, ttl: 300 }) + const r = await fetch(`https://developers.hostinger.com/api/dns/v1/zones/${domain}`, { + method: 'PUT', + headers: { + 'Authorization': `Bearer ${cfg.hostinger_api_key}`, + 'Content-Type': 'application/json', + 'Accept': 'application/json' + }, + body: JSON.stringify({ + overwrite: false, + zone: [ + { + name: subdomain, + type: type, + records: [ + { + content: value, + ttl: 300 + } + ] + } + ] + }) }); + + console.log('[BACKEND] Status Criação DNS:', r.status); + + if (!r.ok) { + const errText = await r.text(); + console.error('[BACKEND] Erro Criação DNS:', errText); + return res.status(r.status).json({ error: `Erro DNS: ${r.status}` }); + } + const data = await r.json(); - res.json(data); + res.json({ ok: true, data }); } catch (e) { + console.error('[BACKEND] Crash Criação DNS:', e.message); res.status(500).json({ error: e.message }); } });