- Dynamische Modulerkennung via module.json in ModuleService.php - Kaskadierender Env-Loader fuer zentrale API-Keys (GEMINI_API_KEY) - Graceful Auth fuer alle Subprojekte (dinos, plants, storymachine, test) - Wiederverwendbares Starter-Template in _template/ - test-Modul auf philippurbschat.de Dark-Design modernisiert
75 lines
2 KiB
PHP
75 lines
2 KiB
PHP
<?php
|
|
/**
|
|
* Modul-API-Proxy: Sicherer serverseitiger Relay für Gemini & externe APIs
|
|
*
|
|
* - Hält den API-Key auf dem Server geheim
|
|
* - Nutzt automatisch den zentralen Key aus /home/.env oder lokale .env
|
|
*/
|
|
|
|
require_once __DIR__ . '/bootstrap.php';
|
|
|
|
header('Content-Type: application/json; charset=UTF-8');
|
|
|
|
// Nur POST-Anfragen erlauben
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode(['error' => 'Nur POST-Anfragen sind erlaubt.']);
|
|
exit;
|
|
}
|
|
|
|
$apiKey = module_env('GEMINI_API_KEY');
|
|
|
|
if (empty($apiKey)) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'API-Schlüssel (GEMINI_API_KEY) ist weder lokal noch zentral konfiguriert.']);
|
|
exit;
|
|
}
|
|
|
|
// Request Payload lesen
|
|
$rawInput = file_get_contents('php://input');
|
|
$requestData = json_decode($rawInput, true);
|
|
|
|
if (json_last_error() !== JSON_ERROR_NONE || !isset($requestData['prompt'])) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Ungültige Anfrage: "prompt" wird benötigt.']);
|
|
exit;
|
|
}
|
|
|
|
$prompt = trim($requestData['prompt']);
|
|
$model = $requestData['model'] ?? 'gemini-2.5-flash';
|
|
|
|
// Google Gemini API Aufruf
|
|
$endpoint = 'https://generativelanguage.googleapis.com/v1beta/models/' . urlencode($model) . ':generateContent?key=' . $apiKey;
|
|
|
|
$payload = [
|
|
'contents' => [
|
|
[
|
|
'parts' => [
|
|
['text' => $prompt]
|
|
]
|
|
]
|
|
]
|
|
];
|
|
|
|
$ch = curl_init($endpoint);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
|
|
CURLOPT_POSTFIELDS => json_encode($payload),
|
|
CURLOPT_TIMEOUT => 30
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$curlError = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
if ($curlError) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Verbindungsfehler zur API: ' . $curlError]);
|
|
exit;
|
|
}
|
|
|
|
http_response_code($httpCode);
|
|
echo $response;
|