Initial commit: Backend with Bun.serve() + Drizzle ORM
- Bun 1.3.6 server setup - MariaDB schema (projects, agents, tasks) - Auto-migrations on startup - WebSocket support - Health check endpoint Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
84
src/index.ts
Normal file
84
src/index.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* AiWorker Backend - Main Entry Point
|
||||
* Using Bun.serve() native API
|
||||
*/
|
||||
|
||||
import { runMigrations } from './db/migrate'
|
||||
import { testConnection } from './db/client'
|
||||
|
||||
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/')) {
|
||||
return Response.json({
|
||||
message: 'AiWorker API',
|
||||
path: url.pathname,
|
||||
method: req.method,
|
||||
})
|
||||
}
|
||||
|
||||
// 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`)
|
||||
Reference in New Issue
Block a user