From e49996c401ba545599060949181fd2a8ddf89866 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Tue, 1 Jun 2021 19:54:13 +0100 Subject: [PATCH] Setup: Don't break if setup not completed, fix restart Since an invalid example config was created on first run, if the app restarted before setup was completed, it would crash on the next start. The example now has a "first_run" flag in it, which is only set to false when the config is modified. Also fixed restart at the end of setup for tray builds. --- api.go | 8 +++++--- main.go | 15 +++++++++++++-- tray.go | 36 +++++++++++++++++++++--------------- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/api.go b/api.go index d21645b..472e5dd 100644 --- a/api.go +++ b/api.go @@ -1576,6 +1576,7 @@ func (app *appContext) ModifyConfig(gc *gin.Context) { } } } + tempConfig.Section("").Key("first_run").SetValue("false") if err := tempConfig.SaveTo(app.configPath); err != nil { app.err.Printf("Failed to save config to \"%s\": %v", app.configPath, err) respondBool(500, false, gc) @@ -1585,9 +1586,10 @@ func (app *appContext) ModifyConfig(gc *gin.Context) { gc.JSON(200, map[string]bool{"success": true}) if req["restart-program"] != nil && req["restart-program"].(bool) { app.info.Println("Restarting...") - err := app.Restart() - if err != nil { - app.err.Printf("Couldn't restart, try restarting manually: %s", err) + if TRAY { + TRAYRESTART <- true + } else { + RESTART <- true } } app.loadConfig() diff --git a/main.go b/main.go index fd3cc94..0735987 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,7 @@ var ( SOCK string = "jfa-go.sock" SRV *http.Server RESTART chan bool + TRAYRESTART chan bool DATA, CONFIG, HOST *string PORT *int DEBUG *bool @@ -201,6 +202,9 @@ func start(asDaemon, firstCall bool) { app.err.Fatalf("Couldn't copy default config.") } app.info.Printf("Copied default configuration to \"%s\"", app.configPath) + tempConfig, _ := ini.Load(app.configPath) + tempConfig.Section("").Key("first_run").SetValue("true") + tempConfig.SaveTo(app.configPath) } var debugMode bool @@ -209,6 +213,10 @@ func start(asDaemon, firstCall bool) { app.err.Fatalf("Failed to load config file \"%s\": %v", app.configPath, err) } + if app.config.Section("").Key("first_run").MustBool(false) { + firstRun = true + } + app.version = app.config.Section("jellyfin").Key("version").String() // read from config... debugMode = app.config.Section("ui").Key("debug").MustBool(false) @@ -394,7 +402,7 @@ func start(asDaemon, firstCall bool) { app.info.Println("Using Jellyfin server type") } - app.jf, _ = mediabrowser.NewServer( + app.jf, err = mediabrowser.NewServer( serverType, server, app.config.Section("jellyfin").Key("client").String(), @@ -404,6 +412,9 @@ func start(asDaemon, firstCall bool) { timeoutHandler, cacheTimeout, ) + if err != nil { + app.err.Fatalf("Failed to authenticate with Jellyfin @ %s: %v", server, err) + } if debugMode { app.jf.Verbose = true } @@ -589,7 +600,7 @@ func flagPassed(name string) (found bool) { } // @title jfa-go internal API -// @version 0.3.4 +// @version 0.3.6 // @description API for the jfa-go frontend // @contact.name Harvey Tindall // @contact.email hrfee@hrfee.dev diff --git a/tray.go b/tray.go index 94679d1..4c16aba 100644 --- a/tray.go +++ b/tray.go @@ -53,11 +53,29 @@ func onReady() { }() RESTART = make(chan bool, 1) + TRAYRESTART = make(chan bool, 1) go start(false, true) mStart.Disable() mStop.Enable() mRestart.Enable() go as.HandleCheck() + trayRestart := func() { + if RUNNING { + RESTART <- true + mStop.Disable() + mStart.Enable() + mRestart.Disable() + for { + if !RUNNING { + break + } + } + go start(false, false) + mStart.Disable() + mStop.Enable() + mRestart.Enable() + } + } for { select { case <-mStart.ClickedCh: @@ -74,22 +92,10 @@ func onReady() { mStart.Enable() mRestart.Disable() } + case <-TRAYRESTART: + trayRestart() case <-mRestart.ClickedCh: - if RUNNING { - RESTART <- true - mStop.Disable() - mStart.Enable() - mRestart.Disable() - for { - if !RUNNING { - break - } - } - go start(false, false) - mStart.Disable() - mStop.Enable() - mRestart.Enable() - } + trayRestart() case <-mOpenLogs.ClickedCh: open.Start(logPath) case <-mQuit.ClickedCh: