Open browser on startup automatically, with configuration option to disable.

This commit is contained in:
Justin Hawkins 2022-05-01 11:55:20 +09:30
parent 0c2fafdc7a
commit 06ac259e0a
10 changed files with 57 additions and 12 deletions

View File

@ -1,6 +1,7 @@
{ {
"cSpell.words": [ "cSpell.words": [
"daulog", "daulog",
"Debugf",
"inconsolata" "inconsolata"
] ]
} }

View File

@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file.
## [Unreleased] ## [Unreleased]
- Automatically open your web browser to the `dau` web interface
(can be disabled in configuration)
## [v0.12.1] - 2020-05-01 ## [v0.12.1] - 2020-05-01
- Show if a new version is available in the web interface - Show if a new version is available in the web interface

View File

@ -38,8 +38,16 @@ type ConfigV2 struct {
Watchers []Watcher Watchers []Watcher
} }
type ConfigV3 struct {
WatchInterval int
Version int
Port int
OpenBrowserOnStart bool
Watchers []Watcher
}
type ConfigService struct { type ConfigService struct {
Config *ConfigV2 Config *ConfigV3
Changed chan bool Changed chan bool
ConfigFilename string ConfigFilename string
} }
@ -66,11 +74,12 @@ func (c *ConfigService) LoadOrInit() error {
} }
} }
func DefaultConfig() *ConfigV2 { func DefaultConfig() *ConfigV3 {
c := ConfigV2{} c := ConfigV3{}
c.Version = 2 c.Version = 3
c.WatchInterval = 10 c.WatchInterval = 10
c.Port = 9090 c.Port = 9090
c.OpenBrowserOnStart = true
w := Watcher{ w := Watcher{
WebHookURL: "https://webhook.url.here", WebHookURL: "https://webhook.url.here",
Path: "/your/screenshot/dir/here", Path: "/your/screenshot/dir/here",
@ -122,6 +131,13 @@ func (c *ConfigService) Load() error {
c.Config.Watchers = []Watcher{onlyWatcher} 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 return nil
} }

View File

@ -22,8 +22,8 @@ func TestNoConfig(t *testing.T) {
t.Errorf("unexpected failure from load: %s", err) t.Errorf("unexpected failure from load: %s", err)
} }
if c.Config.Version != 2 { if c.Config.Version != 3 {
t.Error("not version 2 starting config") t.Error("not version 3 starting config")
} }
if fileSize(c.ConfigFilename) < 40 { 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 := ConfigService{}
c.ConfigFilename = v1Config() c.ConfigFilename = v1Config()
@ -53,8 +53,12 @@ func TestMigrateFromV1toV2(t *testing.T) {
if err != nil { if err != nil {
t.Error("unexpected error from LoadOrInit()") t.Error("unexpected error from LoadOrInit()")
} }
if c.Config.Version != 2 { if c.Config.Version != 3 {
t.Errorf("Version %d not 2", c.Config.Version) 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 { if len(c.Config.Watchers) != 1 {

7
dau.go
View File

@ -21,6 +21,8 @@ import (
daulog "github.com/tardisx/discord-auto-upload/log" daulog "github.com/tardisx/discord-auto-upload/log"
"github.com/tardisx/discord-auto-upload/upload" "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/upload"
"github.com/tardisx/discord-auto-upload/version" "github.com/tardisx/discord-auto-upload/version"
"github.com/tardisx/discord-auto-upload/web" "github.com/tardisx/discord-auto-upload/web"
@ -51,6 +53,11 @@ func main() {
web := web.WebService{Config: config, Uploader: up} web := web.WebService{Config: config, Uploader: up}
web.StartWebServer() web.StartWebServer()
if config.Config.OpenBrowserOnStart {
address := fmt.Sprintf("http://localhost:%d", config.Config.Port)
open.Start(address)
}
go func() { go func() {
version.GetOnlineVersion() version.GetOnlineVersion()
if version.UpdateAvailable() { if version.UpdateAvailable() {

1
go.mod
View File

@ -7,6 +7,7 @@ require (
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/gorilla/mux v1.8.0 // indirect github.com/gorilla/mux v1.8.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 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/image v0.0.0-20210504121937-7319ad40d33e
golang.org/x/mod v0.4.2 golang.org/x/mod v0.4.2
) )

2
go.sum
View File

@ -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/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 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= 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-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 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= golang.org/x/image v0.0.0-20210504121937-7319ad40d33e h1:PzJMNfFQx+QO9hrC1GwZ4BoPGeNGhfeQEgcQFArEjPk=

View File

@ -21,7 +21,7 @@
</p> </p>
<p>The Watch Interval is how often new files will be discovered by your <p>The Watch Interval is how often new files will be discovered by your
watchers (configured below).</p> watchers (watchers are configured below).</p>
<div class="form-row align-items-center"> <div class="form-row align-items-center">
<div class="col-sm-6 my-1"> <div class="col-sm-6 my-1">
@ -33,6 +33,17 @@
</div> </div>
</div> </div>
<div class="form-row align-items-center">
<div class="col-sm-6 my-1">
<span>Open browser on startup</span>
</div>
<div class="col-sm-6 my-1">
<label class="sr-only">Open browser</label>
<button type="button" @click="config.OpenBrowserOnStart = ! config.OpenBrowserOnStart" class="btn btn-success" x-text="config.OpenBrowserOnStart ? 'Enabled' : 'Disabled'"></button>
</div>
</div>
<div class="form-row align-items-center"> <div class="form-row align-items-center">
<div class="col-sm-6 my-1"> <div class="col-sm-6 my-1">
<span>Watch interval</span> <span>Watch interval</span>

View File

@ -133,7 +133,7 @@ func (ws *WebService) getLogs(w http.ResponseWriter, r *http.Request) {
func (ws *WebService) handleConfig(w http.ResponseWriter, r *http.Request) { func (ws *WebService) handleConfig(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" { if r.Method == "POST" {
newConfig := config.ConfigV2{} newConfig := config.ConfigV3{}
defer r.Body.Close() defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body) b, err := ioutil.ReadAll(r.Body)

View File

@ -78,7 +78,7 @@ func TestGetConfig(t *testing.T) {
t.Errorf("expected error to be nil got %v", err) 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 { if string(b) != exp {
t.Errorf("Got unexpected response\n%v\n%v", string(b), exp) t.Errorf("Got unexpected response\n%v\n%v", string(b), exp)
} }