53 lines
1.8 KiB
PHP
53 lines
1.8 KiB
PHP
<?php
|
|
// backend/config.php
|
|
|
|
// Simple function to load environment variables from a .env file.
|
|
// In a production environment, you might use a more robust solution like 'dotenv' library
|
|
// or server-level environment variables (e.g., in Apache/Nginx config, Docker, Kubernetes).
|
|
function loadEnv($path = __DIR__ . '/.env') {
|
|
if (!file_exists($path)) {
|
|
throw new Exception("'.env' file not found at $path");
|
|
}
|
|
|
|
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
foreach ($lines as $line) {
|
|
if (strpos(trim($line), '#') === 0) { // Skip comments
|
|
continue;
|
|
}
|
|
|
|
list($name, $value) = explode('=', $line, 2);
|
|
$name = trim($name);
|
|
$value = trim($value);
|
|
|
|
if (!array_key_exists($name, $_SERVER) && !array_key_exists($name, $_ENV)) {
|
|
putenv(sprintf('%s=%s', $name, $value));
|
|
$_ENV[$name] = $value;
|
|
$_SERVER[$name] = $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Load environment variables when config.php is included
|
|
loadEnv();
|
|
|
|
// Set CORS headers
|
|
// In production, replace '*' with your actual frontend domain(s)
|
|
header("Access-Control-Allow-Origin: *");
|
|
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
|
|
header("Access-Control-Allow-Headers: Content-Type, Authorization");
|
|
header("Content-Type: application/json"); // Default to JSON response
|
|
|
|
// Handle OPTIONS request for CORS preflight
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|
http_response_code(200);
|
|
exit();
|
|
}
|
|
|
|
// Basic error reporting for development. Disable in production.
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
// Define API Keys from environment variables
|
|
define('RUNWAY_API_KEY', getenv('RUNWAY_API_KEY'));
|
|
define('GEMINI_API_KEY', getenv('GEMINI_API_KEY')); // For simulation
|