From 144b119b741939ea62787017ffaca50b36772be7 Mon Sep 17 00:00:00 2001 From: jiangjiwei Date: Mon, 6 Jan 2025 14:29:01 +0800 Subject: [PATCH 1/6] fix: add support for configurable port via environment variable --- server/index.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/server/index.ts b/server/index.ts index 46b657c7c..344c05fed 100644 --- a/server/index.ts +++ b/server/index.ts @@ -69,9 +69,8 @@ app.use((req, res, next) => { serveStatic(app); } - // ALWAYS serve the app on port 3000 - // this serves both the API and the client - const PORT = 3000; + // ALWAYS serve the app on port 3000 unless specified in environment + const PORT = Number(process.env.PORT) || 3000; server.listen(PORT, "0.0.0.0", () => { log(`serving on port ${PORT}`); }); From cd0ceca35ef94122574beb3e746f021a2536b752 Mon Sep 17 00:00:00 2001 From: jiangjiwei Date: Mon, 6 Jan 2025 14:29:05 +0800 Subject: [PATCH 2/6] docs: update port configuration in README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1f0d3775c..9f14b30ba 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ Created by [@ammaar](https://x.com/ammaar) - `GOOGLE_API_KEY`: Your Google API key with access to Gemini API - `NODE_ENV`: Set to "development" by default, use "production" for production builds +- `PORT`: Server port (defaults to 3000) ## Development From 01a35c8c5b4c891d5a211acf8256632247eb41a0 Mon Sep 17 00:00:00 2001 From: jiangjiwei Date: Mon, 6 Jan 2025 14:29:08 +0800 Subject: [PATCH 3/6] feat: add Dockerfile for containerization --- Dockerfile | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..89fc9a5c6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,41 @@ +FROM node:20-alpine as builder + +WORKDIR /app + +# 复制项目文件 +COPY package*.json ./ +COPY tsconfig.json ./ +COPY vite.config.ts ./ +COPY postcss.config.js ./ +COPY tailwind.config.ts ./ +COPY drizzle.config.ts ./ +COPY client/ ./client/ +COPY server/ ./server/ +COPY db/ ./db/ + +# 安装依赖并构建 +RUN npm ci +RUN npm run build + +# 生产环境 +FROM node:20-alpine + +WORKDIR /app + +# 复制构建产物和必要文件 +COPY --from=builder /app/dist ./dist +COPY --from=builder /app/package*.json ./ +COPY --from=builder /app/db ./db + +# 仅安装生产环境依赖 +RUN npm ci --production + +# 设置环境变量 +ENV NODE_ENV=production +ENV PORT=7788 + +# 暴露端口 +EXPOSE 7788 + +# 启动应用 +CMD ["npm", "start"] \ No newline at end of file From b4f8c4eda518effe3bcb9e9cbb11aeb5195dc1d7 Mon Sep 17 00:00:00 2001 From: jiangjiwei Date: Mon, 6 Jan 2025 14:36:11 +0800 Subject: [PATCH 4/6] fix: add theme.json to Docker build context to fix vite build error --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index 89fc9a5c6..8e892237f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,6 +9,7 @@ COPY vite.config.ts ./ COPY postcss.config.js ./ COPY tailwind.config.ts ./ COPY drizzle.config.ts ./ +COPY theme.json ./ COPY client/ ./client/ COPY server/ ./server/ COPY db/ ./db/ From 6ee256ed12e2efe41070554be1bbce2dbbf62974 Mon Sep 17 00:00:00 2001 From: jiangjiwei Date: Mon, 6 Jan 2025 14:39:08 +0800 Subject: [PATCH 5/6] fix: ensure vite package is available in production environment --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8e892237f..328053669 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,8 +28,8 @@ COPY --from=builder /app/dist ./dist COPY --from=builder /app/package*.json ./ COPY --from=builder /app/db ./db -# 仅安装生产环境依赖 -RUN npm ci --production +# 安装生产环境依赖,并确保包含 vite +RUN npm ci && npm install vite # 设置环境变量 ENV NODE_ENV=production From e5369b7fc448161010347db2066c85af5fcab886 Mon Sep 17 00:00:00 2001 From: jiangjiwei Date: Mon, 6 Jan 2025 14:48:32 +0800 Subject: [PATCH 6/6] refactor: make env file optional and use environment variables directly --- server/env.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/server/env.ts b/server/env.ts index 3259812b1..cc72895ea 100644 --- a/server/env.ts +++ b/server/env.ts @@ -7,16 +7,12 @@ const __dirname = path.dirname(__filename); const envPath = path.resolve(__dirname, "../.env"); export function setupEnvironment() { - const result = dotenv.config({ path: envPath }); - if (result.error) { - throw new Error( - `Failed to load .env file from ${envPath}: ${result.error.message}` - ); - } + // 尝试加载 .env 文件,但不强制要求 + dotenv.config({ path: envPath }); if (!process.env.GOOGLE_API_KEY) { throw new Error( - "GOOGLE_API_KEY environment variable must be set in .env file" + "GOOGLE_API_KEY environment variable must be set" ); }