Skip to content

Commit

Permalink
docs: improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonraimondi committed Mar 21, 2024
1 parent 5a6f0b5 commit 43ac6c9
Show file tree
Hide file tree
Showing 29 changed files with 25,521 additions and 249 deletions.
41 changes: 33 additions & 8 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,21 +1,50 @@
# Logging level (debug, info, warn, error)
LOG_LEVEL=debug

# Node environment (development, production)
NODE_ENV=development
PORT=3089

ALLOW_LIST=jasonraimondi.com,github.com
# Port number for the application to listen on (optional)
#PORT=3089

# Comma-separated list of allowed domains for screenshots (optional)
#ALLOW_LIST=jasonraimondi.com,github.com

# Cache-Control header value for the responses (optional)
#CACHE_CONTROL="public, max-age=86400, immutable"

# Browser timeout in milliseconds for rendering (optional)
#BROWSER_TIMEOUT=10000

# Event to wait for before considering the page loaded (optional)
#BROWSER_WAIT_UNTIL=domcontentloaded

# Idle timeout for database connection pool in milliseconds (optional)
#POOL_IDLE_TIMEOUT_MS=15000

# Maximum number of connections in the database pool (optional)
#POOL_MAX=10

# Maximum number of waiting clients for the database pool (optional)
#POOL_MAX_WAITING_CLIENTS=50

# Minimum number of connections in the database pool (optional)
#POOL_MIN=2

METRICS=true
# Enable or disable metrics collection (optional)
#METRICS=false

# Encryption key for sensitive data (optional)
#CRYPTO_KEY=

# Storage provider (stub, s3, couchdb, filesystem)
STORAGE_PROVIDER=stub

# Configuration for filesystem storage provider (optional)
#STORAGE_PROVIDER=filesystem
#IMAGE_STORAGE_PATH=

# Configuration for S3 storage provider (optional)
#STORAGE_PROVIDER=s3
#AWS_BUCKET=
#AWS_ACCESS_KEY_ID=
Expand All @@ -24,15 +53,11 @@ STORAGE_PROVIDER=stub
#AWS_ENDPOINT_URL_S3=
#AWS_FORCE_PATH_STYLE=false

# Configuration for CouchDB storage provider (optional)
#STORAGE_PROVIDER=couchdb
#COUCH_DB_HOST=
#COUCH_DB_PASS=
#COUCH_DB_PORT=
#COUCH_DB_PROTOCOL=
#COUCH_DB_USER=
#COUCHDB_DATABASE=images

#STORAGE_PROVIDER=filesystem
#IMAGE_STORAGE_PATH=

#CRYPTO_KEY=
30 changes: 30 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Deploy VitePress site to GitHub Pages

on:
push:
branches:
- main
- next
paths:
- '.github/workflows/docs.yml'
- 'docs/**'

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v3
with:
version: 8
- uses: actions/setup-node@v4
with:
node-version-file: 20
cache: pnpm
cache-dependency-path: pnpm-lock.yaml
- run: pnpm install --frozen-lockfile
- run: pnpm docs:build
- uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: docs/.vitepress/dist
7 changes: 3 additions & 4 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build and Publish Docker Image
name: Build and publish Docker images

on:
push:
Expand All @@ -12,7 +12,7 @@ jobs:
build-and-publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: docker/setup-qemu-action@v3
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v1
Expand All @@ -37,8 +37,7 @@ jobs:
else
echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
fi
- name: Build and Push Docker Image
uses: docker/build-push-action@v5
- uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile
Expand Down
4 changes: 2 additions & 2 deletions .mise.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[tools]
node = "20"
pnpm = "8"
node = "20"
pnpm = "8"
182 changes: 15 additions & 167 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,173 +1,21 @@
<h1 align="center">
<img height="100" src="https://res.cloudinary.com/jmondi/image/upload/v1536798191/jasonraimondi.com/url-to-img/url-to-png-logo.png" alt="URL to PNG logo" />
<br /> URL to PNG
<img style="height:200px;padding:0;" height="100" src="https://res.cloudinary.com/jmondi/image/upload/v1536798191/jasonraimondi.com/url-to-img/url-to-png-logo.png" alt="URL-to-PNG logo" />
URL-to-PNG
</h1>

