From 92a67123e72702d2ce4207f7a7a2b0fa8afdcdaa Mon Sep 17 00:00:00 2001 From: Laura Date: Mon, 11 Aug 2025 01:59:26 +0200 Subject: [PATCH] open in browser --- .github/workflows/release.yml | 2 +- env.go | 4 +++- main.go | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d23d790..ea00cae 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -37,7 +37,7 @@ jobs: --original-filename whiskr.exe \ --icon static/favicon.ico \ --copyright "(c) 2025 coalaura" \ - --file-description "AI story writing tool" \ + --file-description "Minimal LLM chat UI using OpenRouter giving you control over your context." \ --file-version "${{ github.ref_name }}" \ --arch "${{ matrix.goarch }}" diff --git a/env.go b/env.go index a2898eb..90a8b4b 100644 --- a/env.go +++ b/env.go @@ -9,6 +9,7 @@ import ( var ( Debug bool + NoOpen bool OpenRouterToken string ) @@ -16,12 +17,13 @@ func init() { log.MustPanic(godotenv.Load()) Debug = os.Getenv("DEBUG") == "true" + NoOpen = os.Getenv("NO_OPEN") == "true" if OpenRouterToken = os.Getenv("OPENROUTER_TOKEN"); OpenRouterToken == "" { log.Panic(errors.New("missing openrouter token")) } if Debug { - log.Debug("Debug mode enabled") + log.Warning("Debug mode enabled") } } diff --git a/main.go b/main.go index 34b18cf..feaf343 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,13 @@ package main import ( + "errors" "net/http" + "os/exec" "path/filepath" + "runtime" "strings" + "time" "github.com/coalaura/logger" adapter "github.com/coalaura/logger/http" @@ -18,9 +22,12 @@ var log = logger.New().DetectTerminal().WithOptions(logger.Options{ }) func main() { + log.Info("Loading models...") + models, err := LoadModels() log.MustPanic(err) + log.Info("Preparing router...") r := chi.NewRouter() r.Use(middleware.Recoverer) @@ -38,7 +45,18 @@ func main() { r.Post("/-/chat", HandleChat) - log.Debug("Listening at http://localhost:3443/") + if !NoOpen { + time.AfterFunc(500*time.Millisecond, func() { + log.Info("Opening browser...") + + err := open("http://localhost:3443/") + if err != nil { + log.WarningE(err) + } + }) + } + + log.Info("Listening at http://localhost:3443/") http.ListenAndServe(":3443", r) } @@ -54,3 +72,16 @@ func cache(next http.Handler) http.Handler { next.ServeHTTP(w, r) }) } + +func open(url string) error { + switch runtime.GOOS { + case "linux": + return exec.Command("xdg-open", url).Start() + case "windows": + return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() + case "darwin": + return exec.Command("open", url).Start() + } + + return errors.New("unsupported platform") +}