Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions cmd/routatic-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ Press Ctrl+C to stop the server.`,
defer cancel()

var guiSrv *gui.Server
var guiDone <-chan struct{}

// Start proxy in background.
go func() {
Expand Down Expand Up @@ -389,23 +390,31 @@ Press Ctrl+C to stop the server.`,
return fmt.Errorf("start gui server: %w", err)
}

// Open GUI (macOS: native webview, Linux/Windows: print URL)
if err := openGUI(guiURL); err != nil {
slog.Warn("GUI error", "error", err)
// Open GUI (macOS: system tray + browser, Linux/Windows: print URL).
// guiDone is closed when the user quits from the tray (CGO
// path); it is nil on non-CGO platforms where only SIGINT
// stops the proxy.
var guiErr error
guiDone, guiErr = openGUI(guiURL)
if guiErr != nil {
slog.Warn("GUI error", "error", guiErr)
fmt.Printf("\nDashboard: %s\n", guiURL)
fmt.Println("\nPress Ctrl+C to stop.")
}
} else {
fmt.Println("\nRunning in headless mode (no dashboard). Press Ctrl+C to stop.")
}

// Wait for signal.
// Wait for signal or GUI window close.
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
select {
case <-sigCh:
fmt.Println("\nShutting down...")
case <-ctx.Done():
case <-guiDone:
fmt.Println("\nGUI window closed, shutting down...")
cancel()
}

// Graceful shutdown.
Expand Down
62 changes: 31 additions & 31 deletions cmd/routatic-proxy/start_gui_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,39 @@ package main

import (
"fmt"
"os/exec"

"github.com/getlantern/systray"
"github.com/webview/webview_go"
"github.com/routatic/proxy/internal/tray"
)

func openGUI(guiURL string) error {
// openGUI starts the system tray icon and opens the dashboard in the default
// browser. It returns a channel that is closed when the user clicks "Quit" in
// the tray menu, signaling that the proxy should shut down.
//
// systray.Run must be called from the main OS thread on macOS (AppKit
// requirement). The goroutine that calls this function must be the main
// goroutine locked to the OS thread via runtime.LockOSThread, OR the library
// handles it internally (getlantern/systray does).
func openGUI(guiURL string) (<-chan struct{}, error) {
fmt.Printf("\nDashboard: %s\n", guiURL)
fmt.Println("Opening native window...")

// Set up system tray first (initializes on main thread), then start webview
// run loop inside onReady so both coexist: tray menu + native webview window.
systray.Run(func() {
systray.SetTitle("routatic-proxy")
systray.SetTooltip("routatic-proxy is running")
mQuit := systray.AddMenuItem("Quit", "Stop the proxy")

wv := webview.New(false)
wv.SetTitle("routatic-proxy")
wv.SetSize(1200, 800, webview.HintNone)
wv.Navigate(guiURL)

go func() {
<-mQuit.ClickedCh
wv.Dispatch(func() {
wv.Terminate()
})
systray.Quit()
}()

wv.Run()
wv.Destroy()
systray.Quit()
}, nil)

return nil

// Open dashboard in default browser.
_ = exec.Command("open", guiURL).Start()

done := make(chan struct{})

go func() {
tray.Run(tray.Callbacks{
InitiallyRunning: true,
InitiallyAutostart: false,
OnOpen: func() {
_ = exec.Command("open", guiURL).Start()
},
OnQuit: func() {
close(done)
},
})
}()

return done, nil
}
10 changes: 5 additions & 5 deletions cmd/routatic-proxy/start_gui_darwin_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

package main

import (
"fmt"
)
import "fmt"

func openGUI(guiURL string) error {
// openGUI prints the dashboard URL. It returns a nil channel because there is
// no native window to wait on — only SIGINT stops the proxy.
func openGUI(guiURL string) (<-chan struct{}, error) {
fmt.Printf("Dashboard: %s\n", guiURL)
fmt.Println("\nPress Ctrl+C to stop.")
return nil
return nil, nil
}
10 changes: 5 additions & 5 deletions cmd/routatic-proxy/start_gui_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

package main

import (
"fmt"
)
import "fmt"

func openGUI(guiURL string) error {
// openGUI prints the dashboard URL. It returns a nil channel because there is
// no native window to wait on — only SIGINT stops the proxy.
func openGUI(guiURL string) (<-chan struct{}, error) {
fmt.Printf("Dashboard: %s\n", guiURL)
fmt.Println("\nPress Ctrl+C to stop.")
return nil
return nil, nil
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/getlantern/systray v1.2.2
github.com/pkoukk/tiktoken-go v0.1.8
github.com/spf13/cobra v1.8.1
github.com/webview/webview_go v0.0.0-20240831120633-6173450d4dd6
golang.org/x/mod v0.38.0
golang.org/x/sys v0.46.0
modernc.org/sqlite v1.53.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/webview/webview_go v0.0.0-20240831120633-6173450d4dd6 h1:VQpB2SpK88C6B5lPHTuSZKb2Qee1QWwiFlC5CKY4AW0=
github.com/webview/webview_go v0.0.0-20240831120633-6173450d4dd6/go.mod h1:yE65LFCeWf4kyWD5re+h4XNvOHJEXOCOuJZ4v8l5sgk=
golang.org/x/mod v0.38.0 h1:MECBjubtXD7yj4HrhIUcywNaGeNVUdfVnxmPajOk4yk=
golang.org/x/mod v0.38.0/go.mod h1:V6Xz0pq8TQ3dGqVQ1FVHuelZpAL0uNhSkk9ogYP3c40=
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
Expand Down
Loading