RWKV-Runner/backend-golang/app.go

57 lines
1.0 KiB
Go
Raw Permalink Normal View History

2023-05-05 23:23:34 +08:00
package backend_golang
2023-05-03 23:38:54 +08:00
import (
"context"
2023-05-17 23:27:52 +08:00
"net/http"
2023-05-18 19:25:13 +08:00
"os"
"os/exec"
2023-05-20 23:34:33 +08:00
"runtime"
2023-05-17 23:27:52 +08:00
"github.com/minio/selfupdate"
2023-05-20 23:34:33 +08:00
wruntime "github.com/wailsapp/wails/v2/pkg/runtime"
2023-05-03 23:38:54 +08:00
)
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
2023-05-05 23:23:34 +08:00
func (a *App) OnStartup(ctx context.Context) {
2023-05-03 23:38:54 +08:00
a.ctx = ctx
2023-05-20 13:00:08 +08:00
a.downloadLoop()
2023-05-03 23:38:54 +08:00
}
2023-05-17 23:27:52 +08:00
func (a *App) UpdateApp(url string) (broken bool, err error) {
resp, err := http.Get(url)
if err != nil {
return false, err
}
defer resp.Body.Close()
err = selfupdate.Apply(resp.Body, selfupdate.Options{})
if err != nil {
if rerr := selfupdate.RollbackError(err); rerr != nil {
return true, rerr
}
return false, err
}
2023-05-18 19:25:13 +08:00
name, err := os.Executable()
if err != nil {
return false, err
}
exec.Command(name, os.Args[1:]...).Start()
2023-05-20 23:34:33 +08:00
wruntime.Quit(a.ctx)
2023-05-17 23:27:52 +08:00
return false, nil
}
2023-05-20 23:34:33 +08:00
func (a *App) GetPlatform() string {
return runtime.GOOS
}