implementing discovering static sites

This commit is contained in:
MeghdadFadaee committed 2026-07-04 15:02:30 +03:30
1 parent 17dfd5e49d
commit 0d320eaeb4
2 files changed
+44 -14

No files matched your search

+12 -7
View File
@@ -2,16 +2,18 @@
Local HTTPS router for PHP and Laravel projects. Local HTTPS router for PHP and Laravel projects.
`php-dev-router` discovers PHP apps in a projects directory, generates nginx `php-dev-router` discovers PHP apps and static HTML sites in a projects
virtual hosts, adds exact local host records to `/etc/hosts`, and can run as a directory, generates nginx virtual hosts, adds exact local host records to
small systemd watcher so newly added projects become available automatically. `/etc/hosts`, and can run as a small systemd watcher so newly added projects
become available automatically.
It is built for development workstations, not production servers. It is built for development workstations, not production servers.
## Features ## Features
- Detects Laravel apps, public front-controller apps, and generic PHP apps. - Detects Laravel apps, public front-controller apps, generic PHP apps, and
- Generates HTTPS nginx server blocks backed by PHP-FPM. static HTML sites.
- Generates HTTPS nginx server blocks, with PHP-FPM routing where needed.
- Maps only detected project hostnames to `127.0.0.1`. - Maps only detected project hostnames to `127.0.0.1`.
- Avoids wildcard DNS hijacking, so unrelated real domains keep resolving - Avoids wildcard DNS hijacking, so unrelated real domains keep resolving
normally. normally.
@@ -23,7 +25,7 @@ It is built for development workstations, not production servers.
- Linux with systemd - Linux with systemd
- PHP 8.2 or newer - PHP 8.2 or newer
- nginx - nginx
- PHP-FPM - PHP-FPM for PHP apps
- A local or trusted development certificate/key for the configured domain - A local or trusted development certificate/key for the configured domain
The default example config uses `php.test`. Browsers will still require a The default example config uses `php.test`. Browsers will still require a
@@ -57,7 +59,7 @@ Start from `config.example.json`.
Important options: Important options:
- `projects_root`: directory that contains your PHP projects. - `projects_root`: directory that contains your PHP projects and static sites.
- `domain`: suffix used for generated hostnames. - `domain`: suffix used for generated hostnames.
- `ssl_certificate` / `ssl_certificate_key`: certificate used by nginx. - `ssl_certificate` / `ssl_certificate_key`: certificate used by nginx.
- `php_fpm_socket`: PHP-FPM socket passed to nginx. - `php_fpm_socket`: PHP-FPM socket passed to nginx.
@@ -88,6 +90,9 @@ installs should let the tool run `nginx -t` before reload.
- Laravel app: `artisan` and `public/index.php`, served from `public`. - Laravel app: `artisan` and `public/index.php`, served from `public`.
- Public front-controller app: `public/index.php`, served from `public`. - Public front-controller app: `public/index.php`, served from `public`.
- Generic PHP app: `index.php`, served from that directory. - Generic PHP app: `index.php`, served from that directory.
- Public static HTML site: `public/index.html` or `public/index.htm`, served
from `public`.
- Static HTML site: `index.html` or `index.htm`, served from that directory.
Ignored directories include hidden/tooling directories, `vendor`, `node_modules`, Ignored directories include hidden/tooling directories, `vendor`, `node_modules`,
`storage`, and common IDE/cache paths. `storage`, and common IDE/cache paths.
+32 -7
View File
@@ -106,7 +106,7 @@ Usage:
php-dev-router install [--dry-run] php-dev-router install [--dry-run]
Commands: Commands:
scan Discover PHP/Laravel apps and show hostnames. scan Discover PHP/Laravel/static apps and show hostnames.
apply Generate nginx config and exact local hosts entries. apply Generate nginx config and exact local hosts entries.
watch Re-apply when the project inventory changes. watch Re-apply when the project inventory changes.
doctor Check config, certs, services, generated files, and DNS. doctor Check config, certs, services, generated files, and DNS.
@@ -124,7 +124,7 @@ function commandScan(array $config, array $options): void
} }
if ($apps === []) { if ($apps === []) {
echo "No serveable PHP apps found in {$config['projects_root']}\n"; echo "No serveable PHP or static HTML apps found in {$config['projects_root']}\n";
return; return;
} }
@@ -252,13 +252,14 @@ function discoverApps(array $config): array
$host = "{$slug}-{$hash}.{$config['domain']}"; $host = "{$slug}-{$hash}.{$config['domain']}";
} }
$usedHosts[$host] = true; $usedHosts[$host] = true;
$indexFile = $candidate['index'] ?? 'index.php';
$apps[] = [ $apps[] = [
'host' => $host, 'host' => $host,
'name' => $slug, 'name' => $slug,
'type' => $candidate['type'], 'type' => $candidate['type'],
'app_dir' => $candidate['app_dir'], 'app_dir' => $candidate['app_dir'],
'root' => $candidate['root'], 'root' => $candidate['root'],
'index' => $candidate['root'] . '/index.php', 'index' => $candidate['root'] . "/{$indexFile}",
]; ];
} }
@@ -275,9 +276,13 @@ function scanDirectory(string $dir, string $base, int $depth, array $config, arr
$dir = rtrim($dir, '/'); $dir = rtrim($dir, '/');
if (is_file("{$dir}/public/index.php")) { if (is_file("{$dir}/public/index.php")) {
$type = is_file("{$dir}/artisan") ? 'laravel' : 'public-front-controller'; $type = is_file("{$dir}/artisan") ? 'laravel' : 'public-front-controller';
$candidates[] = ['app_dir' => $dir, 'root' => "{$dir}/public", 'type' => $type]; $candidates[] = ['app_dir' => $dir, 'root' => "{$dir}/public", 'type' => $type, 'index' => 'index.php'];
} elseif (is_file("{$dir}/index.php")) { } elseif (is_file("{$dir}/index.php")) {
$candidates[] = ['app_dir' => $dir, 'root' => $dir, 'type' => 'generic-php']; $candidates[] = ['app_dir' => $dir, 'root' => $dir, 'type' => 'generic-php', 'index' => 'index.php'];
} elseif ($index = firstExistingFile($dir, ['public/index.html', 'public/index.htm'])) {
$candidates[] = ['app_dir' => $dir, 'root' => "{$dir}/public", 'type' => 'public-static-html', 'index' => basename($index)];
} elseif ($index = firstExistingFile($dir, ['index.html', 'index.htm'])) {
$candidates[] = ['app_dir' => $dir, 'root' => $dir, 'type' => 'static-html', 'index' => basename($index)];
} }
$children = @scandir($dir); $children = @scandir($dir);
@@ -315,6 +320,17 @@ function shouldSkip(string $path, string $base, array $skipDirs): bool
return false; return false;
} }
function firstExistingFile(string $dir, array $files): ?string
{
foreach ($files as $file) {
if (is_file("{$dir}/{$file}")) {
return $file;
}
}
return null;
}
function renderNginx(array $config, array $apps): string function renderNginx(array $config, array $apps): string
{ {
$lines = [ $lines = [
@@ -329,6 +345,7 @@ function renderNginx(array $config, array $apps): string
$cert = nginxEscape($config['ssl_certificate']); $cert = nginxEscape($config['ssl_certificate']);
$key = nginxEscape($config['ssl_certificate_key']); $key = nginxEscape($config['ssl_certificate_key']);
$socket = nginxEscape($config['php_fpm_socket']); $socket = nginxEscape($config['php_fpm_socket']);
$isStatic = isStaticApp($app);
$lines[] = "server {"; $lines[] = "server {";
$lines[] = " listen 80;"; $lines[] = " listen 80;";
@@ -344,13 +361,15 @@ function renderNginx(array $config, array $apps): string
$lines[] = " ssl_certificate_key \"{$key}\";"; $lines[] = " ssl_certificate_key \"{$key}\";";
$lines[] = ""; $lines[] = "";
$lines[] = " root \"{$root}\";"; $lines[] = " root \"{$root}\";";
$lines[] = " index index.php index.html;"; $lines[] = $isStatic ? " index index.html index.htm;" : " index index.php index.html index.htm;";
$lines[] = ""; $lines[] = "";
$lines[] = " client_max_body_size 128m;"; $lines[] = " client_max_body_size 128m;";
$lines[] = ""; $lines[] = "";
$lines[] = " location / {"; $lines[] = " location / {";
$lines[] = " try_files \$uri \$uri/ /index.php?\$query_string;"; $lines[] = $isStatic ? " try_files \$uri \$uri/ =404;" : " try_files \$uri \$uri/ /index.php?\$query_string;";
$lines[] = " }"; $lines[] = " }";
if (!$isStatic) {
$lines[] = ""; $lines[] = "";
$lines[] = " location ~ \\.php$ {"; $lines[] = " location ~ \\.php$ {";
$lines[] = " try_files \$uri =404;"; $lines[] = " try_files \$uri =404;";
@@ -358,6 +377,7 @@ function renderNginx(array $config, array $apps): string
$lines[] = " fastcgi_index index.php;"; $lines[] = " fastcgi_index index.php;";
$lines[] = " include fastcgi.conf;"; $lines[] = " include fastcgi.conf;";
$lines[] = " }"; $lines[] = " }";
}
$lines[] = ""; $lines[] = "";
$lines[] = " location ~ /\\.(?!well-known).* {"; $lines[] = " location ~ /\\.(?!well-known).* {";
$lines[] = " deny all;"; $lines[] = " deny all;";
@@ -369,6 +389,11 @@ function renderNginx(array $config, array $apps): string
return implode("\n", $lines); return implode("\n", $lines);
} }
function isStaticApp(array $app): bool
{
return in_array($app['type'], ['public-static-html', 'static-html'], true);
}
function renderHostsBlock(array $apps): string function renderHostsBlock(array $apps): string
{ {
$lines = [HOSTS_BEGIN]; $lines = [HOSTS_BEGIN];