diff --git a/.env b/.env index ede15fff02..42eb831815 100644 --- a/.env +++ b/.env @@ -1,3 +1,4 @@ ENVIRONMENT=devel PORT=8407 LXD_UI_BACKEND_IP=172.17.0.1 +VITE_PORT=3000 \ No newline at end of file diff --git a/.github/workflows/coverage.yaml b/.github/workflows/coverage.yaml index 7066d88745..7d8aa76b8c 100644 --- a/.github/workflows/coverage.yaml +++ b/.github/workflows/coverage.yaml @@ -33,6 +33,7 @@ jobs: ENVIRONMENT: devel PORT: 8407 LXD_UI_BACKEND_IP: 172.17.0.1 + VITE_PORT: 3000 run: | dotrun & curl --head --fail --retry-delay 2 --retry 100 --retry-connrefused --insecure https://localhost:8407 diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml index 21f63f6664..235c8f0418 100644 --- a/.github/workflows/pr.yaml +++ b/.github/workflows/pr.yaml @@ -90,6 +90,7 @@ jobs: ENVIRONMENT: devel PORT: 8407 LXD_UI_BACKEND_IP: 172.17.0.1 + VITE_PORT: 3000 run: | dotrun & curl --head --fail --retry-delay 2 --retry 100 --retry-connrefused --insecure https://localhost:8407 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index dad2537bd3..a221634f10 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -82,6 +82,9 @@ You should enable a firewall as `dotrun` exposes an api to start or interact with unprivileged containers on your public ip via port `8407`. Ensure that the lxd API on port `8443` is open, so `dotrun` can access it. + For local development, the UI is served by the Vite development server, which uses port 3000 by default. + If this port conflicts with another process on your system, you can set a different port by specifying `VITE_PORT` in the `.env.local` file. + The first time, running `dotrun` will generate certificates for you. You can find them in the `keys` folder on the top level of the repo. Trust them from your local `lxc` with diff --git a/entrypoint b/entrypoint index 5de2c1a8b8..2dd41f3358 100755 --- a/entrypoint +++ b/entrypoint @@ -1,26 +1,37 @@ #! /usr/bin/env bash set -e +load_env_file() { + if [ -f "$1" ]; then + set -o allexport + source "$1" + set +o allexport + fi +} + +load_env_file .env +load_env_file .env.local +# default to 3000 if VITE_PORT is not set, in demo environment +VITE_PORT="${VITE_PORT:-3000}" + # demo config if [[ ! -z "$LXD_UI_BACKEND_KEY_PEM" ]]; then printf %s "$LXD_UI_BACKEND_KEY_PEM" > key.pem cp haproxy-demo.cfg /tmp/haproxy-local.cfg sed -i "s#LXD_UI_BACKEND_IP#$LXD_UI_BACKEND_IP#" /tmp/haproxy-local.cfg sed -i "s#LXD_UI_BACKEND_SECRET#$LXD_UI_BACKEND_SECRET#" /tmp/haproxy-local.cfg + sed -i "s#VITE_PORT#$VITE_PORT#" /tmp/haproxy-local.cfg . /usr/local/nvm/nvm.sh haproxy -f /tmp/haproxy-local.cfg - npx serve --single --no-clipboard -l 3000 build + npx serve --single --no-clipboard -l $VITE_PORT build # dev config else cp haproxy-dev.cfg /tmp/haproxy-local.cfg - set -o allexport; source .env; set +o allexport - if [ -f .env.local ] - then - set -o allexport; source .env.local; set +o allexport - fi sed -i "s#LXD_UI_BACKEND_IP#$LXD_UI_BACKEND_IP#" /tmp/haproxy-local.cfg + sed -i "s#VITE_PORT#$VITE_PORT#" /tmp/haproxy-local.cfg + # generate certificates for dev environment if [ ! -d "keys" ]; then mkdir -p keys diff --git a/haproxy-demo.cfg b/haproxy-demo.cfg index e7dd7bb00d..995ffa990b 100644 --- a/haproxy-demo.cfg +++ b/haproxy-demo.cfg @@ -19,7 +19,7 @@ frontend lxd_frontend default_backend lxd_ui backend lxd_ui - server yarn_serve_port 127.0.0.1:3000 + server yarn_serve_port 127.0.0.1:VITE_PORT backend lxd_core server lxd_https LXD_UI_BACKEND_IP:8443 ssl verify none crt /srv/key.pem diff --git a/haproxy-dev.cfg b/haproxy-dev.cfg index 486569c447..b61dd92b51 100644 --- a/haproxy-dev.cfg +++ b/haproxy-dev.cfg @@ -16,7 +16,7 @@ frontend lxd_frontend default_backend lxd_ui backend lxd_ui - server yarn_serve_port 127.0.0.1:3000 + server yarn_serve_port 127.0.0.1:VITE_PORT backend lxd_core server lxd_https LXD_UI_BACKEND_IP:8443 ssl verify none crt keys/lxd-ui.pem diff --git a/package.json b/package.json index 9dc77e2741..037a7ca685 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "lint-js": "tsc --noEmit && yarn lint-js-eslint && yarn lint-js-prettier", "hooks-add": "husky install", "hooks-remove": "husky uninstall", - "start": "concurrently --kill-others --raw 'vite --host | grep -v 3000' 'yarn serve'", + "start": "concurrently --kill-others --raw 'vite --host | grep -v http:' 'yarn serve'", "serve": "./entrypoint", "test-js": "vitest --run", "test-e2e-edge": "tests/scripts/setup_test && npx playwright test --project chromium:lxd-latest-edge firefox:lxd-latest-edge && tests/scripts/teardown_test", diff --git a/vite.config.ts b/vite.config.ts index 4a763715d2..e39aba0efd 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,6 +1,15 @@ import { defineConfig } from "vite"; import react from "@vitejs/plugin-react"; import tsconfigPaths from "vite-tsconfig-paths"; +import * as dotenv from "dotenv"; +import * as fs from "fs"; + +// Load .env.local if it exists, otherwise load .env +if (fs.existsSync(".env.local")) { + dotenv.config({ path: ".env.local" }); +} else { + dotenv.config({ path: ".env" }); +} export default defineConfig({ css: { @@ -13,7 +22,8 @@ export default defineConfig({ }, plugins: [tsconfigPaths(), react()], server: { - port: 3000, + port: process.env.VITE_PORT ? Number(process.env.VITE_PORT) : 3000, + strictPort: true, proxy: { "/ui/assets": { target: "https://localhost:8407/",