-
Notifications
You must be signed in to change notification settings - Fork 9
Port offset script to support running multiple instances #5295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Environment Variables | ||
|
|
||
| Environment variables are defined in `.env` (committed defaults) at the repo root. | ||
| For demo services, port/URL overrides go in `demo/.env.local` (gitignored), which is symlinked into `demo/api/`, `demo/admin/`, and `demo/site/`. | ||
| A root-level `.env.local` (also gitignored) can hold non-demo overrides (e.g. `MUI_LICENSE_KEY`). | ||
|
|
||
| ## Adding a new environment variable | ||
|
|
||
| When adding a variable to `.env`, also update `set-ports.js` if applicable: | ||
|
|
||
| ### Add to `PORT_VARS` in `set-ports.js` if: | ||
| - The variable holds a plain port number (e.g. `MY_SERVICE_PORT=1234`) | ||
|
|
||
| ### Add to `URL_VARS` in `set-ports.js` if: | ||
| - The variable holds a URL, address, or any value that references a port variable | ||
| (e.g. `MY_SERVICE_URL=http://localhost:${MY_SERVICE_PORT}`) | ||
| - This includes HTTP/HTTPS URLs, host:port strings, comma-separated upstream lists, redirect URLs, etc. | ||
|
|
||
| ### Do NOT add to `set-ports.js` if: | ||
| - The variable is a secret, credential, feature flag, or non-URL string | ||
| - The value does not contain or reference a port number | ||
|
|
||
| ## Checklist when adding a new service | ||
|
|
||
| 1. Add `MY_SERVICE_PORT=<default>` to `.env` | ||
| 2. Add `'MY_SERVICE_PORT'` to `PORT_VARS` in `set-ports.js` | ||
| 3. If you also add URL/address variables referencing that port, add them to `URL_VARS` in `set-ports.js` | ||
| 4. Keep array ordering consistent with `.env` for readability |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -131,6 +131,17 @@ It is also possible to start specific microservices | |||||||
| pnpm exec dev-pm start @demo-api # (@demo-api|@demo-admin|@demo-site) | ||||||||
| ``` | ||||||||
|
|
||||||||
| #### Port Offset (running multiple apps simultaneously) | ||||||||
|
|
||||||||
| All Comet DXP demo services share the same default ports (API: 4000, Admin: 8001, Site: 3000, etc.). | ||||||||
| To run two instances at the same time, shift one app's ports by an integer offset: | ||||||||
|
|
||||||||
| pnpm run set-ports -- 100 # shift all ports by +100 (writes demo/.env.local) | ||||||||
| pnpm run set-ports -- 0 # reset to default port values | ||||||||
|
|
||||||||
|
Comment on lines
+139
to
+141
|
||||||||
| The script rewrites `demo/.env.local` with the new port and URL values. | ||||||||
| Since `demo/.env.local` is symlinked into `demo/api/`, `demo/admin/`, and `demo/site/`, one command covers the whole demo application. | ||||||||
|
|
||||||||
|
||||||||
| **Mailpit and Docker note:** The `set-ports` script only affects services that read their configuration from `demo/.env.local` (API, Admin, Site, etc.). Mailpit, which is started via Docker, is still mapped to fixed host ports (typically `1025` for SMTP and `8025` for the web UI) in `docker-compose.yml`. When running multiple demo stacks at the same time, you must either adjust the Mailpit port mappings for one stack (e.g. by editing `docker-compose.yml`) or disable Mailpit for that stack to avoid host port conflicts. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,7 +54,8 @@ | |
| "install-agent-skills": "pnpm exec comet install-agent-skills --config agent-skills.json", | ||
| "setup:ci": "pnpm run copy-project-files", | ||
| "setup:download-oauth2-proxy": "dotenv -- sh -c 'pnpm exec comet download-oauth2-proxy -v $OAUTH2_PROXY_VERSION'", | ||
| "setup:download-mitmproxy": "dotenv -- sh -c 'pnpm exec comet download-mitmproxy -v $MITMPROXY_VERSION'" | ||
| "setup:download-mitmproxy": "dotenv -- sh -c 'pnpm exec comet download-mitmproxy -v $MITMPROXY_VERSION'", | ||
| "set-ports": "node set-ports.js" | ||
|
||
| }, | ||
| "devDependencies": { | ||
| "@changesets/cli": "^2.29.8", | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,194 @@ | ||||||||||||||||||||||
| #!/usr/bin/env node | ||||||||||||||||||||||
| 'use strict'; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const fs = require('node:fs'); | ||||||||||||||||||||||
| const path = require('node:path'); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const ENV_FILE = path.join(__dirname, '.env'); | ||||||||||||||||||||||
| const ENV_LOCAL_FILE = path.join(__dirname, 'demo', '.env.local'); | ||||||||||||||||||||||
|
||||||||||||||||||||||
| const ENV_LOCAL_FILE = path.join(__dirname, 'demo', '.env.local'); | |
| const ENV_LOCAL_FILE = path.join(__dirname, '.env'); |
Copilot
AI
Mar 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VALKEY_PORT is used by docker-compose (valkey service binds 127.0.0.1:${VALKEY_PORT}:6379) and by the demo site at runtime, but it isn’t included in PORT_VARS. With a non-zero offset, two demo instances will still conflict on 6379 and the generated .env.local won’t keep valkey aligned with the container port. Add VALKEY_PORT to PORT_VARS (and ensure it’s also reflected wherever docker compose gets its env from).
Copilot
AI
Mar 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.claude/rules/env-vars.md asks to “Keep array ordering consistent with .env for readability”, but PORT_VARS/URL_VARS ordering doesn’t currently match the order in .env (e.g. postgres/imgproxy appear earlier in .env but later here). Reordering these arrays to follow .env would make future updates less error-prone.
Copilot
AI
Mar 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ports are computed as base + offset without validating the resulting range. Offsets that produce ports <= 0 or > 65535 will generate an unusable .env.local and potentially confusing runtime failures. Consider validating the computed ports and exiting with a clear error when an invalid range is produced.
| newPorts[portVar] = base + offset; | |
| const computedPort = base + offset; | |
| if (computedPort <= 0 || computedPort > 65535) { | |
| console.error( | |
| `Error: computed port for ${portVar} (${computedPort}) is outside the valid range 1–65535. ` + | |
| 'Check your base port and port offset configuration.', | |
| ); | |
| process.exit(1); | |
| } | |
| newPorts[portVar] = computedPort; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sentence says “Admin: 8001”, but elsewhere in the README the Demo Admin is documented as available at
http://localhost:8000/(auth-proxy). In.env,AUTHPROXY_PORTis 8000 andADMIN_PORTis 8001; the user-facing Admin URL is typically 8000. Consider clarifying which port is which (e.g., Admin via auth-proxy 8000 vs internal admin 8001) to avoid confusion.