/** * AiWorker Backend - Main Entry Point * Using Bun.serve() native API */ import { runMigrations } from './db/migrate' import { testConnection } from './db/client' import { handleProjectRoutes, handleTaskRoutes, handleAgentRoutes } from './api/routes' console.log('🚀 Starting AiWorker Backend...') console.log(`Bun version: ${Bun.version}`) console.log(`Environment: ${process.env.NODE_ENV || 'development'}`) // Run migrations on startup await runMigrations() // Test database connection await testConnection() const PORT = process.env.PORT || 3000 // Health check route function handleHealthCheck() { return Response.json({ status: 'ok', timestamp: new Date().toISOString(), version: '1.0.0', bun: Bun.version, }) } // Main server const server = Bun.serve({ port: PORT, async fetch(req) { const url = new URL(req.url) // Health check if (url.pathname === '/api/health') { return handleHealthCheck() } // API routes if (url.pathname.startsWith('/api/projects')) { return handleProjectRoutes(req, url) } if (url.pathname.startsWith('/api/tasks')) { return handleTaskRoutes(req, url) } if (url.pathname.startsWith('/api/agents')) { return handleAgentRoutes(req, url) } // Generic API info if (url.pathname.startsWith('/api/')) { return Response.json({ message: 'AiWorker API', version: '1.0.0', endpoints: [ 'GET /api/health', 'GET /api/projects', 'GET /api/projects/:id', 'POST /api/projects', 'PATCH /api/projects/:id', 'DELETE /api/projects/:id', 'GET /api/tasks', 'GET /api/tasks/:id', 'POST /api/tasks', 'PATCH /api/tasks/:id', 'POST /api/tasks/:id/respond', 'DELETE /api/tasks/:id', 'GET /api/agents', 'GET /api/agents/:id', 'POST /api/agents', 'PATCH /api/agents/:id', 'POST /api/agents/:id/heartbeat', 'DELETE /api/agents/:id', ], }) } // Root if (url.pathname === '/') { return new Response('AiWorker Backend - Running on Bun 🚀', { headers: { 'Content-Type': 'text/plain' } }) } // 404 return new Response('Not Found', { status: 404 }) }, // WebSocket support websocket: { open(ws) { console.log('WebSocket client connected') ws.send(JSON.stringify({ type: 'connected', timestamp: Date.now() })) }, message(ws, message) { console.log('WebSocket message:', message) // Echo back for now ws.send(message) }, close(ws) { console.log('WebSocket client disconnected') }, }, // Development mode development: process.env.NODE_ENV === 'development', }) console.log(`✅ Server listening on http://localhost:${server.port}`) console.log(`📊 Health check: http://localhost:${server.port}/api/health`)