Spaces:
Running
Running
| const express = require('express'); | |
| const path = require('path'); | |
| const cors = require('cors'); | |
| const app = express(); | |
| const PORT = process.env.PORT || 3000; | |
| // Simple in-memory rate limit per IP (token bucket) | |
| const buckets = new Map(); | |
| function rateLimit(req, res, next) { | |
| const ip = req.ip || req.connection.remoteAddress || 'unknown'; | |
| const now = Date.now(); | |
| const cap = 100; // tokens | |
| const refill = 50; // tokens per minute | |
| const b = buckets.get(ip) || { tokens: cap, last: now }; | |
| const elapsed = (now - b.last) / 1000 / 60; // minutes | |
| b.tokens = Math.min(cap, b.tokens + elapsed * refill); | |
| b.last = now; | |
| if (b.tokens < 1) { | |
| return res.status(429).json({ error: 'Too many requests. Please slow down.' }); | |
| } | |
| b.tokens -= 1; | |
| buckets.set(ip, b); | |
| next(); | |
| } | |
| // Middleware | |
| app.use(cors()); | |
| app.use(express.json({ limit: '2mb' })); | |
| // Health check | |
| app.get('/api/health', (req, res) => { | |
| res.json({ | |
| ok: true, | |
| time: Date.now(), | |
| service: 'VibeCode Local AI', | |
| version: '2.0.0' | |
| }); | |
| }); | |
| // Template generation endpoint | |
| app.post('/api/generate', rateLimit, async (req, res) => { | |
| try { | |
| const { prompt, template, style } = req.body || {}; | |
| if (!prompt || !template) { | |
| return res.status(400).json({ | |
| error: 'Missing required parameters: prompt, template' | |
| }); | |
| } | |
| // Template configurations | |
| const templates = { | |
| vibecode: { | |
| name: "VibeCode App", | |
| description: "Modern web app with vibecoding aesthetics", | |
| structure: { | |
| html: `<!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>VibeApp</title> | |
| <script src="https://cdn.tailwindcss.com"></script> | |
| <style> | |
| .gradient-bg { | |
| background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); | |
| } | |
| .glass { | |
| backdrop-filter: blur(16px) saturate(180%); | |
| background-color: rgba(255, 255, 255, 0.75); | |
| border: 1px solid rgba(255, 255, 255, 0.125); | |
| } | |
| .neon { | |
| text-shadow: 0 0 10px currentColor; | |
| } | |
| @keyframes float { | |
| 0%, 100% { transform: translateY(0px); } | |
| 50% { transform: translateY(-10px); } | |
| } | |
| .float { animation: float 6s ease-in-out infinite; } | |
| </style> | |
| </head> | |
| <body class="gradient-bg min-h-screen"> | |
| <div class="container mx-auto px-4 py-8"> | |
| <header class="text-center mb-12"> | |
| <h1 class="text-4xl font-bold text-white mb-4 neon float">${prompt}</h1> | |
| <p class="text-white/80">Modern vibes, clean code, smooth UX</p> | |
| </header> | |
| <div class="grid md:grid-cols-3 gap-6"> | |
| <div class="glass rounded-lg p-6 shadow-lg hover:shadow-xl transition"> | |
| <h3 class="text-lg font-semibold mb-3">Dashboard</h3> | |
| <p class="text-sm opacity-80">Clean overview of your data</p> | |
| </div> | |
| <div class="glass rounded-lg p-6 shadow-lg hover:shadow-xl transition"> | |
| <h3 class="text-lg font-semibold mb-3">Analytics</h3> | |
| <p class="text-sm opacity-80">Real-time insights</p> | |
| </div> | |
| <div class="glass rounded-lg p-6 shadow-lg hover:shadow-xl transition"> | |
| <h3 class="text-lg font-semibold mb-3">Settings</h3> | |
| <p class="text-sm opacity-80">Configure your vibe</p> | |
| </div> | |
| </div> | |
| </div> | |
| </body> | |
| </html>`, | |
| js: `// VibeApp JavaScript | |
| console.log('${prompt} loaded!'); | |
| document.addEventListener('DOMContentLoaded', function() { | |
| // Add smooth animations | |
| const cards = document.querySelectorAll('.glass'); | |
| cards.forEach((card, index) => { | |
| card.style.opacity = '0'; | |
| card.style.transform = 'translateY(20px)'; | |
| setTimeout(() => { | |
| card.style.transition = 'all 0.5s ease'; | |
| card.style.opacity = '1'; | |
| card.style.transform = 'translateY(0)'; | |
| }, index * 100); | |
| }); | |
| // Add hover effects | |
| const hero = document.querySelector('.neon'); | |
| if (hero) { | |
| hero.addEventListener('mouseenter', function() { | |
| this.style.textShadow = '0 0 20px currentColor, 0 0 30px currentColor'; | |
| }); | |
| hero.addEventListener('mouseleave', function() { | |
| this.style.textShadow = '0 0 10px currentColor'; | |
| }); | |
| } | |
| });` | |
| } | |
| }, | |
| website: { | |
| name: "Clean Website", | |
| description: "Professional business website", | |
| structure: { | |
| html: `<!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Professional Website - ${prompt}</title> | |
| <script src="https://cdn.tailwindcss.com"></script> | |
| </head> | |
| <body class="bg-white"> | |
| <nav class="bg-white shadow-sm border-b"> | |
| <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8"> | |
| <div class="flex justify-between h-16"> | |
| <div class="flex items-center"> | |
| <h1 class="text-xl font-bold text-gray-900">YourBrand</h1> | |
| </div> | |
| <div class="hidden md:flex items-center space-x-8"> | |
| <a href="#" class="text-gray-600 hover:text-gray-900 transition">Home</a> | |
| <a href="#" class="text-gray-600 hover:text-gray-900 transition">About</a> | |
| <a href="#" class="text-gray-600 hover:text-gray-900 transition">Services</a> | |
| <a href="#" class="text-gray-600 hover:text-gray-900 transition">Contact</a> | |
| </div> | |
| </div> | |
| </div> | |
| </nav> | |
| <main> | |
| <section class="bg-gradient-to-r from-blue-600 to-purple-600 text-white"> | |
| <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-24"> | |
| <div class="text-center"> | |
| <h1 class="text-5xl font-bold mb-6">${prompt}</h1> | |
| <p class="text-xl mb-8">We create amazing digital experiences</p> | |
| <button class="bg-white text-blue-600 px-8 py-3 rounded-lg font-semibold hover:bg-gray-100 transition"> | |
| Get Started | |
| </button> | |
| </div> | |
| </div> | |
| </section> | |
| <section class="py-16"> | |
| <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8"> | |
| <div class="grid md:grid-cols-3 gap-8"> | |
| <div class="text-center"> | |
| <div class="w-16 h-16 bg-blue-100 rounded-full flex items-center justify-center mx-auto mb-4"> | |
| <svg class="w-8 h-8 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"> | |
| <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"></path> | |
| </svg> | |
| </div> | |
| <h3 class="text-lg font-semibold mb-2">Fast & Efficient</h3> | |
| <p class="text-gray-600">Lightning-fast performance and optimized workflows</p> | |
| </div> | |
| <div class="text-center"> | |
| <div class="w-16 h-16 bg-purple-100 rounded-full flex items-center justify-center mx-auto mb-4"> | |
| <svg class="w-8 h-8 text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"> | |
| <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path> | |
| </svg> | |
| </div> | |
| <h3 class="text-lg font-semibold mb-2">Quality Assured</h3> | |
| <p class="text-gray-600">Every project meets our highest standards</p> | |
| </div> | |
| <div class="text-center"> | |
| <div class="w-16 h-16 bg-green-100 rounded-full flex items-center justify-center mx-auto mb-4"> | |
| <svg class="w-8 h-8 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"> | |
| <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M18.364 5.636l-3.536 3.536m0 5.656l3.536 3.536M9.172 9.172L5.636 5.636m3.536 9.192L5.636 18.364M12 2.25a9.75 9.75 0 109.75 9.75A9.75 9.75 0 0012 2.25z"></path> | |
| </svg> | |
| </div> | |
| <h3 class="text-lg font-semibold mb-2">24/7 Support</h3> | |
| <p class="text-gray-600">Round-the-clock assistance when you need it</p> | |
| </div> | |
| </div> | |
| </div> | |
| </section> | |
| </main> | |
| </body> | |
| </html>`, | |
| js: `// Professional Website JavaScript | |
| document.addEventListener('DOMContentLoaded', function() { | |
| console.log('Professional website loaded successfully'); | |
| // Smooth scrolling for anchor links | |
| document.querySelectorAll('a[href^="#"]').forEach(anchor => { | |
| anchor.addEventListener('click', function (e) { | |
| e.preventDefault(); | |
| document.querySelector(this.getAttribute('href')).scrollIntoView({ | |
| behavior: 'smooth' | |
| }); | |
| }); | |
| }); | |
| // Add intersection observer for animations | |
| const observerOptions = { | |
| threshold: 0.1, | |
| rootMargin: '0px 0px -50px 0px' | |
| }; | |
| const observer = new IntersectionObserver(function(entries) { | |
| entries.forEach(entry => { | |
| if (entry.isIntersecting) { | |
| entry.target.style.opacity = '1'; | |
| entry.target.style.transform = 'translateY(0)'; | |
| } | |
| }); | |
| }, observerOptions); | |
| // Observe elements for animation | |
| document.querySelectorAll('.text-center').forEach(el => { | |
| el.style.opacity = '0'; | |
| el.style.transform = 'translateY(20px)'; | |
| el.style.transition = 'all 0.6s ease'; | |
| observer.observe(el); | |
| }); | |
| });` | |
| } | |
| }, | |
| saas: { | |
| name: "SaaS Dashboard", | |
| description: "Analytics & admin panel", | |
| structure: { | |
| html: `<!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>SaaS Dashboard - ${prompt}</title> | |
| <script src="https://cdn.tailwindcss.com"></script> | |
| <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> | |
| </head> | |
| <body class="bg-gray-50"> | |
| <div class="min-h-screen flex"> | |
| <!-- Sidebar --> | |
| <div class="w-64 bg-white shadow-sm"> | |
| <div class="p-6"> | |
| <h2 class="text-xl font-bold text-gray-800">Dashboard</h2> | |
| </div> | |
| <nav class="px-4 space-y-2"> | |
| <a href="#" class="block px-3 py-2 text-gray-600 rounded-md bg-blue-50 border-r-2 border-blue-500">Overview</a> | |
| <a href="#" class="block px-3 py-2 text-gray-600 hover:bg-gray-50 rounded-md">Analytics</a> | |
| <a href="#" class="block px-3 py-2 text-gray-600 hover:bg-gray-50 rounded-md">Users</a> | |
| <a href="#" class="block px-3 py-2 text-gray-600 hover:bg-gray-50 rounded-md">Settings</a> | |
| </nav> | |
| </div> | |
| <!-- Main Content --> | |
| <div class="flex-1"> | |
| <header class="bg-white shadow-sm border-b px-6 py-4"> | |
| <h1 class="text-2xl font-semibold text-gray-900">${prompt}</h1> | |
| <p class="text-gray-600">Welcome back! Here's what's happening with your business.</p> | |
| </header> | |
| <main class="p-6"> | |
| <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-8"> | |
| <div class="bg-white p-6 rounded-lg shadow-sm hover:shadow-md transition"> | |
| <div class="flex items-center justify-between"> | |
| <div> | |
| <h3 class="text-sm font-medium text-gray-500">Total Users</h3> | |
| <p class="text-2xl font-semibold text-gray-900">1,234</p> | |
| <span class="text-sm text-green-600">+12%</span> | |
| </div> | |
| <div class="w-12 h-12 bg-blue-100 rounded-full flex items-center justify-center"> | |
| <svg class="w-6 h-6 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"> | |
| <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197m13.5-9a2.5 2.5 0 11-5 0 2.5 2.5 0 015 0z"></path> | |
| </svg> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="bg-white p-6 rounded-lg shadow-sm hover:shadow-md transition"> | |
| <div class="flex items-center justify-between"> | |
| <div> | |
| <h3 class="text-sm font-medium text-gray-500">Revenue</h3> | |
| <p class="text-2xl font-semibold text-gray-900">$12,345</p> | |
| <span class="text-sm text-green-600">+8.2%</span> | |
| </div> | |
| <div class="w-12 h-12 bg-green-100 rounded-full flex items-center justify-center"> | |
| <svg class="w-6 h-6 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"> | |
| <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1"></path> | |
| </svg> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="bg-white p-6 rounded-lg shadow-sm hover:shadow-md transition"> | |
| <div class="flex items-center justify-between"> | |
| <div> | |
| <h3 class="text-sm font-medium text-gray-500">Growth</h3> | |
| <p class="text-2xl font-semibold text-gray-900">+23%</p> | |
| <span class="text-sm text-green-600">+4.1%</span> | |
| </div> | |
| <div class="w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center"> | |
| <svg class="w-6 h-6 text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"> | |
| <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 7h8m0 0v8m0-8l-8 8-4-4-6 6"></path> | |
| </svg> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="bg-white p-6 rounded-lg shadow-sm hover:shadow-md transition"> | |
| <div class="flex items-center justify-between"> | |
| <div> | |
| <h3 class="text-sm font-medium text-gray-500">Active Users</h3> | |
| <p class="text-2xl font-semibold text-gray-900">856</p> | |
| <span class="text-sm text-red-600">-2.1%</span> | |
| </div> | |
| <div class="w-12 h-12 bg-yellow-100 rounded-full flex items-center justify-center"> | |
| <svg class="w-6 h-6 text-yellow-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"> | |
| <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path> | |
| <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path> | |
| </svg> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="grid grid-cols-1 lg:grid-cols-2 gap-6"> | |
| <div class="bg-white rounded-lg shadow-sm p-6"> | |
| <h3 class="text-lg font-medium text-gray-900 mb-4">Revenue Chart</h3> | |
| <canvas id="revenueChart" width="400" height="200"></canvas> | |
| </div> | |
| <div class="bg-white rounded-lg shadow-sm p-6"> | |
| <h3 class="text-lg font-medium text-gray-900 mb-4">User Activity</h3> | |
| <canvas id="activityChart" width="400" height="200"></canvas> | |
| </div> | |
| </div> | |
| </main> | |
| </div> | |
| </div> | |
| </body> | |
| </html>`, | |
| js: `// SaaS Dashboard JavaScript | |
| document.addEventListener('DOMContentLoaded', function() { | |
| // Initialize revenue chart | |
| const ctx1 = document.getElementById('revenueChart').getContext('2d'); | |
| new Chart(ctx1, { | |
| type: 'line', | |
| data: { | |
| labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'], | |
| datasets: [{ | |
| label: 'Revenue', | |
| data: [12000, 19000, 15000, 25000, 22000, 30000], | |
| borderColor: 'rgb(59, 130, 246)', | |
| backgroundColor: 'rgba(59, 130, 246, 0.1)', | |
| tension: 0.4, | |
| fill: true | |
| }] | |
| }, | |
| options: { | |
| responsive: true, | |
| plugins: { | |
| legend: { | |
| display: false | |
| } | |
| }, | |
| scales: { | |
| y: { | |
| beginAtZero: true, | |
| ticks: { | |
| callback: function(value) { | |
| return ' + value.toLocaleString(); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| }); | |
| // Initialize activity chart | |
| const ctx2 = document.getElementById('activityChart').getContext('2d'); | |
| new Chart(ctx2, { | |
| type: 'doughnut', | |
| data: { | |
| labels: ['Desktop', 'Mobile', 'Tablet'], | |
| datasets: [{ | |
| data: [45, 35, 20], | |
| backgroundColor: [ | |
| 'rgba(59, 130, 246, 0.8)', | |
| 'rgba(16, 185, 129, 0.8)', | |
| 'rgba(245, 158, 11, 0.8)' | |
| ] | |
| }] | |
| }, | |
| options: { | |
| responsive: true, | |
| plugins: { | |
| legend: { | |
| position: 'bottom' | |
| } | |
| } | |
| } | |
| }); | |
| // Add real-time updates simulation | |
| function updateMetrics() { | |
| const metrics = document.querySelectorAll('.bg-white.p-6'); | |
| metrics.forEach(metric => { | |
| const value = metric.querySelector('.text-2xl'); | |
| if (value && Math.random() > 0.7) { | |
| // Simulate small changes | |
| const currentValue = parseFloat(value.textContent.replace(/[^0-9.]/g, '')); | |
| const change = (Math.random() - 0.5) * 0.1; | |
| const newValue = currentValue * (1 + change); | |
| value.textContent = value.textContent.replace(/\d+/, Math.round(newValue)); | |
| } | |
| }); | |
| } | |
| // Update metrics every 5 seconds | |
| setInterval(updateMetrics, 5000); | |
| });` | |
| } | |
| } | |
| }; | |
| const selectedTemplate = templates[template] || templates.vibecode; | |
| // Enhance based on style preferences | |
| let generatedHTML = selectedTemplate.structure.html; | |
| let generatedJS = selectedTemplate.structure.js; | |
| // Apply style modifications | |
| if (style) { | |
| if (style.includes('dark')) { | |
| generatedHTML = generatedHTML | |
| .replace(/bg-white/g, 'bg-gray-900') | |
| .replace(/text-gray-900/g, 'text-white') | |
| .replace(/text-gray-600/g, 'text-gray-300') | |
| .replace(/text-gray-500/g, 'text-gray-400') | |
| .replace(/bg-gray-50/g, 'bg-gray-800') | |
| .replace(/border-gray-200/g, 'border-gray-700') | |
| .replace(/shadow-sm/g, 'shadow-lg'); | |
| } | |
| if (style.includes('colorful') || style.includes('rainbow')) { | |
| generatedHTML = generatedHTML.replace( | |
| 'background: linear-gradient(135deg, #667eea 0%, #764ba2 100%)', | |
| 'background: linear-gradient(135deg, #ff6b6b, #4ecdc4, #45b7d1, #96ceb4, #feca57)' | |
| ).replace( | |
| 'from-blue-600 to-purple-600', | |
| 'from-red-500 via-yellow-500 to-green-500' | |
| ); | |
| } | |
| } | |
| const fullHTML = generatedHTML.replace('</body>', `<script>${generatedJS}</script></body>`); | |
| res.json({ | |
| success: true, | |
| template: selectedTemplate.name, | |
| html: generatedHTML, | |
| js: generatedJS, | |
| preview: fullHTML, | |
| description: selectedTemplate.description | |
| }); | |
| } catch (err) { | |
| console.error('Template generation error:', err); | |
| res.status(500).json({ error: 'Internal server error' }); | |
| } | |
| }); | |
| // Serve static files AFTER API routes to ensure API routes take precedence | |
| app.use(express.static(path.join(__dirname))); | |
| app.listen(PORT, () => { | |
| console.log(`VibeCode Local AI server running at http://localhost:${PORT}`); | |
| console.log('API routes registered:'); | |
| console.log(' GET /api/health'); | |
| console.log(' POST /api/generate'); | |
| }); | |