72 lines
2.1 KiB
PHP
72 lines
2.1 KiB
PHP
<?php
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class IceServerController extends Controller
|
|
{
|
|
public function __invoke(): JsonResponse
|
|
{
|
|
if (!Auth::check() && !session()->has('candidate_interview_id')) {
|
|
return response()->json(['error' => 'Unauthenticated access.'], 401);
|
|
}
|
|
|
|
try {
|
|
$baseUrl = config(
|
|
"services.metered.url",
|
|
env('METERD_URL', 'https://single-login-system.metered.live/api/v1/turn/credentials')
|
|
);
|
|
$apiKey = config(
|
|
"services.metered.key",
|
|
env('METERED_KEY', 'ffc9146ac5c2f75d83d7c679d8553a21c8fa')
|
|
);
|
|
|
|
$url = $baseUrl;
|
|
if ($apiKey && !str_contains($url, "apiKey=")) {
|
|
$separator = str_contains($url, "?") ? "&" : "?";
|
|
$url .= $separator . "apiKey=" . urlencode($apiKey);
|
|
}
|
|
|
|
$response = Http::timeout(5)->get($url);
|
|
|
|
if ($response->successful()) {
|
|
return response()->json($response->json());
|
|
}
|
|
|
|
Log::error(
|
|
"Metered API error response",
|
|
[
|
|
"status" => $response->status(),
|
|
"body" => $response->body(),
|
|
],
|
|
);
|
|
|
|
return response()->json(
|
|
[
|
|
"error" => "Failed to fetch ICE servers",
|
|
"status" => $response->status(),
|
|
],
|
|
$response->status() >= 400 && $response->status() < 600
|
|
? $response->status()
|
|
: 500,
|
|
);
|
|
} catch (\Throwable $e) {
|
|
Log::error(
|
|
"ICE servers fetch exception: " . $e->getMessage(),
|
|
);
|
|
|
|
return response()->json(
|
|
[
|
|
"error" => "An error occurred while fetching ICE servers.",
|
|
"message" => $e->getMessage(),
|
|
],
|
|
500,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|