Do not hardcode 32100 port (#1969)
Fixes #1949 <!-- CURSOR_SUMMARY --> --- > [!NOTE] > <sup>[Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) is generating a summary for commit 46ac310c762fd4044c35bc59264122234ed19bbf. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Make app ports dynamic instead of hardcoded 32100 to prevent conflicts and keep local runs, Docker, and env URLs in sync. Ports now derive from appId using a base of 32100. - **Bug Fixes** - Added getAppPort(appId) = 32100 + (appId % 10_000). - Used the dynamic port in NEXT_PUBLIC_SERVER_URL, start commands, Docker -p mapping, and cleanUpPort. - Updated getCommand to accept appId and generate a per-app default start command. <sup>Written for commit 46ac310c762fd4044c35bc59264122234ed19bbf. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. -->
This commit is contained in:
7
shared/ports.ts
Normal file
7
shared/ports.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
/**
|
||||||
|
* Calculate the port for a given app based on its ID.
|
||||||
|
* Uses a base port of 32100 and offsets by appId % 10_000.
|
||||||
|
*/
|
||||||
|
export function getAppPort(appId: number): number {
|
||||||
|
return 32100 + (appId % 10_000);
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { IpcClient } from "@/ipc/ipc_client";
|
import { IpcClient } from "@/ipc/ipc_client";
|
||||||
|
import { getAppPort } from "../../shared/ports";
|
||||||
|
|
||||||
import { v4 as uuidv4 } from "uuid";
|
import { v4 as uuidv4 } from "uuid";
|
||||||
|
|
||||||
@@ -29,7 +30,7 @@ export async function neonTemplateHook({
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: "NEXT_PUBLIC_SERVER_URL",
|
key: "NEXT_PUBLIC_SERVER_URL",
|
||||||
value: "http://localhost:32100",
|
value: `http://localhost:${getAppPort(appId)}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: "GMAIL_USER",
|
key: "GMAIL_USER",
|
||||||
|
|||||||
@@ -62,8 +62,12 @@ import { getVercelTeamSlug } from "../utils/vercel_utils";
|
|||||||
import { storeDbTimestampAtCurrentVersion } from "../utils/neon_timestamp_utils";
|
import { storeDbTimestampAtCurrentVersion } from "../utils/neon_timestamp_utils";
|
||||||
import { AppSearchResult } from "@/lib/schemas";
|
import { AppSearchResult } from "@/lib/schemas";
|
||||||
|
|
||||||
const DEFAULT_COMMAND =
|
import { getAppPort } from "../../../shared/ports";
|
||||||
"(pnpm install && pnpm run dev --port 32100) || (npm install --legacy-peer-deps && npm run dev -- --port 32100)";
|
|
||||||
|
function getDefaultCommand(appId: number): string {
|
||||||
|
const port = getAppPort(appId);
|
||||||
|
return `(pnpm install && pnpm run dev --port ${port}) || (npm install --legacy-peer-deps && npm run dev -- --port ${port})`;
|
||||||
|
}
|
||||||
async function copyDir(
|
async function copyDir(
|
||||||
source: string,
|
source: string,
|
||||||
destination: string,
|
destination: string,
|
||||||
@@ -150,7 +154,7 @@ async function executeAppLocalNode({
|
|||||||
installCommand?: string | null;
|
installCommand?: string | null;
|
||||||
startCommand?: string | null;
|
startCommand?: string | null;
|
||||||
}): Promise<void> {
|
}): Promise<void> {
|
||||||
const command = getCommand({ installCommand, startCommand });
|
const command = getCommand({ appId, installCommand, startCommand });
|
||||||
const spawnedProcess = spawn(command, [], {
|
const spawnedProcess = spawn(command, [], {
|
||||||
cwd: appPath,
|
cwd: appPath,
|
||||||
shell: true,
|
shell: true,
|
||||||
@@ -418,6 +422,7 @@ RUN npm install -g pnpm
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Run the Docker container
|
// Run the Docker container
|
||||||
|
const port = getAppPort(appId);
|
||||||
const process = spawn(
|
const process = spawn(
|
||||||
"docker",
|
"docker",
|
||||||
[
|
[
|
||||||
@@ -426,7 +431,7 @@ RUN npm install -g pnpm
|
|||||||
"--name",
|
"--name",
|
||||||
containerName,
|
containerName,
|
||||||
"-p",
|
"-p",
|
||||||
"32100:32100",
|
`${port}:${port}`,
|
||||||
"-v",
|
"-v",
|
||||||
`${appPath}:/app`,
|
`${appPath}:/app`,
|
||||||
"-v",
|
"-v",
|
||||||
@@ -438,7 +443,7 @@ RUN npm install -g pnpm
|
|||||||
`dyad-app-${appId}`,
|
`dyad-app-${appId}`,
|
||||||
"sh",
|
"sh",
|
||||||
"-c",
|
"-c",
|
||||||
getCommand({ installCommand, startCommand }),
|
getCommand({ appId, installCommand, startCommand }),
|
||||||
],
|
],
|
||||||
{
|
{
|
||||||
stdio: "pipe",
|
stdio: "pipe",
|
||||||
@@ -817,8 +822,8 @@ export function registerAppHandlers() {
|
|||||||
|
|
||||||
const appPath = getDyadAppPath(app.path);
|
const appPath = getDyadAppPath(app.path);
|
||||||
try {
|
try {
|
||||||
// There may have been a previous run that left a process on port 32100.
|
// There may have been a previous run that left a process on this port.
|
||||||
await cleanUpPort(32100);
|
await cleanUpPort(getAppPort(appId));
|
||||||
await executeApp({
|
await executeApp({
|
||||||
appPath,
|
appPath,
|
||||||
appId,
|
appId,
|
||||||
@@ -918,8 +923,8 @@ export function registerAppHandlers() {
|
|||||||
logger.log(`App ${appId} not running. Proceeding to start.`);
|
logger.log(`App ${appId} not running. Proceeding to start.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// There may have been a previous run that left a process on port 32100.
|
// There may have been a previous run that left a process on this port.
|
||||||
await cleanUpPort(32100);
|
await cleanUpPort(getAppPort(appId));
|
||||||
|
|
||||||
// Now start the app again
|
// Now start the app again
|
||||||
const app = await db.query.apps.findFirst({
|
const app = await db.query.apps.findFirst({
|
||||||
@@ -1573,16 +1578,18 @@ export function registerAppHandlers() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getCommand({
|
function getCommand({
|
||||||
|
appId,
|
||||||
installCommand,
|
installCommand,
|
||||||
startCommand,
|
startCommand,
|
||||||
}: {
|
}: {
|
||||||
|
appId: number;
|
||||||
installCommand?: string | null;
|
installCommand?: string | null;
|
||||||
startCommand?: string | null;
|
startCommand?: string | null;
|
||||||
}) {
|
}) {
|
||||||
const hasCustomCommands = !!installCommand?.trim() && !!startCommand?.trim();
|
const hasCustomCommands = !!installCommand?.trim() && !!startCommand?.trim();
|
||||||
return hasCustomCommands
|
return hasCustomCommands
|
||||||
? `${installCommand!.trim()} && ${startCommand!.trim()}`
|
? `${installCommand!.trim()} && ${startCommand!.trim()}`
|
||||||
: DEFAULT_COMMAND;
|
: getDefaultCommand(appId);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function cleanUpPort(port: number) {
|
async function cleanUpPort(port: number) {
|
||||||
|
|||||||
Reference in New Issue
Block a user