Skip to content

Commit

Permalink
Enable using Chrome with WebSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
Ordspilleren committed Jun 2, 2021
1 parent f47981e commit 9a63105
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ RUN CGO_ENABLED=0 GOOS=linux go build -o ./out/app .
# Runtime image
FROM alpine
COPY --from=build /app/out/app /usr/local/bin/changemonitor
RUN apk add --no-cache chromium
RUN apk add --no-cache tzdata
ENV CONFIG_FILE=/config/config.json
ENV STORAGE_DIRECTORY=/data
ENV CHROME_PATH=/usr/bin/chromium-browser
ENV CHROME_WS=ws://127.0.0.1:9222
ENV ENABLE_WEBUI=false
ENTRYPOINT ["/usr/local/bin/changemonitor"]
17 changes: 17 additions & 0 deletions Dockerfile.chrome
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Build image
FROM golang:alpine AS build
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . ./
RUN CGO_ENABLED=0 GOOS=linux go build -o ./out/app .

# Runtime image
FROM alpine
COPY --from=build /app/out/app /usr/local/bin/changemonitor
RUN apk add --no-cache tzdata chromium
ENV CONFIG_FILE=/config/config.json
ENV STORAGE_DIRECTORY=/data
ENV CHROME_PATH=/usr/bin/chromium-browser
ENV ENABLE_WEBUI=false
ENTRYPOINT ["/usr/local/bin/changemonitor"]
7 changes: 6 additions & 1 deletion html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"html/template"
"io"
"net/http"
"strings"

"github.com/Ordspilleren/ChangeMonitor/monitor"
)
Expand All @@ -20,6 +21,10 @@ var (
monitorNew = parse("monitornew.html")
)

var funcs = template.FuncMap{
"StringsJoin": strings.Join,
}

type MonitorListParams struct {
MonitorService *monitor.MonitorService
}
Expand All @@ -39,7 +44,7 @@ func MonitorNew(w io.Writer, p MonitorNewParams) error {

func parse(file string) *template.Template {
return template.Must(
template.New("layout.html").ParseFS(htmlTemplates, "layout.html", file))
template.New("layout.html").Funcs(funcs).ParseFS(htmlTemplates, "layout.html", file))
}

func GetAssetFS() http.FileSystem {
Expand Down
6 changes: 3 additions & 3 deletions html/monitornew.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@
<div class="field">
<label class="label">Interval</label>
<div class="control">
<input name="interval" class="input" type="number"{{if .Monitor}} value="{{.Monitor.Interval}}"{{end}}>
<input name="interval" class="input" type="number"{{if .Monitor}} value="{{.Monitor.Interval.Nanoseconds}}"{{end}}>
</div>
</div>

<div class="field">
<label class="label">CSS Selectors</label>
<div class="control">
<textarea name="cssselectors" class="textarea"></textarea>
<textarea name="cssselectors" class="textarea">{{if .Monitor.Selectors.CSS}}{{StringsJoin .Monitor.Selectors.CSS "\n"}}{{end}}</textarea>
</div>
</div>

<div class="field">
<label class="label">JSON Selectors</label>
<div class="control">
<textarea name="jsonselectors" class="textarea"></textarea>
<textarea name="jsonselectors" class="textarea">{{if .Monitor.Selectors.JSON}}{{StringsJoin .Monitor.Selectors.JSON "\n"}}{{end}}</textarea>
</div>
</div>

Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var wg = &sync.WaitGroup{}
var ConfigFile string
var StorageDirectory string
var ChromePath string
var ChromeWs string
var EnableWebUI bool

type Config struct {
Expand All @@ -43,6 +44,7 @@ func init() {
ConfigFile = getEnv("CONFIG_FILE", "config.json")
StorageDirectory = getEnv("STORAGE_DIRECTORY", "data")
ChromePath = getEnv("CHROME_PATH", "/usr/bin/chromium")
ChromeWs = getEnv("CHROME_WS", "")
EnableWebUI, _ = strconv.ParseBool(getEnv("ENABLE_WEBUI", "false"))
log.Printf("Config File: %s", ConfigFile)
log.Printf("Storage Directory: %s", StorageDirectory)
Expand All @@ -62,7 +64,11 @@ func init() {
storageManager := storage.InitStorage(StorageDirectory)

monitorService = monitor.NewMonitorService(wg, config.Monitors, storageManager, notifierService)
monitorService.NewMonitorClients(ChromePath)
if ChromeWs != "" {
monitorService.NewMonitorClients(ChromeWs, true)
} else {
monitorService.NewMonitorClients(ChromePath, false)
}
monitorService.InitMonitors()
}

Expand Down
28 changes: 25 additions & 3 deletions monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func NewMonitorService(wg *sync.WaitGroup, monitors Monitors, storage Storage, n
return &monitorService
}

func (ms *MonitorService) NewMonitorClients(chromePath string) error {
func (ms *MonitorService) NewMonitorClients(chromePath string, externalClient bool) error {
var usingChrome bool

for _, monitor := range ms.Monitors {
Expand All @@ -97,7 +97,7 @@ func (ms *MonitorService) NewMonitorClients(chromePath string) error {
}

if usingChrome {
chromeClient, err := newChromeClient(chromePath)
chromeClient, err := newChromeClient(chromePath, externalClient)
if err != nil {
return fmt.Errorf("failed to create new chrome client: %v", err)
}
Expand Down Expand Up @@ -375,7 +375,11 @@ func (h ChromeClient) getHTTPBody(url string, headers http.Header) (io.ReadClose
return readCloser, nil
}

func newChromeClient(chromePath string) (*ChromeClient, error) {
func newChromeClient(chromePath string, externalClient bool) (*ChromeClient, error) {
if externalClient {
return newExternalChromeClient(chromePath)
}

launcher := launcher.New().Bin(chromePath)
u, err := launcher.Launch()
if err != nil {
Expand All @@ -398,3 +402,21 @@ func newChromeClient(chromePath string) (*ChromeClient, error) {

return chromeClient, nil
}

func newExternalChromeClient(chromeWs string) (*ChromeClient, error) {
url, err := launcher.ResolveURL(chromeWs)
if err != nil {
return nil, fmt.Errorf("failed to connect to browser: %v", err)
}
browser := rod.New().ControlURL(url)
err = browser.Connect()
if err != nil {
return nil, fmt.Errorf("failed to connect to browser: %v", err)
}

chromeClient := &ChromeClient{
Browser: browser,
}

return chromeClient, nil
}

0 comments on commit 9a63105

Please sign in to comment.