const express = require('express'); const cors = require('cors'); const dotenv = require('dotenv'); const Groq = require('groq-sdk'); const { createClient } = require('@supabase/supabase-js'); const ws = require('ws'); // Fix for Supabase Realtime in Node < 22 global.WebSocket = ws; dotenv.config(); const app = express(); app.use(cors()); app.use(express.json()); app.use(express.static('./')); // Initialize Supabase const supabase = createClient( process.env.SUPABASE_URL, process.env.SUPABASE_ANON_KEY ); const groq = new Groq({ apiKey: process.env.GROQ_API_KEY }); const PORT = process.env.PORT || 7860; // API: Generate Gig Content app.post('/api/generate-gig', async (req, res) => { const { topic, category } = req.body; if (!topic) return res.status(400).json({ error: 'Topic is required' }); try { const prompt = `Create a professional freelance gig for a platform like Fiverr or Upwork. Topic: ${topic} Category: ${category} Provide: 1. A catchy title starting with "I will..." 2. A detailed, high-converting description (300-500 words). 3. 5 relevant tags. Format as JSON: { "title": "...", "description": "...", "tags": ["...", "..."] }`; const chatCompletion = await groq.chat.completions.create({ messages: [{ role: 'user', content: prompt }], model: 'llama3-8b-8192', response_format: { type: 'json_object' } }); const result = JSON.parse(chatCompletion.choices[0].message.content); res.json(result); } catch (error) { console.error('Groq Error:', error); res.status(500).json({ error: 'Failed to generate gig content' }); } }); // API: Healthcheck / Supabase Test app.get('/api/health', async (req, res) => { res.json({ status: 'ok', supabase: !!supabase }); }); app.listen(PORT, '0.0.0.0', () => { console.log(`🚀 GigSync AI Backend running on port ${PORT}`); });