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.
This commit is contained in:
Harvey Tindall 2021-06-01 19:54:13 +01:00
parent aa40a72075
commit e49996c401
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
3 changed files with 39 additions and 20 deletions

8
api.go
View File

@ -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()

15
main.go
View File

@ -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

36
tray.go
View File

@ -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: