125 lines
4.8 KiB
PHP
Executable File
125 lines
4.8 KiB
PHP
Executable File
<?php
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(200); exit; }
|
|
|
|
$N8N_URL = 'http://127.0.0.1:5678';
|
|
$N8N_KEY = 'n8n_api_wevads2026';
|
|
|
|
function n8nRequest($endpoint, $method = 'GET', $data = null) {
|
|
global $N8N_URL, $N8N_KEY;
|
|
$url = $N8N_URL . '/api/v1/' . ltrim($endpoint, '/');
|
|
|
|
$opts = [
|
|
'http' => [
|
|
'method' => $method,
|
|
'header' => "Content-Type: application/json\r\nX-N8N-API-KEY: $N8N_KEY\r\n",
|
|
'timeout' => 10,
|
|
'ignore_errors' => true
|
|
]
|
|
];
|
|
if ($data && $method === 'POST') {
|
|
$opts['http']['content'] = json_encode($data);
|
|
}
|
|
|
|
$ctx = stream_context_create($opts);
|
|
$result = @file_get_contents($url, false, $ctx);
|
|
|
|
if ($result === false) {
|
|
return ['error' => 'N8N unreachable', 'url' => $url];
|
|
}
|
|
return json_decode($result, true) ?: ['raw' => $result];
|
|
}
|
|
|
|
$action = $_GET['action'] ?? $_POST['action'] ?? 'status';
|
|
$input = json_decode(file_get_contents('php://input'), true) ?? [];
|
|
if (isset($input['action'])) $action = $input['action'];
|
|
|
|
try {
|
|
switch ($action) {
|
|
case 'status':
|
|
// N8N health check
|
|
$health = @file_get_contents("$N8N_URL/healthz", false, stream_context_create(['http' => ['timeout' => 5]]));
|
|
$workflows = n8nRequest('workflows');
|
|
$wfCount = isset($workflows['data']) ? count($workflows['data']) : 0;
|
|
|
|
echo json_encode([
|
|
'status' => 'success',
|
|
'data' => [
|
|
'n8n_alive' => ($health !== false),
|
|
'n8n_url' => $N8N_URL,
|
|
'workflows_count' => $wfCount,
|
|
'workflows' => array_map(function($wf) {
|
|
return [
|
|
'id' => $wf['id'] ?? null,
|
|
'name' => $wf['name'] ?? 'Unknown',
|
|
'active' => $wf['active'] ?? false,
|
|
'updatedAt' => $wf['updatedAt'] ?? null
|
|
];
|
|
}, $workflows['data'] ?? [])
|
|
]
|
|
]);
|
|
break;
|
|
|
|
case 'list_workflows':
|
|
$result = n8nRequest('workflows');
|
|
echo json_encode(['status' => 'success', 'data' => $result['data'] ?? []]);
|
|
break;
|
|
|
|
case 'execute_workflow':
|
|
$wfId = $input['workflow_id'] ?? $_GET['workflow_id'] ?? null;
|
|
if (!$wfId) {
|
|
echo json_encode(['status' => 'error', 'message' => 'workflow_id required']);
|
|
break;
|
|
}
|
|
$result = n8nRequest("workflows/$wfId/activate", 'POST');
|
|
echo json_encode(['status' => 'success', 'data' => $result]);
|
|
break;
|
|
|
|
case 'trigger_provision':
|
|
// Trigger server provisioning via N8N webhook
|
|
$provider = $input['provider'] ?? 'hetzner';
|
|
$specs = $input['specs'] ?? ['type' => 'cx21', 'location' => 'hel1'];
|
|
|
|
// Try webhook trigger first
|
|
$webhookUrl = "$N8N_URL/webhook/provision-server";
|
|
$opts = [
|
|
'http' => [
|
|
'method' => 'POST',
|
|
'header' => "Content-Type: application/json\r\n",
|
|
'content' => json_encode(['provider' => $provider, 'specs' => $specs]),
|
|
'timeout' => 15
|
|
]
|
|
];
|
|
$result = @file_get_contents($webhookUrl, false, stream_context_create($opts));
|
|
|
|
if ($result === false) {
|
|
// Fallback: try Scaleway API via N8N execution
|
|
$result = n8nRequest('executions', 'POST', [
|
|
'workflowId' => 'provision-' . $provider,
|
|
'data' => ['provider' => $provider, 'specs' => $specs]
|
|
]);
|
|
}
|
|
|
|
echo json_encode([
|
|
'status' => 'success',
|
|
'message' => "Provision triggered via N8N ($provider)",
|
|
'data' => json_decode($result, true) ?? $result ?? ['queued' => true]
|
|
]);
|
|
break;
|
|
|
|
case 'executions':
|
|
$limit = $_GET['limit'] ?? 20;
|
|
$result = n8nRequest("executions?limit=$limit");
|
|
echo json_encode(['status' => 'success', 'data' => $result['data'] ?? []]);
|
|
break;
|
|
|
|
default:
|
|
echo json_encode(['status' => 'error', 'message' => "Unknown action: $action"]);
|
|
}
|
|
} catch (Exception $e) {
|
|
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
|
}
|