diff --git a/server-api/app/support/MaterialLocalResourceService.php b/server-api/app/support/MaterialLocalResourceService.php index e5986ee..75c764d 100644 --- a/server-api/app/support/MaterialLocalResourceService.php +++ b/server-api/app/support/MaterialLocalResourceService.php @@ -125,7 +125,10 @@ class MaterialLocalResourceService } if (!in_array(strtolower((string)($_ENV['APP_ENV'] ?? '')), ['production', 'prod'], true)) { - return 'http://' . '127.0.0.' . '1:8787'; + $port = filter_var($_ENV['APP_PORT'] ?? '', FILTER_VALIDATE_INT, [ + 'options' => ['min_range' => 1, 'max_range' => 65535], + ]) ?: 8787; + return 'http://' . '127.0.0.' . '1:' . $port; } return ''; diff --git a/server-api/config/process.php b/server-api/config/process.php index b12a8e2..16ca8d3 100644 --- a/server-api/config/process.php +++ b/server-api/config/process.php @@ -18,10 +18,34 @@ use app\process\Http; global $argv; +$resolveServerPort = static function (): int { + $rawPort = $_ENV['APP_PORT'] ?? $_SERVER['APP_PORT'] ?? getenv('APP_PORT') ?: ''; + if ($rawPort === '' && is_file(base_path('.env'))) { + foreach (file(base_path('.env'), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: [] as $line) { + $line = trim($line); + if ($line === '' || str_starts_with($line, '#') || !str_contains($line, '=')) { + continue; + } + + [$key, $value] = array_map('trim', explode('=', $line, 2)); + if ($key === 'APP_PORT') { + $rawPort = trim($value, "\"'"); + break; + } + } + } + + $port = filter_var($rawPort, FILTER_VALIDATE_INT, [ + 'options' => ['min_range' => 1, 'max_range' => 65535], + ]); + + return $port ?: 8787; +}; + return [ 'webman' => [ 'handler' => Http::class, - 'listen' => 'http://0.0.0.0:8787', + 'listen' => 'http://0.0.0.0:' . $resolveServerPort(), 'count' => cpu_count() * 4, 'user' => '', 'group' => '',