chore: support configurable api port

This commit is contained in:
wushumin
2026-06-04 13:11:43 +08:00
parent 55c357f2c2
commit 42d86e4c66
2 changed files with 29 additions and 2 deletions

View File

@@ -125,7 +125,10 @@ class MaterialLocalResourceService
} }
if (!in_array(strtolower((string)($_ENV['APP_ENV'] ?? '')), ['production', 'prod'], true)) { 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 ''; return '';

View File

@@ -18,10 +18,34 @@ use app\process\Http;
global $argv; 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 [ return [
'webman' => [ 'webman' => [
'handler' => Http::class, 'handler' => Http::class,
'listen' => 'http://0.0.0.0:8787', 'listen' => 'http://0.0.0.0:' . $resolveServerPort(),
'count' => cpu_count() * 4, 'count' => cpu_count() * 4,
'user' => '', 'user' => '',
'group' => '', 'group' => '',