diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..328053669 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,42 @@ +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 theme.json ./ +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 + +# 安装生产环境依赖,并确保包含 vite +RUN npm ci && npm install vite + +# 设置环境变量 +ENV NODE_ENV=production +ENV PORT=7788 + +# 暴露端口 +EXPOSE 7788 + +# 启动应用 +CMD ["npm", "start"] \ No newline at end of file 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 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" ); } 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}`); });