Files
aiworker-backend/src/index.ts
Hector Ros 5672127593
All checks were successful
Build and Push Backend / build (push) Successful in 5s
Implement Backend API, MCP Server, and Gitea integration
- Add REST API routes for projects, tasks, and agents (CRUD operations)
- Implement MCP Server with 4 core tools:
  - get_next_task: Assign tasks to agents
  - update_task_status: Update task states
  - create_branch: Create Git branches via Gitea API
  - create_pull_request: Create PRs via Gitea API
- Add Gitea API client for repository operations
- Fix database migration error handling for existing tables
- Connect all routes to Bun.serve() main server

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
2026-01-20 00:43:46 +01:00

118 lines
2.9 KiB
TypeScript

/**
* 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`)