matching API andpoints
This commit is contained in:
@@ -2,14 +2,6 @@
|
||||
declare(strict_types=1);
|
||||
require_once __DIR__ . '/../src/Calco2latoApiClient.php';
|
||||
|
||||
$env = file_get_contents(__DIR__."/../src/.env");
|
||||
$lines = explode("\n",$env);
|
||||
|
||||
foreach($lines as $line){
|
||||
preg_match("/([^#]+)\=(.*)/",$line,$matches);
|
||||
if(isset($matches[2])){ putenv(trim($line)); }
|
||||
}
|
||||
|
||||
// --- Basic CORS (adjust origin to your site/domain) ---
|
||||
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
|
||||
$allowedOrigin = preg_match('#^https://(www\.)?your-frontend\.example$#', $origin) ? $origin : '';
|
||||
@@ -43,15 +35,26 @@ if ($bucket['count'] > 120) { // 120 requests/min/IP
|
||||
exit;
|
||||
}
|
||||
|
||||
// --- Instantiate API client ---
|
||||
$base = getenv('CALCO2_API_BASE') ?: 'https://api.calco2la.to';
|
||||
$key = getenv('CALCO2_API_KEY') ?: '';
|
||||
// --- read .env file with CALCO2LATO_API_BASE and CALCO2LATO_API_KEY
|
||||
$env = file_get_contents(__DIR__."/../src/.env");
|
||||
$lines = explode("\n",$env);
|
||||
|
||||
foreach($lines as $line){
|
||||
preg_match("/([^#]+)\=(.*)/",$line,$matches);
|
||||
if(isset($matches[2])){ putenv(trim($line)); }
|
||||
}
|
||||
|
||||
// --- Read config ---
|
||||
$base = getenv('CALCO2LATO_API_BASE') ?: 'https://api.calco2la.to';
|
||||
$key = getenv('CALCO2LATO_API_KEY') ?: '';
|
||||
if (!$key) {
|
||||
http_response_code(500);
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['error' => 'Server misconfiguration: missing API key']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// --- Instantiate API client ---
|
||||
$client = new Calco2latoApiClient($base, $key);
|
||||
|
||||
// --- Whitelist router ---
|
||||
@@ -65,34 +68,18 @@ try {
|
||||
switch ($endpoint) {
|
||||
case 'airports.search':
|
||||
// GET /?endpoint=airports.search&q=FRA&per_page=10
|
||||
$q = $_GET['q'] ?? '';
|
||||
$per_page = isset($_GET['per_page']) ? (int)$_GET['per_page'] : 20;
|
||||
$page= isset($_GET['page']) ? (int)$_GET['page'] : 1;
|
||||
$data = $client->searchAirports($q, $per_page, $page);
|
||||
echo json_encode($data);
|
||||
break;
|
||||
|
||||
case 'airports.get':
|
||||
// GET /?endpoint=airports.get&code=FRA
|
||||
$code = $_GET['code'] ?? '';
|
||||
if ($code === '') throw new InvalidArgumentException('Missing airport code');
|
||||
$data = $client->getAirport($code);
|
||||
$q = $_GET['q'] ?? '';
|
||||
$per_page = isset($_GET['per_page']) ? (int)$_GET['per_page'] : 20;
|
||||
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
|
||||
$data = $client->searchAirports($q, $per_page, $page);
|
||||
echo json_encode($data);
|
||||
break;
|
||||
|
||||
case 'flights.estimate':
|
||||
// POST with JSON body: { endpoint: "flights.estimate", params: {...} }
|
||||
if ($method !== 'POST') throw new RuntimeException('Use POST');
|
||||
$params = $input['params'] ?? [];
|
||||
$data = $client->flightEstimate($params);
|
||||
echo json_encode($data);
|
||||
break;
|
||||
|
||||
case 'flights.get':
|
||||
// GET /?endpoint=flights.get&id=abc123
|
||||
$id = $_GET['id'] ?? '';
|
||||
if ($id === '') throw new InvalidArgumentException('Missing flight id');
|
||||
$data = $client->getFlightById($id);
|
||||
$params = $input['params'] ?? [];
|
||||
$data = $client->flightEstimate($params);
|
||||
echo json_encode($data);
|
||||
break;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user