Skip to content

Commit

Permalink
Wire-pod is now fully stable on Windows
Browse files Browse the repository at this point in the history
Former-commit-id: 2ea10c2
  • Loading branch information
kercre123 committed Nov 12, 2023
1 parent 9b60019 commit d52335d
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 160 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/chipper/vbuild/botpack.tar.gz
/chipper/windows/tmp
/chipper/windows/chipper.exe
/chipper/windows/wire-pod-win.zip
/chipper/windows/*.zip
/chipper/vbuild/built-libs
/chipper/windows/libs
/chipper/windows/opus
Expand Down
116 changes: 46 additions & 70 deletions chipper/cmd/windows/main.go
Original file line number Diff line number Diff line change
@@ -1,66 +1,64 @@
package main

import (
"image/color"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"runtime"
"strconv"
"strings"

"gioui.org/app"
"gioui.org/font/gofont"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"github.com/digital-dream-labs/hugh/log"
"github.com/getlantern/systray"
"github.com/kercre123/chipper/pkg/logger"
"github.com/kercre123/chipper/pkg/wirepod/sdkapp"
botsetup "github.com/kercre123/chipper/pkg/wirepod/setup"
stt "github.com/kercre123/chipper/pkg/wirepod/stt/vosk"
"github.com/ncruces/zenity"
)

var (
editor widget.Editor
list layout.List
)

var mBoxTitle = "wire-pod"
var mBoxNeedsSetupMsg = `Wire-pod is now running in the background. You must set it up by heading to "http://localhost:8080" in a browser.`
var mBoxError = `There was an error starting wire-pod: `
var mBoxAlreadyRunning = "Wire-pod is already running. You must quit that instance before starting another one. Exiting."
var mBoxExit = `Wire-pod is exiting.`
var mBoxSuccess = `Wire-pod has started successfully! It is now running in the background. It can be stopped in the system tray.`
var mBoxIcon = "./icons/start-up-full.png"

func getNeedsSetupMsg() string {
return `Wire-pod is now running in the background. You must set it up by heading to http://` + botsetup.GetOutboundIP().String() + `:` + sdkapp.WebPort + ` in a browser.`
}

func main() {
hcl := http.Client{}
hcl.Timeout = 2
resp, err := hcl.Get("http://localhost:8080/api/is_running")
if os.Getenv("WEBSERVER_PORT") != "" {
if _, err := strconv.Atoi(os.Getenv("WEBSERVER_PORT")); err == nil {
sdkapp.WebPort = os.Getenv("WEBSERVER_PORT")
} else {
logger.Println("WEBSERVER_PORT contains letters, using default of 8080")
sdkapp.WebPort = "8080"
}
} else {
sdkapp.WebPort = "8080"
}
resp, err := http.Get("http://" + botsetup.GetOutboundIP().String() + ":" + sdkapp.WebPort + "/api/is_running")
if err == nil {
body, _ := io.ReadAll(resp.Body)
if strings.TrimSpace(string(body)) == "true" {
zenity.Error(mBoxAlreadyRunning,
zenity.ErrorIcon,
zenity.Title(mBoxTitle))
os.Exit(0)
os.Exit(1)
} else {
zenity.Error("Port 8080 is in use by another program. Close that program before starting wire-pod. Exiting.",
zenity.Error("Port "+sdkapp.WebPort+" is in use by another program. Close that program before starting wire-pod. Exiting.",
zenity.ErrorIcon,
zenity.Title(mBoxTitle))
os.Exit(0)
os.Exit(1)
}
}
go app.Main()
systray.Run(onReady, onExit)
}

func onExit() {
zenity.Info(
mBoxExit,
zenity.Icon(mBoxIcon),
zenity.Title(mBoxTitle),
)
os.Exit(0)
}

Expand All @@ -83,64 +81,42 @@ func onReady() {
systray.SetTitle("wire-pod")
systray.SetTooltip("wire-pod is starting...")
mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
mLogs := systray.AddMenuItem("View Logs", "Open the logs")
mBrowse := systray.AddMenuItem("Web interface", "Open web UI")

go func() {
for {
select {
case <-mQuit.ClickedCh:
zenity.Info(
mBoxExit,
"Wire-pod will now exit.",
zenity.Icon(mBoxIcon),
zenity.Title(mBoxTitle),
)
os.Exit(0)
case <-mLogs.ClickedCh:
go ShowLogs()
case <-mBrowse.ClickedCh:
go openBrowser("http://" + botsetup.GetOutboundIP().String() + ":" + sdkapp.WebPort)
}
}
}()

StartFromProgramInit(stt.Init, stt.STT, stt.Name)
}

func ShowLogs() error {
w := app.NewWindow(app.Title("wire-pod log viewer"), app.Size(unit.Dp(600), unit.Dp(400)))
th := material.NewTheme()
th.Shaper = text.NewShaper(text.WithCollection(gofont.Collection()))

var ops op.Ops
editor := new(widget.Editor)
editor.SingleLine = false
editor.ReadOnly = true // Make the editor read-only

go func() {
chann := logger.GetChan()
for range chann {
w.Invalidate()
}
}()

for {
e := <-w.Events()
switch e := e.(type) {
case system.DestroyEvent:
return e.Err
case system.FrameEvent:
gtx := layout.NewContext(&ops, e)

// Update the editor's text with logger.TrayLogList
editor.SetText(logger.TrayLogList)
func openBrowser(url string) {
var err error

switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}

layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
editor := material.Editor(th, editor, "")
editor.TextSize = unit.Sp(16)
editor.Color = color.NRGBA{R: 127, G: 0, B: 0, A: 255}
return editor.Layout(gtx)
}),
)
e.Frame(gtx.Ops)
}
if err != nil {
log.Fatal(err)
}
}
Loading

0 comments on commit d52335d

Please sign in to comment.