Skip to content

Commit 91d44b9

Browse files
committed
chore: 更新环境变量配置,添加 Puppeteer 可执行路径,优化 Dockerfile 构建流程
1 parent 96e49f2 commit 91d44b9

File tree

9 files changed

+82
-43
lines changed

9 files changed

+82
-43
lines changed

.env.development

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
JWT_SECRET="42910EBF48299407B572A1521A73CCD73AD95204"
2-
SERVER_PORT="10010"
2+
SERVER_PORT="10010"
3+
PUPPETEER_EXECUTABLE_PATH="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"

.env.production

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
JWT_SECRET="3FD9F4FF47CBFCD410039B80ADE1452613B66C59"
2-
SERVER_PORT="7777"
2+
SERVER_PORT="7777"
3+
PUPPETEER_EXECUTABLE_PATH="/usr/bin/chromium-browser"

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
22

3+
# building block
4+
entry
5+
36
# dotenvx private key
47
.env.keys
58

README.md

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,36 @@
3535

3636
### 标准接口
3737

38-
> /pv 地址简化版
38+
| 接口路径 | 请求方法 | Content-Type | 请求参数示例 | 说明 |
39+
| ------------------ | -------- | ---------------- | ------------------------------------------------------------------------ | ------------------------ |
40+
| /pv | POST | application/json | {<br> &nbsp;&nbsp; "url": "https://www.xiaohongshu.com/explore/xxx"<br>} | 解析短视频接口,简化地址 |
41+
| /public/parseVideo | POST | application/json | {<br> &nbsp;&nbsp; "url": "https://www.xiaohongshu.com/explore/xxx"<br>} | 解析短视频接口 |
3942

