(req: Request)
| 499 | * GET request handler for starting a Docker container |
| 500 | */ |
| 501 | export async function GET(req: Request) { |
| 502 | const { searchParams } = new URL(req.url); |
| 503 | const projectPath = searchParams.get('projectPath'); |
| 504 | |
| 505 | if (!projectPath) { |
| 506 | return NextResponse.json( |
| 507 | { error: 'Missing required parameters' }, |
| 508 | { status: 400 } |
| 509 | ); |
| 510 | } |
| 511 | |
| 512 | // Check if a container is already running |
| 513 | const existingContainer = runningContainers.get(projectPath); |
| 514 | if (existingContainer) { |
| 515 | // Verify container is still running |
| 516 | const isRunning = await checkContainerRunning( |
| 517 | existingContainer.containerId |
| 518 | ); |
| 519 | if (isRunning) { |
| 520 | return NextResponse.json({ |
| 521 | message: 'Docker container already running', |
| 522 | domain: existingContainer.domain, |
| 523 | containerId: existingContainer.containerId, |
| 524 | }); |
| 525 | } else { |
| 526 | // Remove non-running container from state |
| 527 | runningContainers.delete(projectPath); |
| 528 | if (existingContainer.port) { |
| 529 | allocatedPorts.delete(existingContainer.port); |
| 530 | } |
| 531 | await saveState(); |
| 532 | |
| 533 | logger.info( |
| 534 | `Container ${existingContainer.containerId} is no longer running, will create a new one` |
| 535 | ); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | // Prevent duplicate builds |
| 540 | if (processingRequests.has(projectPath)) { |
| 541 | return NextResponse.json({ |
| 542 | message: 'Container creation in progress', |
| 543 | status: 'pending', |
| 544 | }); |
| 545 | } |
| 546 | |
| 547 | processingRequests.add(projectPath); |
| 548 | |
| 549 | try { |
| 550 | const { domain, containerId } = await runDockerContainer(projectPath); |
| 551 | |
| 552 | return NextResponse.json({ |
| 553 | message: 'Docker container started', |
| 554 | domain, |
| 555 | containerId, |
| 556 | }); |
| 557 | } catch (error: any) { |
| 558 | logger.error(`Failed to start Docker container:`, error); |
nothing calls this directly
no test coverage detected