基于 Puppeteer 的网页截图服务,支持高并发、文件缓存。
docker run -d \
-p 8080:8080 \
--name shot \
docker.cnb.cool/shellingford/shot:latest
访问 http://localhost:8080/screenshot?url=https://www.example.com 即可获取截图。
通过环境变量或挂载配置文件自定义配置:
docker run -d \
-p 8080:8080 \
-v ./config.js:/app/src/config.js \
--name shot \
docker.cnb.cool/shellingford/shot:latest
docker build -t shot .
npm install
npm start
服务默认运行在 http://localhost:8080
GET /screenshot?url=<目标 URL>&width=<宽度>&height=<高度>
参数说明:
| 参数 | 必填 | 默认值 | 说明 |
|---|---|---|---|
| url | 是 | - | 目标网页 URL |
| width | 否 | 1280 | 视口宽度 |
| height | 否 | 720 | 视口高度 |
请求示例:
# 默认尺寸
curl -o screenshot.jpg "http://localhost:8080/screenshot?url=https://www.example.com"
# 自定义尺寸
curl -o screenshot.jpg "http://localhost:8080/screenshot?url=https://www.example.com&width=1920&height=1080"
响应头:
X-Screenshot-From-Cache: true/false 表示是否命中缓存GET /health
GET /status
返回浏览器资源池和缓存统计信息。
GET /cache/stats
POST /cache/cleanup
编辑 src/config.js 修改配置:
module.exports = {
port: 8080, // 服务端口
screenshot: {
format: 'jpeg', // 图片格式
quality: 80, // JPG 质量 (1-100)
width: 1280, // 默认宽度
height: 720, // 默认高度
timeout: 15000, // 导航超时时间 (ms)
waitUntil: 'load', // 等待事件:'load' 或 'domcontentloaded'
extraWait: 3000, // 额外等待时间 (ms)
fontWaitTimeout: 3000, // 字体加载最大等待时间 (ms)
},
cache: {
ttl: 3600000, // 缓存时长 (ms)
dir: './cache', // 缓存目录
},
pool: {
maxPages: 5, // 最大并发数
minPages: 1, // 最小页面数
}
}
shot/
├── src/
│ ├── config.js # 配置文件
│ ├── browser-pool.js # 浏览器资源池
│ ├── cache-manager.js # 缓存管理
│ ├── screenshot-service.js # 截图服务
│ ├── server.js # HTTP 服务
│ └── index.js # 入口文件
├── cache/ # 缓存目录(自动生成)
├── package.json
└── README.md