From 42e5b5210b71cbfcbbaa79d63c344eff4635c2ff Mon Sep 17 00:00:00 2001 From: "Buster \"Silver Eagle\" Neece" Date: Sat, 28 May 2022 23:07:56 -0500 Subject: [PATCH] Expand custom nginx config. --- src/Console/Command/RestartRadioCommand.php | 6 ++- .../AbstractLogViewerController.php | 6 +++ src/Http/Response.php | 19 +++------ src/Nginx/ConfigWriter.php | 31 ++++++++++++++- src/Nginx/CustomUrls.php | 39 +++++++++++++++++++ src/Nginx/Nginx.php | 7 +++- src/Radio/Backend/Liquidsoap.php | 5 +-- util/ansible/roles/nginx/templates/default.j2 | 13 ------- util/docker/web/nginx/azuracast.conf.tmpl | 12 ------ 9 files changed, 92 insertions(+), 46 deletions(-) create mode 100644 src/Nginx/CustomUrls.php diff --git a/src/Console/Command/RestartRadioCommand.php b/src/Console/Command/RestartRadioCommand.php index 99dac5444..9f08b7bb3 100644 --- a/src/Console/Command/RestartRadioCommand.php +++ b/src/Console/Command/RestartRadioCommand.php @@ -79,7 +79,7 @@ class RestartRadioCommand extends CommandAbstract $this->nginx->writeConfiguration( station: $station, - reloadIfChanged: !$noSupervisorRestart + reloadIfChanged: false ); } catch (Throwable $e) { $io->error([ @@ -90,6 +90,10 @@ class RestartRadioCommand extends CommandAbstract $io->progressAdvance(); } + if (!$noSupervisorRestart) { + $this->nginx->reload(); + } + $io->progressFinish(); return 0; } diff --git a/src/Controller/AbstractLogViewerController.php b/src/Controller/AbstractLogViewerController.php index ab901d07b..765117122 100644 --- a/src/Controller/AbstractLogViewerController.php +++ b/src/Controller/AbstractLogViewerController.php @@ -22,6 +22,12 @@ abstract class AbstractLogViewerController $stationConfigDir = $station->getRadioConfigDir(); + $log_paths['station_nginx'] = [ + 'name' => __('Station Nginx Configuration'), + 'path' => $stationConfigDir . '/nginx.conf', + 'tail' => false, + ]; + if (BackendAdapters::Liquidsoap === $station->getBackendTypeEnum()) { $log_paths['liquidsoap_log'] = [ 'name' => __('Liquidsoap Log'), diff --git a/src/Http/Response.php b/src/Http/Response.php index c379c7ddf..956b416b0 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Http; +use App\Nginx\CustomUrls; use Azura\Files\Adapter\LocalAdapterInterface; use Azura\Files\ExtendedFilesystemInterface; use InvalidArgumentException; @@ -134,21 +135,11 @@ final class Response extends \Slim\Http\Response $adapter = $filesystem->getAdapter(); if ($adapter instanceof LocalAdapterInterface) { - $localPath = $filesystem->getLocalPath($path); - // Special internal nginx routes to use X-Accel-Redirect for far more performant file serving. - $specialPaths = [ - '/var/azuracast/backups' => '/internal/backups', - '/var/azuracast/stations' => '/internal/stations', - ]; - - foreach ($specialPaths as $diskPath => $nginxPath) { - if (str_starts_with($localPath, $diskPath)) { - $accelPath = str_replace($diskPath, $nginxPath, $localPath); - - return $response->withHeader('Content-Type', $fileMeta->mimeType() ?? '') - ->withHeader('X-Accel-Redirect', $accelPath); - } + $accelPath = CustomUrls::getXAccelPath($filesystem->getLocalPath($path)); + if (null !== $accelPath) { + return $response->withHeader('Content-Type', $fileMeta->mimeType() ?? '') + ->withHeader('X-Accel-Redirect', $accelPath); } } diff --git a/src/Nginx/ConfigWriter.php b/src/Nginx/ConfigWriter.php index 06d2761ff..d1526e9cc 100644 --- a/src/Nginx/ConfigWriter.php +++ b/src/Nginx/ConfigWriter.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace App\Nginx; use App\Event\Nginx\WriteNginxConfiguration; +use App\Radio\Enums\BackendAdapters; use App\Radio\Enums\FrontendAdapters; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -18,6 +19,7 @@ final class ConfigWriter implements EventSubscriberInterface return [ WriteNginxConfiguration::class => [ ['writeRadioSection', 35], + ['writeWebDjSection', 30], ], ]; } @@ -31,14 +33,14 @@ final class ConfigWriter implements EventSubscriberInterface return; } - $shortCode = $station->getShortName(); + $listenBaseUrl = CustomUrls::getListenUrl($station); $port = $station->getFrontendConfig()->getPort(); $event->appendBlock( <<getStation(); + + // Only forward Liquidsoap + if (BackendAdapters::Liquidsoap !== $station->getBackendTypeEnum()) { + return; + } + + $webDjBaseUrl = CustomUrls::getWebDjUrl($station); + + $autoDjPort = $station->getBackendConfig()->getDjPort(); + + $event->appendBlock( + <<getShortName(); + } + + public static function getWebDjUrl(Station $station): string + { + return '/webdj/' . $station->getShortName(); + } + + /** + * Returns a custom path if X-Accel-Redirect is configured for the path provided. + */ + public static function getXAccelPath(string $path): ?string + { + $specialPaths = [ + '/var/azuracast/backups' => '/internal/backups', + '/var/azuracast/stations' => '/internal/stations', + ]; + + foreach ($specialPaths as $diskPath => $nginxPath) { + if (str_starts_with($path, $diskPath)) { + return str_replace($diskPath, $nginxPath, $path); + } + } + + return null; + } +} diff --git a/src/Nginx/Nginx.php b/src/Nginx/Nginx.php index 80c183ae5..c5c6b2508 100644 --- a/src/Nginx/Nginx.php +++ b/src/Nginx/Nginx.php @@ -39,7 +39,7 @@ final class Nginx (new Filesystem())->dumpFile($configPath, $newConfig); if ($reloadIfChanged) { - $this->supervisor->signalProcess(self::PROCESS_NAME, 'HUP'); + $this->reload(); } } @@ -57,6 +57,11 @@ final class Nginx return $event->buildConfiguration(); } + public function reload(): void + { + $this->supervisor->signalProcess(self::PROCESS_NAME, 'HUP'); + } + private function getConfigPath(Station $station): string { return $station->getRadioConfigDir() . '/nginx.conf'; diff --git a/src/Radio/Backend/Liquidsoap.php b/src/Radio/Backend/Liquidsoap.php index 52495783d..0daf095b4 100644 --- a/src/Radio/Backend/Liquidsoap.php +++ b/src/Radio/Backend/Liquidsoap.php @@ -7,6 +7,7 @@ namespace App\Radio\Backend; use App\Entity; use App\Event\Radio\WriteLiquidsoapConfiguration; use App\Exception; +use App\Nginx\CustomUrls; use App\Radio\Enums\LiquidsoapQueues; use LogicException; use Psr\Http\Message\UriInterface; @@ -307,13 +308,11 @@ class Liquidsoap extends AbstractBackend public function getWebStreamingUrl(Entity\Station $station, UriInterface $base_url): UriInterface { - $stream_port = $this->getStreamPort($station); - $djMount = $station->getBackendConfig()->getDjMountPoint(); return $base_url ->withScheme('wss') - ->withPath($base_url->getPath() . '/radio/' . $stream_port . $djMount); + ->withPath($base_url->getPath() . CustomUrls::getWebDjUrl($station) . $djMount); } public function verifyConfig(string $config): void diff --git a/util/ansible/roles/nginx/templates/default.j2 b/util/ansible/roles/nginx/templates/default.j2 index 29b7524fa..61ef9a906 100644 --- a/util/ansible/roles/nginx/templates/default.j2 +++ b/util/ansible/roles/nginx/templates/default.j2 @@ -144,19 +144,6 @@ server { proxy_pass http://127.0.0.1:$1/$3?$args; } - # Reverse proxy the Liquidsoap harbor inputs to allow for streaming. - location ~ ^/radio/(8[0-4][0-9]5)(/?)(.*)$ { - proxy_buffering off; - proxy_ignore_client_abort off; - proxy_send_timeout 21600; - proxy_read_timeout 21600; - - proxy_pass http://127.0.0.1:$1/$3; - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection "Upgrade"; - } - # pub/sub endpoints location ~ /api/live/nowplaying/([^\/]+)$ { nchan_subscriber; diff --git a/util/docker/web/nginx/azuracast.conf.tmpl b/util/docker/web/nginx/azuracast.conf.tmpl index 0ffd0841f..47be1d617 100644 --- a/util/docker/web/nginx/azuracast.conf.tmpl +++ b/util/docker/web/nginx/azuracast.conf.tmpl @@ -159,18 +159,6 @@ server { proxy_pass http://127.0.0.1:$1/$3?$args; } - # Reverse proxy the Liquidsoap harbor inputs to allow for streaming. -{{ if eq .Env.NGINX_WEBDJ_PORTS "default" }} - location ~ ^/radio/(8[0-9][0-9]5)(/?)(.*)$ { -{{ else }} - location ~ ^/radio/{{ .Env.NGINX_WEBDJ_PORTS }}(/?)(.*)$ { -{{ end }} - - include proxy_params; - - proxy_pass http://127.0.0.1:$1/$3; - } - # pub/sub endpoints location ~ /api/live/nowplaying/([^\/]+)$ { nchan_access_control_allow_origin "*";