1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 09:00:10 +00:00

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 { if err := tempConfig.SaveTo(app.configPath); err != nil {
app.err.Printf("Failed to save config to \"%s\": %v", app.configPath, err) app.err.Printf("Failed to save config to \"%s\": %v", app.configPath, err)
respondBool(500, false, gc) respondBool(500, false, gc)
@ -1585,9 +1586,10 @@ func (app *appContext) ModifyConfig(gc *gin.Context) {
gc.JSON(200, map[string]bool{"success": true}) gc.JSON(200, map[string]bool{"success": true})
if req["restart-program"] != nil && req["restart-program"].(bool) { if req["restart-program"] != nil && req["restart-program"].(bool) {
app.info.Println("Restarting...") app.info.Println("Restarting...")
err := app.Restart() if TRAY {
if err != nil { TRAYRESTART <- true
app.err.Printf("Couldn't restart, try restarting manually: %s", err) } else {
RESTART <- true
} }
} }
app.loadConfig() app.loadConfig()

15
main.go
View File

@ -36,6 +36,7 @@ var (
SOCK string = "jfa-go.sock" SOCK string = "jfa-go.sock"
SRV *http.Server SRV *http.Server
RESTART chan bool RESTART chan bool
TRAYRESTART chan bool
DATA, CONFIG, HOST *string DATA, CONFIG, HOST *string
PORT *int PORT *int
DEBUG *bool DEBUG *bool
@ -201,6 +202,9 @@ func start(asDaemon, firstCall bool) {
app.err.Fatalf("Couldn't copy default config.") app.err.Fatalf("Couldn't copy default config.")
} }
app.info.Printf("Copied default configuration to \"%s\"", app.configPath) 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 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) 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() app.version = app.config.Section("jellyfin").Key("version").String()
// read from config... // read from config...
debugMode = app.config.Section("ui").Key("debug").MustBool(false) 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.info.Println("Using Jellyfin server type")
} }
app.jf, _ = mediabrowser.NewServer( app.jf, err = mediabrowser.NewServer(
serverType, serverType,
server, server,
app.config.Section("jellyfin").Key("client").String(), app.config.Section("jellyfin").Key("client").String(),
@ -404,6 +412,9 @@ func start(asDaemon, firstCall bool) {
timeoutHandler, timeoutHandler,
cacheTimeout, cacheTimeout,
) )
if err != nil {
app.err.Fatalf("Failed to authenticate with Jellyfin @ %s: %v", server, err)
}
if debugMode { if debugMode {
app.jf.Verbose = true app.jf.Verbose = true
} }
@ -589,7 +600,7 @@ func flagPassed(name string) (found bool) {
} }
// @title jfa-go internal API // @title jfa-go internal API
// @version 0.3.4 // @version 0.3.6
// @description API for the jfa-go frontend // @description API for the jfa-go frontend
// @contact.name Harvey Tindall // @contact.name Harvey Tindall
// @contact.email hrfee@hrfee.dev // @contact.email hrfee@hrfee.dev

36
tray.go
View File

@ -53,11 +53,29 @@ func onReady() {
}() }()
RESTART = make(chan bool, 1) RESTART = make(chan bool, 1)
TRAYRESTART = make(chan bool, 1)
go start(false, true) go start(false, true)
mStart.Disable() mStart.Disable()
mStop.Enable() mStop.Enable()
mRestart.Enable() mRestart.Enable()
go as.HandleCheck() 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 { for {
select { select {
case <-mStart.ClickedCh: case <-mStart.ClickedCh:
@ -74,22 +92,10 @@ func onReady() {
mStart.Enable() mStart.Enable()
mRestart.Disable() mRestart.Disable()
} }
case <-TRAYRESTART:
trayRestart()
case <-mRestart.ClickedCh: case <-mRestart.ClickedCh:
if RUNNING { trayRestart()
RESTART <- true
mStop.Disable()
mStart.Enable()
mRestart.Disable()
for {
if !RUNNING {
break
}
}
go start(false, false)
mStart.Disable()
mStop.Enable()
mRestart.Enable()
}
case <-mOpenLogs.ClickedCh: case <-mOpenLogs.ClickedCh:
open.Start(logPath) open.Start(logPath)
case <-mQuit.ClickedCh: case <-mQuit.ClickedCh: