Skip to content

Commit

Permalink
Use Browserless for capturing screenshots
Browse files Browse the repository at this point in the history
  • Loading branch information
misenhower committed Nov 18, 2024
1 parent 2743ca7 commit 35316e0
Show file tree
Hide file tree
Showing 9 changed files with 496 additions and 180 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ USER_AGENT=
# Sentry
SENTRY_DSN=

# Browserless (for screenshots)
BROWSERLESS_ENDPOINT=ws://localhost:3000
SCREENSHOT_HOST=host.docker.internal
BROWSERLESS_CONCURRENT=2

# S3 parameters
AWS_S3_ENDPOINT=
AWS_S3_BUCKET=
Expand Down
14 changes: 6 additions & 8 deletions app/screenshots/ScreenshotHelper.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { URL } from 'url';
import puppeteer from 'puppeteer';
import puppeteer from 'puppeteer-core';
import HttpServer from './HttpServer.mjs';

const defaultViewport = {
Expand Down Expand Up @@ -36,12 +36,9 @@ export default class ScreenshotHelper
this.#httpServer = new HttpServer;
await this.#httpServer.open();

// Launch a new Chrome instance
this.#browser = await puppeteer.launch({
args: [
'--no-sandbox', // Allow running as root inside the Docker container
],
// headless: false, // For testing
// Connect to Browserless
this.#browser = await puppeteer.connect({
browserWSEndpoint: process.env.BROWSERLESS_ENDPOINT,
});

// Create a new page and set the viewport
Expand All @@ -66,7 +63,8 @@ export default class ScreenshotHelper
await this.applyViewport(options.viewport);

// Navigate to the URL
let url = new URL(`http://localhost:${this.#httpServer.port}/screenshots/`);
let host = process.env.SCREENSHOT_HOST || 'localhost';
let url = new URL(`http://${host}:${this.#httpServer.port}/screenshots/`);
url.hash = path;

let params = {
Expand Down
2 changes: 1 addition & 1 deletion app/social/clients/TwitterClient.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class TwitterClient extends Client
// Upload images
let mediaIds = await Promise.all(
status.media.map(async m => {
let id = await this.api().v1.uploadMedia(m.file, { mimeType: m.type });
let id = await this.api().v1.uploadMedia(Buffer.from(m.file), { mimeType: m.type });

if (m.altText) {
await this.api().v1.createMediaMetadata(id, { alt_text: { text: m.altText } });
Expand Down
21 changes: 21 additions & 0 deletions docker-compose.override.yml.dev.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Example Docker Compose override file for local development

services:
app:
platform: linux/amd64 # Needed for Apple Silicon
build:
dockerfile: docker/app/Dockerfile
init: true
restart: unless-stopped
environment:
BROWSERLESS_ENDPOINT: ws://browserless:3000
SCREENSHOT_HOST: app
depends_on:
- browserless
volumes:
- .:/app

browserless:
platform: linux/arm64 # Needed for Apple Silicon
ports:
- 3000:3000
22 changes: 0 additions & 22 deletions docker-compose.override.yml.example

This file was deleted.

23 changes: 23 additions & 0 deletions docker-compose.override.yml.prod.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Example Docker Compose override file for production

services:
app:
image: ghcr.io/misenhower/splatoon2.ink:main
init: true
restart: unless-stopped
environment:
BROWSERLESS_ENDPOINT: ws://browserless:3000
SCREENSHOT_HOST: app
depends_on:
- browserless
labels: [ "com.centurylinklabs.watchtower.scope=splatoon3ink" ]

browserless:
labels: [ "com.centurylinklabs.watchtower.scope=splatoon3ink" ]

watchtower:
image: containrrr/watchtower
volumes:
- /var/run/docker.sock:/var/run/docker.sock
command: --interval 30 --scope splatoon3ink
labels: [ "com.centurylinklabs.watchtower.scope=splatoon3ink" ]
21 changes: 6 additions & 15 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
version: "3.8"
# See docker-compose.override.yml.* example files for dev/prod environments

services:
app:
build: docker/app
init: true
browserless:
image: ghcr.io/browserless/chromium
restart: unless-stopped
working_dir: /app
volumes:
- ./:/app
command: npm run cron

nginx:
image: nginx
restart: unless-stopped
volumes:
- ./docker/nginx/conf.d:/etc/nginx/conf.d:ro
- ./dist:/usr/share/nginx/html:ro
environment:
CONCURRENT: ${BROWSERLESS_CONCURRENT:-1}
QUEUED: ${BROWSERLESS_QUEUED:-100}
Loading

0 comments on commit 35316e0

Please sign in to comment.