From 06ac259e0ae9a7868ee515881bb0765dbb10c1f3 Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Sun, 1 May 2022 11:55:20 +0930 Subject: [PATCH] Open browser on startup automatically, with configuration option to disable. --- .vscode/settings.json | 1 + CHANGELOG.md | 3 +++ config/config.go | 24 ++++++++++++++++++++---- config/config_test.go | 14 +++++++++----- dau.go | 7 +++++++ go.mod | 1 + go.sum | 2 ++ web/data/config.html | 13 ++++++++++++- web/server.go | 2 +- web/server_test.go | 2 +- 10 files changed, 57 insertions(+), 12 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 2b4fbce..d1afcdf 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "cSpell.words": [ "daulog", + "Debugf", "inconsolata" ] } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index ebd15d2..9d73a54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +- Automatically open your web browser to the `dau` web interface + (can be disabled in configuration) + ## [v0.12.1] - 2020-05-01 - Show if a new version is available in the web interface diff --git a/config/config.go b/config/config.go index 93f72f3..c7c2dc2 100644 --- a/config/config.go +++ b/config/config.go @@ -38,8 +38,16 @@ type ConfigV2 struct { Watchers []Watcher } +type ConfigV3 struct { + WatchInterval int + Version int + Port int + OpenBrowserOnStart bool + Watchers []Watcher +} + type ConfigService struct { - Config *ConfigV2 + Config *ConfigV3 Changed chan bool ConfigFilename string } @@ -66,11 +74,12 @@ func (c *ConfigService) LoadOrInit() error { } } -func DefaultConfig() *ConfigV2 { - c := ConfigV2{} - c.Version = 2 +func DefaultConfig() *ConfigV3 { + c := ConfigV3{} + c.Version = 3 c.WatchInterval = 10 c.Port = 9090 + c.OpenBrowserOnStart = true w := Watcher{ WebHookURL: "https://webhook.url.here", Path: "/your/screenshot/dir/here", @@ -122,6 +131,13 @@ func (c *ConfigService) Load() error { c.Config.Watchers = []Watcher{onlyWatcher} } + if c.Config.Version == 2 { + // need to migrate this + daulog.Info("Migrating config to V3") + c.Config.Version = 3 + c.Config.OpenBrowserOnStart = true + } + return nil } diff --git a/config/config_test.go b/config/config_test.go index e98a37a..43994e1 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -22,8 +22,8 @@ func TestNoConfig(t *testing.T) { t.Errorf("unexpected failure from load: %s", err) } - if c.Config.Version != 2 { - t.Error("not version 2 starting config") + if c.Config.Version != 3 { + t.Error("not version 3 starting config") } if fileSize(c.ConfigFilename) < 40 { @@ -45,7 +45,7 @@ func TestEmptyFileConfig(t *testing.T) { } -func TestMigrateFromV1toV2(t *testing.T) { +func TestMigrateFromV1toV3(t *testing.T) { c := ConfigService{} c.ConfigFilename = v1Config() @@ -53,8 +53,12 @@ func TestMigrateFromV1toV2(t *testing.T) { if err != nil { t.Error("unexpected error from LoadOrInit()") } - if c.Config.Version != 2 { - t.Errorf("Version %d not 2", c.Config.Version) + if c.Config.Version != 3 { + t.Errorf("Version %d not 3", c.Config.Version) + } + + if c.Config.OpenBrowserOnStart != true { + t.Errorf("Open browser on start not true") } if len(c.Config.Watchers) != 1 { diff --git a/dau.go b/dau.go index 50141f7..04ee2c9 100644 --- a/dau.go +++ b/dau.go @@ -21,6 +21,8 @@ import ( daulog "github.com/tardisx/discord-auto-upload/log" "github.com/tardisx/discord-auto-upload/upload" + "github.com/skratchdot/open-golang/open" + // "github.com/tardisx/discord-auto-upload/upload" "github.com/tardisx/discord-auto-upload/version" "github.com/tardisx/discord-auto-upload/web" @@ -51,6 +53,11 @@ func main() { web := web.WebService{Config: config, Uploader: up} web.StartWebServer() + if config.Config.OpenBrowserOnStart { + address := fmt.Sprintf("http://localhost:%d", config.Config.Port) + open.Start(address) + } + go func() { version.GetOnlineVersion() if version.UpdateAvailable() { diff --git a/go.mod b/go.mod index f285ca3..c7513ff 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/gorilla/mux v1.8.0 // indirect github.com/mitchellh/go-homedir v1.1.0 + github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 golang.org/x/image v0.0.0-20210504121937-7319ad40d33e golang.org/x/mod v0.4.2 ) diff --git a/go.sum b/go.sum index d2e5d2f..002ef7c 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA= +github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/image v0.0.0-20210504121937-7319ad40d33e h1:PzJMNfFQx+QO9hrC1GwZ4BoPGeNGhfeQEgcQFArEjPk= diff --git a/web/data/config.html b/web/data/config.html index 64590eb..64cf610 100644 --- a/web/data/config.html +++ b/web/data/config.html @@ -21,7 +21,7 @@

The Watch Interval is how often new files will be discovered by your - watchers (configured below).

+ watchers (watchers are configured below).

@@ -33,6 +33,17 @@
+
+
+ Open browser on startup +
+
+ + + +
+
+
Watch interval diff --git a/web/server.go b/web/server.go index 1c2af09..2e2b25e 100644 --- a/web/server.go +++ b/web/server.go @@ -133,7 +133,7 @@ func (ws *WebService) getLogs(w http.ResponseWriter, r *http.Request) { func (ws *WebService) handleConfig(w http.ResponseWriter, r *http.Request) { if r.Method == "POST" { - newConfig := config.ConfigV2{} + newConfig := config.ConfigV3{} defer r.Body.Close() b, err := ioutil.ReadAll(r.Body) diff --git a/web/server_test.go b/web/server_test.go index eb160b8..d79b3f7 100644 --- a/web/server_test.go +++ b/web/server_test.go @@ -78,7 +78,7 @@ func TestGetConfig(t *testing.T) { t.Errorf("expected error to be nil got %v", err) } - exp := `{"WatchInterval":10,"Version":2,"Port":9090,"Watchers":[{"WebHookURL":"https://webhook.url.here","Path":"/your/screenshot/dir/here","Username":"","NoWatermark":false,"HoldUploads":false,"Exclude":[]}]}` + exp := `{"WatchInterval":10,"Version":3,"Port":9090,"OpenBrowserOnStart":true,"Watchers":[{"WebHookURL":"https://webhook.url.here","Path":"/your/screenshot/dir/here","Username":"","NoWatermark":false,"HoldUploads":false,"Exclude":[]}]}` if string(b) != exp { t.Errorf("Got unexpected response\n%v\n%v", string(b), exp) }