Files
Siro/stress_test/generate_mock_data.php
T

66 lines
1.9 KiB
PHP

<?php
/**
* generate_mock_data.php
* Standalone JWT generator for stress testing.
* Does NOT use connect.php (which requires HTTP context and JWT auth).
* Reads the JWT secret the same way driver_socket.php does.
*/
error_reporting(0);
ini_set('display_errors', '0');
// Load JWT library from loction_server vendor
require_once dirname(__DIR__) . '/loction_server/vendor/autoload.php';
use Firebase\JWT\JWT;
// ── Load .env (same parser as driver_socket.php) ──
function loadEnvFile(string $path): void {
if (!file_exists($path)) return;
foreach (file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
if (str_starts_with(trim($line), '#') || !str_contains($line, '=')) continue;
[$name, $value] = explode('=', $line, 2);
putenv(trim($name) . '=' . trim($value, "\"'"));
}
}
// Try location server env first (same source driver_socket.php uses)
loadEnvFile('/home/location/env/.env');
// Fallback: try backend .env
loadEnvFile(dirname(__DIR__) . '/backend/.env');
// ── Resolve JWT secret (same logic as driver_socket.php) ──
function getJwtSecret(): string {
$keyPath = getenv('JWT_SECRET_KEY_PATH');
if ($keyPath && file_exists($keyPath)) {
return trim(file_get_contents($keyPath));
}
$key = getenv('JWT_SECRET_KEY');
if ($key) return $key;
// backend .env uses JWT_SECRET (without _KEY suffix)
$key = getenv('JWT_SECRET');
if ($key) return $key;
return '';
}
$secret = getJwtSecret();
if (empty($secret)) {
echo json_encode(['error' => 'JWT secret not found', 'jwt' => '']);
exit(1);
}
$driverId = $argv[1] ?? '999999';
$payload = [
'sub' => (string)$driverId,
'role' => 'driver',
'iat' => time(),
'exp' => time() + 86400,
];
$jwt = JWT::encode($payload, $secret, 'HS256');
echo json_encode([
'driver_id' => $driverId,
'jwt' => $jwt,
]);