Skip to content
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

Add Docker Support and Optimize Port Configuration #4

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 3 additions & 7 deletions server/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}

Expand Down
5 changes: 2 additions & 3 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
});
Expand Down