RWKV-Runner/backend-golang/app.go

309 lines
6.7 KiB
Go
Raw Normal View History

2023-05-05 23:23:34 +08:00
package backend_golang
2023-05-03 23:38:54 +08:00
import (
2024-02-05 00:25:04 +08:00
"archive/zip"
2023-08-27 22:53:18 +08:00
"bufio"
2024-02-05 00:25:04 +08:00
"bytes"
2023-05-03 23:38:54 +08:00
"context"
2023-06-20 22:22:14 +08:00
"errors"
2023-11-09 21:38:02 +08:00
"io"
2024-03-26 21:23:09 +08:00
"log"
"net"
2023-05-17 23:27:52 +08:00
"net/http"
2024-03-26 21:23:09 +08:00
"net/http/httputil"
"net/url"
2023-05-18 19:25:13 +08:00
"os"
"os/exec"
2023-06-01 16:54:21 +08:00
"path/filepath"
2023-05-20 23:34:33 +08:00
"runtime"
2024-02-05 00:25:04 +08:00
"strings"
2023-08-27 22:53:18 +08:00
"syscall"
2023-11-09 21:38:02 +08:00
"time"
2023-05-17 23:27:52 +08:00
2023-07-03 17:41:47 +08:00
"github.com/fsnotify/fsnotify"
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 {
2023-06-20 22:22:14 +08:00
ctx context.Context
HasConfigData bool
ConfigData map[string]any
2024-02-03 20:29:56 +08:00
Dev bool
2024-03-26 21:23:09 +08:00
proxyPort int
2023-06-20 22:22:14 +08:00
exDir string
cmdPrefix string
2023-05-03 23:38:54 +08:00
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
2024-03-26 21:23:09 +08:00
func (a *App) newFetchProxy() {
go func() {
handler := func(w http.ResponseWriter, r *http.Request) {
if r.Method == "OPTIONS" {
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Allow-Origin", "*")
return
}
proxy := &httputil.ReverseProxy{
ModifyResponse: func(res *http.Response) error {
res.Header.Set("Access-Control-Allow-Origin", "*")
return nil
},
Director: func(req *http.Request) {
realTarget := req.Header.Get("Real-Target")
if realTarget != "" {
2024-03-26 22:25:24 +08:00
realTarget, err := url.PathUnescape(realTarget)
if err != nil {
log.Printf("Error decoding target URL: %v\n", err)
return
}
2024-03-26 21:23:09 +08:00
target, err := url.Parse(realTarget)
if err != nil {
log.Printf("Error parsing target URL: %v\n", err)
return
}
req.Header.Set("Accept", "*/*")
req.Header.Del("Origin")
req.Header.Del("Referer")
req.Header.Del("Real-Target")
req.Header.Del("Sec-Fetch-Dest")
req.Header.Del("Sec-Fetch-Mode")
req.Header.Del("Sec-Fetch-Site")
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.URL.Path = target.Path
2024-03-26 22:25:24 +08:00
req.URL.RawQuery = url.PathEscape(target.RawQuery)
2024-03-26 21:23:09 +08:00
log.Println("Proxying to", realTarget)
} else {
log.Println("Real-Target header is missing")
}
},
}
proxy.ServeHTTP(w, r)
}
http.HandleFunc("/", handler)
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return
}
a.proxyPort = listener.Addr().(*net.TCPAddr).Port
http.Serve(listener, nil)
}()
}
2023-05-03 23:38:54 +08:00
// 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-06-02 22:20:57 +08:00
a.exDir = ""
2023-06-01 16:54:21 +08:00
a.cmdPrefix = ""
2024-02-02 22:00:01 +08:00
ex, err := os.Executable()
if err == nil {
if runtime.GOOS == "darwin" {
a.exDir = filepath.Dir(ex) + "/../../../"
a.cmdPrefix = "cd " + a.exDir + " && "
} else {
a.exDir = filepath.Dir(ex) + "/"
a.cmdPrefix = "cd " + a.exDir + " && "
}
2024-02-03 20:29:56 +08:00
if a.Dev {
a.exDir = ""
} else {
os.Chdir(a.exDir)
}
2023-06-01 16:54:21 +08:00
}
2023-05-20 13:00:08 +08:00
2023-11-20 20:12:20 +08:00
os.Chmod(a.exDir+"backend-rust/webgpu_server", 0777)
os.Chmod(a.exDir+"backend-rust/web-rwkv-converter", 0777)
2023-07-09 12:10:14 +08:00
os.Mkdir(a.exDir+"models", os.ModePerm)
os.Mkdir(a.exDir+"lora-models", os.ModePerm)
os.Mkdir(a.exDir+"finetune/json2binidx_tool/data", os.ModePerm)
2024-02-02 22:00:01 +08:00
trainLogPath := "lora-models/train_log.txt"
2023-12-22 13:00:13 +08:00
if !a.FileExists(trainLogPath) {
2024-02-02 22:00:01 +08:00
f, err := os.Create(a.exDir + trainLogPath)
2023-12-22 13:00:13 +08:00
if err == nil {
f.Close()
}
2023-07-09 12:10:14 +08:00
}
2023-05-20 13:00:08 +08:00
a.downloadLoop()
2023-11-29 14:05:58 +08:00
a.midiLoop()
2023-08-27 22:53:18 +08:00
a.watchFs()
a.monitorHardware()
2024-03-26 21:23:09 +08:00
a.newFetchProxy()
2023-08-27 22:53:18 +08:00
}
func (a *App) OnBeforeClose(ctx context.Context) bool {
if monitor != nil {
monitor.Process.Kill()
}
return false
}
2023-07-03 17:41:47 +08:00
2023-08-27 22:53:18 +08:00
func (a *App) watchFs() {
2023-07-03 17:41:47 +08:00
watcher, err := fsnotify.NewWatcher()
if err == nil {
2023-11-21 22:30:42 +08:00
watcher.Add(a.exDir + "./lora-models")
watcher.Add(a.exDir + "./models")
2023-07-03 17:41:47 +08:00
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
2023-08-27 22:53:18 +08:00
wruntime.EventsEmit(a.ctx, "fsnotify", event.Name)
2023-07-03 17:41:47 +08:00
case _, ok := <-watcher.Errors:
if !ok {
return
}
}
}
}()
}
2023-05-03 23:38:54 +08:00
}
2023-05-17 23:27:52 +08:00
2023-08-27 22:53:18 +08:00
var monitor *exec.Cmd
func (a *App) monitorHardware() {
if runtime.GOOS != "windows" {
return
}
monitor = exec.Command("./components/LibreHardwareMonitor.Console/LibreHardwareMonitor.Console.exe")
stdout, err := monitor.StdoutPipe()
if err != nil {
monitor = nil
return
}
go func() {
reader := bufio.NewReader(stdout)
for {
line, _, err := reader.ReadLine()
if err != nil {
wruntime.EventsEmit(a.ctx, "monitorerr", err.Error())
break
}
wruntime.EventsEmit(a.ctx, "monitor", string(line))
}
}()
2023-08-27 23:56:30 +08:00
monitor.SysProcAttr = &syscall.SysProcAttr{}
//go:custom_build windows monitor.SysProcAttr.HideWindow = true
2023-08-27 22:53:18 +08:00
monitor.Start()
}
2023-11-09 21:38:02 +08:00
type ProgressReader struct {
reader io.Reader
total int64
err error
}
func (pr *ProgressReader) Read(p []byte) (n int, err error) {
n, err = pr.reader.Read(p)
pr.err = err
pr.total += int64(n)
return
}
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()
2023-11-09 21:38:02 +08:00
pr := &ProgressReader{reader: resp.Body}
ticker := time.NewTicker(250 * time.Millisecond)
defer ticker.Stop()
2024-02-05 00:25:04 +08:00
// update progress
2023-11-09 21:38:02 +08:00
go func() {
for {
<-ticker.C
wruntime.EventsEmit(a.ctx, "updateApp", &DownloadStatus{
Name: filepath.Base(url),
Path: "",
Url: url,
Transferred: pr.total,
Size: resp.ContentLength,
Speed: 0,
Progress: 100 * (float64(pr.total) / float64(resp.ContentLength)),
Downloading: pr.err == nil && pr.total < resp.ContentLength,
Done: pr.total == resp.ContentLength,
})
if pr.err != nil || pr.total == resp.ContentLength {
break
}
}
}()
2024-02-05 00:25:04 +08:00
var updateFile io.Reader = pr
// extract macos binary from zip
if strings.HasSuffix(url, ".zip") && runtime.GOOS == "darwin" {
zipBytes, err := io.ReadAll(pr)
if err != nil {
return false, err
}
archive, err := zip.NewReader(bytes.NewReader(zipBytes), int64(len(zipBytes)))
if err != nil {
return false, err
}
2024-02-05 12:31:26 +08:00
file, err := archive.Open("RWKV-Runner.app/Contents/MacOS/RWKV-Runner")
2024-02-05 00:25:04 +08:00
if err != nil {
return false, err
}
defer file.Close()
updateFile = file
}
// apply update
err = selfupdate.Apply(updateFile, selfupdate.Options{})
2023-05-17 23:27:52 +08:00
if err != nil {
if rerr := selfupdate.RollbackError(err); rerr != nil {
return true, rerr
}
return false, err
}
2024-02-05 00:25:04 +08:00
// restart app
if runtime.GOOS == "windows" {
name, err := os.Executable()
if err != nil {
return false, err
}
exec.Command(name, os.Args[1:]...).Start()
wruntime.Quit(a.ctx)
2023-05-18 19:25:13 +08:00
}
2023-05-17 23:27:52 +08:00
return false, nil
}
2023-05-20 23:34:33 +08:00
2023-06-20 22:22:14 +08:00
func (a *App) RestartApp() error {
if runtime.GOOS == "windows" {
name, err := os.Executable()
if err != nil {
return err
}
exec.Command(name, os.Args[1:]...).Start()
wruntime.Quit(a.ctx)
return nil
}
return errors.New("unsupported OS")
}
2023-05-20 23:34:33 +08:00
func (a *App) GetPlatform() string {
return runtime.GOOS
}
2024-03-26 21:23:09 +08:00
func (a *App) GetProxyPort() int {
return a.proxyPort
}