A URL to PNG generator over HTTP with a fairly simple API accessed via query params passed to the servers single endpoint.
![GitHub License](https://img.shields.io/github/license/jasonraimondi/url-to-png)
[![GitHub Workflow Status]( https://img.shields.io/github/actions/workflow/status/jasonraimondi/url-to-png/publish.yml?branch=main&style=flat-square)](https://github.com/jasonraimondi/url-to-png)
![Docker Pulls](https://img.shields.io/docker/pulls/jasonraimondi/url-to-png)

## Getting Started
URL-to-PNG is a server that generates PNG images from URLs over HTTP. It provides a simple API that can be accessed via query parameters passed to the server's single endpoint.

### Docker
### Features

Run the following command:

```
docker run --rm -p 3089:3089 ghcr.io/jasonraimondi/url-to-png
```

On the hub: [Link to DockerHub](https://hub.docker.com/r/jasonraimondi/url-to-png/)

### Local Serve

Serve the project

```
git clone [email protected]:jasonraimondi/url-to-png.git
cd url-to-png
pnpm install
# you may need to run this command if you are having issues with playwright
pnpm exec playwright install
pnpm dev
```

## Query Params

- `url: string` - Valid URL to be captured - **Required**
- `width: number` - Width of output screenshot - **Optional** - Default: `250`
- `height: number` - Height of output screenshot - **Optional** - Default: `250`
- `viewPortWidth: number` - Width of render viewport - **Optional** - Default: `1080`
- `viewPortHeight: number` - Height of render viewport - **Optional** - Default: `1080`
- `forceReload: boolean` - Force cached image reload - **Optional** - Default: `false`
- `isMobile: boolean` - Adds mobile flag to user agent - **Optional** - Default: `false
- `isFullPage: boolean` - Render full page instead of viewport crop - **Optional** - Default: `false`
- `isDarkMode: boolean` - Prefer color scheme dark - **Optional** - Default: `false`
- `deviceScaleFactor: number` - Specify device scale factor (can be thought of as dpr) - **Optional** - Default: `1`

Try it out at `localhost:3089?url=https://www.jasonraimondi.com&isDarkMode=true` and you should get back an image capture of my website homepage.

All sorts of combos work:

```
http://localhost:3089?url=https://jasonraimondi.com
http://localhost:3089?url=https://jasonraimondi.com&forceReload=true
http://localhost:3089?url=https://jasonraimondi.com&isFullPage=true
http://localhost:3089?url=https://jasonraimondi.com&isMobile=true
http://localhost:3089?url=https://jasonraimondi.com&isDarkMode=true
http://localhost:3089?url=https://jasonraimondi.com&width=400&height=400
http://localhost:3089?url=https://jasonraimondi.com&viewPortHeight=400&viewPortWidth=400
http://localhost:3089?url=https://jasonraimondi.com&viewPortHeight=400&viewPortWidth=400
http://localhost:3089?url=https://jasonraimondi.com&isFullPage=true&isMobile=true&width=400&height=400&viewPortHeight=400&viewPortWidth=400
http://localhost:3089?url=https://jasonraimondi.com&isMobile=true&isFullPage=true&viewPortWidth=375&width=375&deviceScaleFactor=1
```

## System Requirements

The following is cut from the Playwright documentation, please view the [official system requirement documentation](https://playwright.dev/docs/1.3.0/intro/#system-requirements) for the most up to date requirements guide.

Playwright requires Node.js version 10.15 or above. The browser binaries for Chromium, Firefox and WebKit work across the 3 platforms (Windows, macOS, Linux):

* **Windows**: Works with Windows and Windows Subsystem for Linux (WSL).
* **macOS**: Requires 10.14 or above.
* **Linux**: Depending on your Linux distribution, you might need to install additional dependencies to run the browsers.
* **Firefox** requires Ubuntu 18.04+
* For **Ubuntu 18.04**, the additional dependencies are defined in the [playwright Docker image](https://github.com/microsoft/playwright/blob/master/utils/docker/Dockerfile.bionic), which is based on Ubuntu.

## Allow List

Allow domain specific requests by passing in an ALLOW_LIST. The list should be **comma separated**, **not include spaces**, and should be the **domain name only**.

If this value is either `undefined || ""`, url-to-png will allow any domain to be rendered.

```
ALLOW_LIST=jasonraimondi.com,github.com
```

## Playwright Options

Please see the Playwright API documentation [here](https://playwright.dev/docs/api/class-page#pagegotourl-options) for further knowledge.

```
BROWSER_TIMEOUT=
BROWSER_WAIT_UNTIL=
```

#### `BROWSER_TIMEOUT`

The maximum navigation time in milliseconds, pass 0 to disable timeout. The default value is 10000

#### `BROWSER_WAIT_UNTIL`

When playwright to considers the navigation succeeded. The default value for this option is `domcontentloaded`

Valid Options:

- `'load'` - consider operation to be finished when the `load` event is fired.
- `'domcontentloaded'` - consider operation to be finished when the `DOMContentLoaded` event is fired.
- `'networkidle'` - consider operation to be finished when there are no network connections for at least `500` ms.

## Image Storage / Cache

NOTE: If you are running in Docker, you should skip the .env and load the environment variables into your container.

You are going to need to copy the environment file to use any of the storage options. By default no images are cached.

```
cp .env.sample .env
```

#### AWS S3

To use Amazon S3 set `STORAGE_PROVIDER=s3`, ensure the following variables are loaded in your `.env`:

```
STORAGE_PROVIDER=s3
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=
AWS_ENDPOINT_URL_S3=
AWS_BUCKET=
```

#### CouchDB

To use CouchDB set `STORAGE_PROVIDER=couchdb`, ensure the following variables are loaded in your `.env`:

```
STORAGE_PROVIDER=couchdb
COUCH_DB_PROTOCOL=
COUCH_DB_HOST=
COUCH_DB_USER=
COUCH_DB_PASS=
```

#### Filesystem

To use your local filesystem, set `STORAGE_PROVIDER=filesystem`, ensure the following variables are loaded in your `.env`:

```
STORAGE_PROVIDER=filesystem
IMAGE_STORAGE_PATH=/tmp/foo
```

## Metrics

You can enable metrics by setting `METRICS=true` in your `.env` file. This will expose a `/metrics` endpoint for Prometheus to scrape.

```json
{
"poolMetrics": {
"spareResourceCapacity": 8,
"size": 2,
"available": 2,
"borrowed": 0,
"pending": 0,
"max": 10,
"min": 2
}
}
```

## Sources

- Upstream Repository: [https://git.jasonraimondi.com/jason/url-to-png](https://git.jasonraimondi.com/jason/url-to-png)
- Logo from [https://www.hipsterlogogenerator.com/](https://www.hipsterlogogenerator.com/)
- Generate PNG images from URLs
- Customizable image dimensions and viewport size
- Support for mobile user agent and dark mode rendering
- Caching of generated images
- Allow list for domain-specific requests
- Configurable Playwright options
- Integration with various storage providers (AWS S3, CouchDB, Filesystem)
- Prometheus metrics endpoint
Loading

0 comments on commit 43ac6c9

Please sign in to comment.