Skip to content

Commit

Permalink
🚀 feat: update browser service in Docker Compose and refactor browser…
Browse files Browse the repository at this point in the history
… fetcher to use go-rod launcher
  • Loading branch information
vaayne committed Dec 28, 2024
1 parent 6826fee commit 66ece98
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 42 deletions.
13 changes: 3 additions & 10 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,11 @@ services:
- .env:/app/.env
depends_on:
- postgres
browserless:
image: gcr.docker.vaayne.com/zenika-hub/alpine-chrome:123
browser:
image: ghcr.docker.vaayne.com/go-rod/rod:latest
restart: unless-stopped
ports:
- "9222:9222"
command:
- --no-sandbox
- --disable-gpu
- --disable-dev-shm-usage
- --remote-debugging-address=0.0.0.0
- --remote-debugging-port=9222
# - --hide-scrollbars
- "7317:7317"
postgres:
image: pgvector/pgvector:pg16
environment:
Expand Down
38 changes: 6 additions & 32 deletions internal/pkg/webreader/fetcher/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fetcher

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
Expand All @@ -12,6 +11,7 @@ import (
"vibrain/internal/pkg/webreader"

"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
)

Expand Down Expand Up @@ -50,40 +50,20 @@ func NewBrowserFetcher(opts ...BroswerOption) (*BrowserFetcher, error) {
}

func newBrowserFetcher(cfg BrowserConfig) (*BrowserFetcher, error) {
// Get WebSocket debugger URL
wsURL, err := getWebSocketDebuggerURL(cfg.ControlURL)
// https://go-rod.github.io/#/custom-launch?id=remotely-manage-the-launcher
l, err := launcher.NewManaged(cfg.ControlURL)
if err != nil {
return nil, fmt.Errorf("get debugger URL: %w", err)
return nil, fmt.Errorf("create new launcher: %w", err)
}

// Connect to browser
browser := rod.New().ControlURL(wsURL).MustConnect()
l.Headless(true).Set("disable-gpu").Set("no-sandbox").Set("disable-dev-shm-usage")
browser := rod.New().Client(l.MustClient()).MustConnect()

return &BrowserFetcher{
config: cfg,
browser: browser,
}, nil
}

// getWebSocketDebuggerURL retrieves the WebSocket debugger URL from Chrome
func getWebSocketDebuggerURL(controlURL string) (string, error) {
resp, err := http.Get(controlURL + "/json/version")
if err != nil {
return "", fmt.Errorf("get version info: %w", err)
}
defer resp.Body.Close()

var info versionInfo
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
return "", fmt.Errorf("decode version info: %w", err)
}

if info.WebSocketDebuggerUrl == "" {
return "", fmt.Errorf("no WebSocket debugger URL found")
}

return info.WebSocketDebuggerUrl, nil
}

// Fetch implements the Fetcher interface
func (f *BrowserFetcher) Fetch(ctx context.Context, url string) (*webreader.Content, error) {
Expand Down Expand Up @@ -161,9 +141,3 @@ func WithBroswerOptionScrollToBottom(scroll bool) BroswerOption {
c.ScrollToBottom = scroll
}
}

// versionInfo represents Chrome DevTools Protocol version info
type versionInfo struct {
Browser string `json:"Browser"`
WebSocketDebuggerUrl string `json:"webSocketDebuggerUrl"`
}

0 comments on commit 66ece98

Please sign in to comment.