Skip to content

feat(dev): extract ports to env config and optimize startup order#25

Open
mmletgo wants to merge 1 commit into
ExplosiveCoderflome:mainfrom
mmletgo:feat/dev-port-config-and-startup-order
Open

feat(dev): extract ports to env config and optimize startup order#25
mmletgo wants to merge 1 commit into
ExplosiveCoderflome:mainfrom
mmletgo:feat/dev-port-config-and-startup-order

Conversation

@mmletgo
Copy link
Copy Markdown

@mmletgo mmletgo commented Apr 19, 2026

Summary

  • Extract hardcoded development ports (3000/5173) to environment variables (SERVER_PORT, VITE_DEV_PORT, VITE_API_PORT) for easy configuration and conflict avoidance
  • Optimize dev startup order: server starts first, then shared + client start concurrently after server is ready, preventing ts-node-dev and shared tsc watch from competing for resources during cold start
  • Increase wait-for-port timeout from 120s to 300s to accommodate slow first-time compilation

Changes

File Change
server/src/app.ts Default port fallback 3000 → 3001
client/vite.config.ts Read dev server port from VITE_DEV_PORT env
client/src/lib/constants.ts API port fallback reads from VITE_API_PORT env
client/.env.example Document VITE_API_BASE_URL and VITE_DEV_PORT
scripts/wait-for-port.cjs Support SERVER_PORT env, increase timeout to 300s
package.json Add dev:after-server script, restructure dev:raw startup order

Test plan

  • pnpm dev starts server first, shared + client start after server is ready
  • Ports are configurable via environment variables without code changes
  • Frontend correctly connects to backend API on the configured port

🤖 Generated with Claude Code

- Move hardcoded ports (3000/5173) to env vars (SERVER_PORT/VITE_DEV_PORT)
- Add dev:after-server script to start shared+client only after server is ready
- Increase wait-for-port timeout from 120s to 300s for slow cold starts
- Avoid ts-node-dev and shared tsc watch competing for resources concurrently

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings April 19, 2026 11:06
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts the development-time port and startup orchestration so the backend comes up first, then the shared package and frontend start after the server is reachable, while also moving toward env-configurable ports.

Changes:

  • Switch server default port to 3001 and update client defaults accordingly.
  • Add wait-for-port support for env-driven server port and extend its timeout.
  • Restructure pnpm dev startup to start server first, then start shared + client after readiness.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
server/src/app.ts Updates the server’s default port fallback to 3001.
scripts/wait-for-port.cjs Uses env-driven default port and increases timeout for slow cold starts.
package.json Reorders dev startup and adds a post-server step for shared+client.
client/vite.config.ts Makes the Vite dev server port configurable via env.
client/src/lib/constants.ts Allows API port to be driven by VITE_API_PORT with a new 3001 default.
client/.env.example Updates documented frontend env overrides for dev.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 12 to 16
const configuredBaseUrl = import.meta.env.VITE_API_BASE_URL?.trim();
const apiPort = import.meta.env.VITE_API_PORT?.trim() || "3001";
if (!import.meta.env.DEV || typeof window === "undefined") {
return configuredBaseUrl || "http://localhost:3000/api";
return configuredBaseUrl || `http://localhost:${apiPort}/api`;
}
parsed.hostname = window.location.hostname;
if (!parsed.port) {
parsed.port = "3000";
parsed.port = import.meta.env.VITE_API_PORT?.trim() || "3001";
Comment thread client/.env.example
Comment on lines +2 to +3
# API 服务地址(包含端口),默认 http://localhost:3001/api
VITE_API_BASE_URL=http://localhost:3001/api
Comment thread scripts/wait-for-port.cjs
: [...DEFAULT_HOSTS],
port: 3000,
timeoutMs: 120000,
port: Number(process.env.SERVER_PORT) || 3001,
Comment thread scripts/wait-for-port.cjs

function printHelp() {
console.log("Usage: node scripts/wait-for-port.cjs [--host 127.0.0.1,localhost,::1] [--port 3000] [--timeout 120000] [--interval 500]");
console.log("Usage: node scripts/wait-for-port.cjs [--host 127.0.0.1,localhost,::1] [--port 3001] [--timeout 300000] [--interval 500]");
Comment thread package.json
"dev:server:log": "node scripts/run-with-log.cjs --name server -- pnpm --filter @ai-novel/server dev",
"dev:client": "pnpm --filter @ai-novel/client dev",
"dev:client:wait": "node scripts/wait-for-port.cjs --port 3000 && pnpm dev:client",
"dev:client:wait": "node scripts/wait-for-port.cjs --port 3001 && pnpm dev:client",
Comment thread client/vite.config.ts
Comment on lines 65 to 68
server: {
host: true, // 允许局域网访问(监听 0.0.0.0)
port: Number(process.env.VITE_DEV_PORT) || 5174,
host: true,
},
Comment thread client/.env.example
Comment on lines +2 to 7
# API 服务地址(包含端口),默认 http://localhost:3001/api
VITE_API_BASE_URL=http://localhost:3001/api
# Vite 开发服务器端口
VITE_DEV_PORT=5174
# 默认 10 分钟,可按需要调大。
VITE_API_TIMEOUT_MS=600000
Comment thread server/src/app.ts
Comment on lines 161 to 165
const ragCompatibilityReport = await initializeRagSettingsCompatibility();
const app = createApp();
const port = Number(process.env.PORT ?? 3000);
const port = Number(process.env.PORT ?? 3001);
const allowLan = parseEnvFlag(process.env.ALLOW_LAN, process.env.NODE_ENV !== "production");
const host = process.env.HOST ?? (allowLan ? "0.0.0.0" : "localhost");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants