From 2e09fbfde1da4edf83e2ed93b12273e59e55a60b Mon Sep 17 00:00:00 2001 From: Buster Neece Date: Sat, 5 Aug 2023 11:18:38 -0500 Subject: [PATCH] Fold NPM/node into parent container; implement vite server on dev env. --- .github/workflows/default.yml | 2 - Dockerfile | 90 ++++++++++++++++++-- Makefile | 9 -- docker-compose.dev.yml | 2 + docker-compose.frontend.yml | 11 --- frontend/package.json | 1 + frontend/vite.config.js | 6 ++ src/Service/ServiceControl.php | 5 ++ src/View.php | 23 ++++- templates/partials/head.phtml | 7 ++ util/docker/dev/service.full/vite.conf | 19 +++++ util/docker/dev/setup.sh | 30 +++++++ util/docker/web/nginx/azuracast.conf.tmpl | 11 +++ util/docker/web/service.full/centrifugo.conf | 2 +- util/docker/web/service.full/sftpgo.conf | 2 +- util/docker/web/setup/node.sh | 6 ++ 16 files changed, 191 insertions(+), 35 deletions(-) delete mode 100644 docker-compose.frontend.yml create mode 100644 util/docker/dev/service.full/vite.conf create mode 100644 util/docker/dev/setup.sh create mode 100644 util/docker/web/setup/node.sh diff --git a/.github/workflows/default.yml b/.github/workflows/default.yml index 42e11b118..ceef8b7b3 100644 --- a/.github/workflows/default.yml +++ b/.github/workflows/default.yml @@ -143,8 +143,6 @@ jobs: path: | .gitinfo translations - web/static/vite_dist - web/static/api/openapi.yml build: name: Build & Publish diff --git a/Dockerfile b/Dockerfile index b260b81f2..7b063fbe0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -58,8 +58,75 @@ RUN bash /bd_build/redis/setup.sh RUN bash /bd_build/cleanup.sh \ && rm -rf /bd_build +USER azuracast + +RUN touch /var/azuracast/.docker + +USER root + VOLUME ["/var/azuracast/stations", "/var/azuracast/uploads", "/var/azuracast/backups", "/var/azuracast/sftpgo/persist", "/var/azuracast/servers/shoutcast2", "/var/azuracast/meilisearch/persist"] +# +# Development Build +# +FROM pre-final AS development + +# Dev build step +COPY ./util/docker/common /bd_build/ +COPY ./util/docker/dev /bd_build/dev + +RUN bash /bd_build/dev/setup.sh \ + && bash /bd_build/cleanup.sh \ + && rm -rf /bd_build + +USER azuracast + +WORKDIR /var/azuracast/www + +COPY --chown=azuracast:azuracast . . + +RUN composer install --no-ansi --no-interaction + +WORKDIR /var/azuracast/www/frontend + +RUN npm ci --include=dev + +WORKDIR /var/azuracast/www + +USER root + +EXPOSE 80 443 2022 +EXPOSE 8000-8999 + +# Sensible default environment variables. +ENV TZ="UTC" \ + LANG="en_US.UTF-8" \ + PATH="${PATH}:/var/azuracast/servers/shoutcast2" \ + DOCKER_IS_STANDALONE="true" \ + APPLICATION_ENV="development" \ + MYSQL_HOST="localhost" \ + MYSQL_PORT=3306 \ + MYSQL_USER="azuracast" \ + MYSQL_PASSWORD="azur4c457" \ + MYSQL_DATABASE="azuracast" \ + ENABLE_REDIS="true" \ + REDIS_HOST="localhost" \ + REDIS_PORT=6379 \ + REDIS_DB=1 \ + NGINX_RADIO_PORTS="default" \ + NGINX_WEBDJ_PORTS="default" \ + COMPOSER_PLUGIN_MODE="false" \ + ADDITIONAL_MEDIA_SYNC_WORKER_COUNT=0 \ + PROFILING_EXTENSION_ENABLED=1 \ + PROFILING_EXTENSION_ALWAYS_ON=0 \ + PROFILING_EXTENSION_HTTP_KEY=dev \ + PROFILING_EXTENSION_HTTP_IP_WHITELIST=* \ + ENABLE_WEB_UPDATER="false" + +# Entrypoint and default command +ENTRYPOINT ["tini", "--", "/usr/local/bin/my_init"] +CMD ["--no-main-command"] + # # Final build (Just environment vars and squishing the FS) # @@ -83,9 +150,21 @@ COPY --chown=azuracast:azuracast . . RUN composer dump-autoload --optimize --classmap-authoritative \ && touch /var/azuracast/.docker +WORKDIR /var/azuracast/www/frontend + +RUN npm ci --include=dev \ + && npm run build + +WORKDIR /var/azuracast/www + +RUN php bin/console locale:import \ + && php bin/console azuracast:api:docs \ + && rm -rf ./translations \ + && rm -rf ./frontend + USER root -EXPOSE 80 2022 +EXPOSE 80 443 2022 EXPOSE 8000-8999 # Sensible default environment variables. @@ -105,16 +184,9 @@ ENV TZ="UTC" \ REDIS_DB=1 \ NGINX_RADIO_PORTS="default" \ NGINX_WEBDJ_PORTS="default" \ - PREFER_RELEASE_BUILDS="false" \ COMPOSER_PLUGIN_MODE="false" \ ADDITIONAL_MEDIA_SYNC_WORKER_COUNT=0 \ - PROFILING_EXTENSION_ENABLED=0 \ - PROFILING_EXTENSION_ALWAYS_ON=0 \ - PROFILING_EXTENSION_HTTP_KEY=dev \ - PROFILING_EXTENSION_HTTP_IP_WHITELIST=* \ - ENABLE_WEB_UPDATER="true" \ - ENABLE_MEILISEARCH="true" \ - MEILISEARCH_MASTER_KEY="zejNISMlGe_6IUGBsdjfG6c6Qi8g2RngTxOmWsTbwvw" + ENABLE_WEB_UPDATER="true" # Entrypoint and default command ENTRYPOINT ["tini", "--", "/usr/local/bin/my_init"] diff --git a/Makefile b/Makefile index 0ccbae3a6..787a9a3c0 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,6 @@ update: # Update everything (i.e. after a branch update) $(MAKE) down docker-compose run --rm web gosu azuracast composer install docker-compose run --rm web azuracast_cli azuracast:setup --update - $(MAKE) frontend-build $(MAKE) up test: @@ -47,14 +46,6 @@ bash: bash-root: docker-compose exec web bash -frontend-bash: - docker-compose -p azuracast_frontend -f docker-compose.frontend.yml build - docker-compose -p azuracast_frontend -f docker-compose.frontend.yml run -e NODE_ENV=development --rm frontend - -frontend-build: - docker-compose -p azuracast_frontend -f docker-compose.frontend.yml build - docker-compose -p azuracast_frontend -f docker-compose.frontend.yml run -e NODE_ENV=development --rm frontend npm run build - generate-locales: docker-compose -p azuracast_frontend -f docker-compose.frontend.yml build docker-compose -p azuracast_frontend -f docker-compose.frontend.yml run -e NODE_ENV=development --rm frontend npm run generate-locales diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 1c684e728..9084542d2 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -2,6 +2,7 @@ services: web: build: context: . + target: development ports: - "127.0.0.1:3306:3306" # MariaDB - "127.0.0.1:6025:6025" # Centrifugo @@ -9,6 +10,7 @@ services: volumes: - $PWD/util/local_ssl/default.crt:/var/azuracast/acme/ssl.crt:ro - $PWD/util/local_ssl/default.key:/var/azuracast/acme/ssl.key:ro + - $PWD/frontend/node_modules:/var/azuracast/www/frontend/node_modules - $PWD/vendor:/var/azuracast/www/vendor - $PWD:/var/azuracast/www extra_hosts: diff --git a/docker-compose.frontend.yml b/docker-compose.frontend.yml deleted file mode 100644 index 5a641a5e5..000000000 --- a/docker-compose.frontend.yml +++ /dev/null @@ -1,11 +0,0 @@ -services: - frontend: - container_name: azuracast_frontend - build: - context: ./frontend - user: "${AZURACAST_PUID:-1000}:${AZURACAST_PGID:-1000}" - volumes: - - $PWD/frontend:/data/frontend - - $PWD/frontend/node_modules:/data/frontend/node_modules - - $PWD/translations:/data/translations - - $PWD/web/static:/data/web/static diff --git a/frontend/package.json b/frontend/package.json index 01b88487f..686416dbb 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -3,6 +3,7 @@ "license": "Apache-2.0", "scripts": { "build": "vite build", + "serve": "vite", "generate-locales": "vue-gettext-extract" }, "dependencies": { diff --git a/frontend/vite.config.js b/frontend/vite.config.js index 234fd74c7..f75a8ddd7 100644 --- a/frontend/vite.config.js +++ b/frontend/vite.config.js @@ -37,6 +37,12 @@ export default defineConfig({ emptyOutDir: true, outDir: resolve(__dirname, '../web/static/vite_dist') }, + server: { + strictPort: true, + fs: { + allow: ['..'] + } + }, resolve: { alias: { '!': resolve(__dirname), diff --git a/src/Service/ServiceControl.php b/src/Service/ServiceControl.php index 362592fc3..ee1ab3579 100644 --- a/src/Service/ServiceControl.php +++ b/src/Service/ServiceControl.php @@ -86,6 +86,7 @@ final class ServiceControl 'redis' => __('Cache'), 'sftpgo' => __('SFTP service'), 'centrifugo' => __('Live Now Playing updates'), + 'vite' => __('Frontend Assets'), ]; if (!$this->centrifugo->isSupported()) { @@ -100,6 +101,10 @@ final class ServiceControl unset($services['redis']); } + if (!$this->environment->isDevelopment()) { + unset($services['vite']); + } + return $services; } } diff --git a/src/View.php b/src/View.php index b1cca1566..0d85f2ac5 100644 --- a/src/View.php +++ b/src/View.php @@ -67,14 +67,33 @@ final class View extends Engine } ); - $vueComponents = Json::loadFromFile($environment->getBaseDirectory() . '/web/static/vite_dist/manifest.json'); + $vueComponents = (!$environment->isDevelopment()) + ? Json::loadFromFile($environment->getBaseDirectory() . '/web/static/vite_dist/manifest.json') + : []; + $this->registerFunction( 'getVueComponentInfo', - function (string $componentPath) use ($vueComponents) { + function (string $componentPath) use ($vueComponents, $environment) { + $assetRoot = '/static/vite_dist'; + + if ($environment->isDevelopment()) { + return [ + 'js' => $assetRoot . '/' . $componentPath, + 'css' => [], + 'prefetch' => [], + ]; + } + if (!isset($vueComponents[$componentPath])) { return null; } + $includes = [ + 'js' => $assetRoot . '/' . $vueComponents[$componentPath]['file'], + 'css' => [], + 'prefetch' => [], + ]; + $assetRoot = '/static/vite_dist'; $includes = [ 'js' => $assetRoot . '/' . $vueComponents[$componentPath]['file'], diff --git a/templates/partials/head.phtml b/templates/partials/head.phtml index 2fc9ea973..8a16e935f 100644 --- a/templates/partials/head.phtml +++ b/templates/partials/head.phtml @@ -1,8 +1,15 @@ isDevelopment()) { + echo << + HTML; +} + $componentDeps = $this->getVueComponentInfo('js/layout.js'); echo <<