Use HTTP API for production deployment data

- Production fetches from local coolify-api.py at port 9876
- Development continues using SSH to query Coolify database
- Avoids need for docker socket access in nuc-portal container

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Alejandro Gutiérrez
2026-02-02 01:56:06 +00:00
parent 58308c9c62
commit 73ac2ddc21
2 changed files with 94 additions and 74 deletions

View File

@@ -1,6 +1,8 @@
import { NextResponse } from 'next/server';
const IS_PRODUCTION = process.env.NODE_ENV === 'production';
// Internal API endpoint for production (served by Python script on NUC host)
const DEPLOYMENTS_API_URL = 'http://192.168.1.3:9876/deployments';
export async function GET(
request: Request,
@@ -9,12 +11,28 @@ export async function GET(
const { uuid } = await params;
try {
const { exec } = await import('child_process');
const { promisify } = await import('util');
const execAsync = promisify(exec);
let deployment: Record<string, unknown>;
// PHP code to fetch single deployment with logs
const phpCode = `
if (IS_PRODUCTION) {
// In production, use internal HTTP API served by coolify-api.py on NUC host
const response = await fetch(`${DEPLOYMENTS_API_URL}/${uuid}`, {
cache: 'no-store',
signal: AbortSignal.timeout(30000),
});
if (!response.ok) {
throw new Error(`Deployments API error: ${response.status}`);
}
deployment = await response.json();
} else {
// In development, use SSH to call docker exec on NUC
const { exec } = await import('child_process');
const { promisify } = await import('util');
const execAsync = promisify(exec);
// PHP code to fetch single deployment with logs
const phpCode = `
$d = \\App\\Models\\ApplicationDeploymentQueue::with('application')
->where('deployment_uuid', '${uuid}')
->first();
@@ -39,45 +57,40 @@ echo json_encode([
]);
`;
const base64Code = Buffer.from(phpCode).toString('base64');
const base64Code = Buffer.from(phpCode).toString('base64');
const command = `ssh nuc "echo '${base64Code}' | base64 -d | docker exec -i coolify php artisan tinker"`;
let command: string;
if (IS_PRODUCTION) {
command = `echo '${base64Code}' | base64 -d | docker exec -i coolify php artisan tinker`;
} else {
command = `ssh nuc "echo '${base64Code}' | base64 -d | docker exec -i coolify php artisan tinker"`;
}
const { stdout } = await execAsync(command, {
maxBuffer: 10 * 1024 * 1024,
timeout: 30000,
});
const { stdout } = await execAsync(command, {
maxBuffer: 10 * 1024 * 1024,
timeout: 30000,
});
// Parse output - find JSON object in tinker output
const lines = stdout.split('\n');
let jsonStr = '';
// Parse output - find JSON object in tinker output
const lines = stdout.split('\n');
let jsonStr = '';
for (const line of lines) {
let cleaned = line;
if (cleaned.startsWith('. ')) {
cleaned = cleaned.substring(2);
} else if (cleaned.startsWith('> ')) {
continue;
}
for (const line of lines) {
let cleaned = line;
if (cleaned.startsWith('. ')) {
cleaned = cleaned.substring(2);
} else if (cleaned.startsWith('> ')) {
continue;
const trimmed = cleaned.trim();
if (trimmed.startsWith('{')) {
jsonStr = trimmed;
break;
}
}
const trimmed = cleaned.trim();
if (trimmed.startsWith('{')) {
jsonStr = trimmed;
break;
if (!jsonStr) {
throw new Error('No JSON output found');
}
}
if (!jsonStr) {
throw new Error('No JSON output found');
deployment = JSON.parse(jsonStr);
}
const deployment = JSON.parse(jsonStr);
if (deployment.error) {
return NextResponse.json({ error: deployment.error }, { status: 404 });
}