40-
```http
41-
POST /public/parseVideo
42-
Content-Type: application/json
43+
---
44+
45+
## 环境变量说明
46+
47+
- `PORT`:服务监听端口(开发环境建议 10010,生产环境建议 7777)
48+
- `PUPPETEER_EXECUTABLE_PATH`:指定 Puppeteer 启动时所用 Chromium/Chrome 浏览器的绝对路径,建议在 Docker 或服务器环境下设置为 `/usr/bin/chromium-browser`,否则 Puppeteer 可能无法正常启动。
49+
50+
## 本地运行
51+
52+
### 安装依赖
53+
54+
```bash
55+
bun install
56+
```
57+
58+
### 直接运行源代码
4359

44-
{
45-
"url": "https://v.douyin.com/xxxxx/"
46-
}
60+
```bash
61+
bun run dev
62+
```
63+
64+
### 编译并运行
65+
66+
```bash
67+
bun run build-local && bun run start
4768
```
4869

4970
## Docker 镜像快速使用
@@ -71,7 +92,7 @@ docker run -d \
7192

7293
---
7394

74-
如需更多高级用法(如数据持久化、日志挂载、反向代理等),请参考[详细部署文档](./docs/DEPLOYMENT.md)
95+
详情内容请参考[详细部署文档](./docs/DEPLOYMENT.md)
7596

7697
如遇镜像拉取缓慢,可参考[镜像源配置指南](./docs/DOCKER_MIRROR.md)加速 Docker 镜像下载。
7798

dockerfile

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
# 第一阶段:构建阶段
2+
FROM oven/bun:1.1.21-alpine AS builder
3+
4+
WORKDIR /app
5+
6+
COPY package.json bun.lockb ./
7+
RUN bun install --frozen-lockfile
8+
9+
COPY . .
10+
# 先删除旧的构建产物
11+
RUN rm -rf ./src/entry && bun run build
12+
13+
# 第二阶段:生产镜像
114
FROM oven/bun:1.1.21-alpine
215

3-
# 安装 Node.js 和 Chromium 依赖
416
RUN apk add --no-cache \
5-
nodejs \
617
chromium \
718
chromium-chromedriver \
819
fontconfig \
@@ -16,33 +27,20 @@ RUN apk add --no-cache \
1627
wget \
1728
ca-certificates
1829

19-
# Puppeteer 无头浏览器参数建议加上
20-
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
21-
22-
# 设置工作目录
2330
WORKDIR /app
2431

25-
# 复制 package.json 和 bun.lockb
26-
COPY package.json bun.lockb ./
27-
28-
# 安装依赖
29-
RUN bun install --frozen-lockfile
30-
31-
# 复制源代码
32-
COPY . .
33-
34-
# 安装 PM2
35-
RUN bun add -g pm2
36-
37-
# 创建日志目录
38-
RUN mkdir -p logs
32+
# 只复制构建产物和依赖
33+
COPY --from=builder /app/package.json /app/bun.lockb ./
34+
COPY --from=builder /app/node_modules ./node_modules
35+
COPY --from=builder /app/src/entry ./src/entry
36+
COPY --from=builder /app/ecosystem.config.json ./
37+
COPY --from=builder /app/.env.production ./
3938

40-
# 设置 Puppeteer 环境变量
4139
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
42-
PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
40+
PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser \
41+
PORT=7777
4342

44-
# 暴露端口
45-
EXPOSE 7777
43+
# 用环境变量控制端口暴露
44+
EXPOSE ${PORT}
4645

47-
# 使用 PM2 Runtime 启动应用
48-
CMD ["pm2-runtime", "ecosystem.config.json"]
46+
CMD ["bun", "run", "start"]

docs/DEPLOYMENT.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,22 @@ docker run -d \
6464

6565
## 环境变量说明
6666

67-
必需的环境变量:
68-
69-
- `PORT`: 服务器端口号(开发环境:10010, 生产环境:7777),具体可在环境变量文件查看
67+
- `PORT`:服务监听端口(开发环境建议 10010,生产环境建议 7777)
68+
- `PUPPETEER_EXECUTABLE_PATH`:指定 Puppeteer 启动时所用 Chromium/Chrome 浏览器的绝对路径,建议在 Docker 或服务器环境下设置为 `/usr/bin/chromium-browser`,否则 Puppeteer 可能无法正常启动。
69+
70+
## 常用脚本说明
71+
72+
- `bun run dev`:开发环境启动服务,自动加载 .env.development 环境变量。
73+
- `bun run build-local`:本地开发环境编译打包。
74+
- `bun run build`:生产环境编译打包,生成 Linux ARM64 可执行文件。
75+
- `bun run start`:直接运行编译后的入口文件。
76+
- `bun run format`:使用 Prettier 格式化全部代码。
77+
- `bun run encrypt-dev` / `bun run encrypt-prod`:加密开发/生产环境变量文件。
78+
- `bun run decrypt-dev` / `bun run decrypt-prod`:解密开发/生产环境变量文件。
79+
- `bun run dotenvx-help`:显示 dotenvx 版本及帮助信息。
80+
- `bun run pm2:dev`:用 PM2 启动开发环境。
81+
- `bun run pm2:start`:用 PM2 启动生产环境。
82+
- `bun run pm2:stop` / `bun run pm2:restart` / `bun run pm2:logs` / `bun run pm2:status`:PM2 进程管理相关命令。
7083

7184
## 更新维护
7285

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"type": "module",
55
"scripts": {
66
"dev": "dotenvx run -f .env.development -- bun --watch src/index.ts",
7-
"preview": "dotenvx run -f .env.production -- bun --watch src/index.ts",
8-
"start": "dotenvx run -f .env.production --overload --debug -- bun --watch src/index.ts",
9-
"test": "dotenvx run -f .env.development -- bun test",
7+
"build-local": "dotenvx run -f .env.development -- bun build ./src/index.ts --compile --outfile ./src/entry",
8+
"build": "dotenvx run -f .env.production -- bun build ./src/index.ts --compile --minify --target=bun-linux-amd64 --outfile ./src/entry",
9+
"start": "./src/entry",
1010
"prepare-fallback": "husky install",
1111
"format": "prettier --write .",
1212
"separate-1": "------------------------------------------------------------",

src/bootstrap/server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export const startServer = () => {
44
const PORT = Bun.env.SERVER_PORT || 7777;
55
const app = createApp();
66

7+
console.log(Bun.env.PUPPETEER_EXECUTABLE_PATH);
8+
79
app.listen(PORT, () =>
810
console.log(`Server is running on http://localhost:${PORT}`),
911
);

src/controllers/core/xiaohongshu.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export async function parseXiaohongshuContent({
3636
try {
3737
console.log("启动Puppeteer浏览器...");
3838
const browser = await puppeteer.launch({
39-
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH,
39+
executablePath: Bun.env.PUPPETEER_EXECUTABLE_PATH,
4040
headless: true,
4141
args: [
4242
"--no-sandbox",

0 commit comments

Comments
 (0)