Skip to content

Commit

Permalink
chore: add README
Browse files Browse the repository at this point in the history
  • Loading branch information
whatwewant committed Dec 21, 2023
1 parent 69fe57e commit bebc2b6
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 10 deletions.
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM whatwewant/builder-node:v20-1 as builder

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true

WORKDIR /build

COPY .npmrc .
Expand All @@ -18,7 +20,7 @@ FROM whatwewant/node:v20-2

WORKDIR /app

COPY --from=builder /build/lib /app/lib
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true

COPY .npmrc .

Expand All @@ -30,4 +32,6 @@ RUN yarn --production

COPY . .

COPY --from=builder /build/lib /app/lib

CMD yarn prod
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Puppeteer as a Service

## Getting Started

```bash
# Step 1: start browserless chrome
docker run -d -p 3000:3000 --name browserless whatwewant/browserless-chrome:v1-0

# Step 2: start puppeteer service
docker run -d -p 8080:8080 --name puppeteer-as-a-service \
-e BROWSER_WS_ENDPOINT=ws://browserless:3000 \
whatwewant/puppeteer:latest
```

## Usage

### 1. Generate PDF
* Params
* url: The URL to site url
* required: true
* format: The format of the outputted PDF
* required: false
* default: A4

```bash
curl -X POST \
http://localhost:8887/pdf \
-H 'Content-Type: application/json' \
-d '{
"url": "https://www.baidu.com",
"options": {
"format": "A4",
"landscape": true
}
}'
```

### 2. Generate Screenshot (Image)
* Params
* url: The URL to site url
* required: true
* fullPage: When true, takes a screenshot of the full scrollable page
* required: false
* default: true
* encoding: The encoding of the image, can be either base64 or undefined
* required: false
* default: none
* available: base64 | undefined
* width: The width of the image
* required: false
* height: The height of the image
* required: false

```bash
curl -X POST \
http://localhost:8080/image \
-H 'Content-Type: application/json' \
-d '{
"url": "https://www.baidu.com"
}'
```

## License
* MIT
10 changes: 7 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
services:
app:
build: .
puppeteer:
# build: .
image: whatwewant/puppeteer:latest
ports:
- 18080:8080
environment:
BROWSER_WS_ENDPOINT: wss://chrome.browserless.io
BROWSER_WS_ENDPOINT: ws://browserless:3000

browserless:
image: whatwewant/browserless-chrome:v1-0
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"prod": "NODE_ENV=production node lib/index.js"
},
"dependencies": {
"@koex/body": "^1.0.2",
"@koex/core": "^1.0.2",
"@znode/dotenv": "^0.0.10",
"@znode/fs": "^1.0.2",
Expand All @@ -23,4 +24,4 @@
"tsc-watch": "^6.0.4",
"typescript": "^5.3.3"
}
}
}
35 changes: 32 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import App, { Context } from '@koex/core';

import body from '@koex/body';

import { pdf } from './pdf';
import { image } from './image';
Expand All @@ -25,14 +25,43 @@ async function main() {
message: err.message || 'Internal Server Error',
};
}
})
});

app.use(body());

app.get('/pdf', pdf);
app.post('/pdf', pdf);

app.get('/image', image);
app.post('/image', image);

app.get('/', (ctx) => {
ctx.body = 'Hello, World!';
ctx.body = {
title: `Puppeteer as a Service`,
features: [
{
name: 'pdf',
description: 'Generate PDF from URL',
usage: '/pdf?url=<The Site URL>[&format=a4]',
examples: [
'/pdf?url=https://www.baidu.com',
'/pdf?url=https://www.baidu.com&format=a4',
'/pdf?url=https://www.baidu.com&format=letter',
],
},
{
name: 'image',
description: 'Generate Image from URL',
usage: '/image?url=<The Site URL>[&encoding=base64][&width=1024][&height=768][&fullPage=true]',
examples: [
'/image?url=https://www.baidu.com',
'/image?url=https://www.baidu.com&encoding=base64',
'/image?url=https://www.baidu.com&width=1024&height=768',
'/image?url=https://www.baidu.com&fullPage=false',
],
},
],
};
});

app.listen(port, '0.0.0.0', () => {
Expand Down
Loading

0 comments on commit bebc2b6

Please sign in to comment.