update webinfo control panel

This commit is contained in:
Aynakeya
2022-07-14 00:51:26 -07:00
parent 8602e6470a
commit 9e7b062790
5 changed files with 132 additions and 18 deletions

View File

@@ -19,11 +19,13 @@ var upgrader = websocket.Upgrader{
}
type WebInfoServer struct {
Info OutInfo
Server *http.Server
Clients map[*Client]int
Running bool
lock sync.Mutex
Info OutInfo
Port int
ServerMux *http.ServeMux
Server *http.Server
Clients map[*Client]int
Running bool
lock sync.Mutex
}
type Client struct {
@@ -34,16 +36,15 @@ type Client struct {
func NewWebInfoServer(port int) *WebInfoServer {
server := &WebInfoServer{
Port: port,
Clients: map[*Client]int{},
}
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir(config.GetAssetPath("webinfo"))))
mux.HandleFunc("/ws/info", server.handleInfo)
mux.HandleFunc("/api/info", server.getInfo)
server.Server = &http.Server{
Addr: fmt.Sprintf("localhost:%d", port),
Handler: mux,
}
server.ServerMux = mux
return server
}
@@ -128,12 +129,17 @@ func (s *WebInfoServer) removeClient(c *Client) {
}
func (s *WebInfoServer) Start() {
lg.Debug("WebInfoServer starting...")
s.Running = true
go func() {
s.Server = &http.Server{
Addr: fmt.Sprintf("localhost:%d", s.Port),
Handler: s.ServerMux,
}
err := s.Server.ListenAndServe()
s.Running = false
if err == http.ErrServerClosed {
lg.Info("server closed")
lg.Info("WebInfoServer closed")
return
}
if err != nil {
@@ -144,8 +150,9 @@ func (s *WebInfoServer) Start() {
}
func (s *WebInfoServer) Stop() error {
lg.Debug("WebInfoServer stopping...")
s.lock.Lock()
s.Clients = map[*Client]int{}
s.lock.Unlock()
return s.Server.Shutdown(context.Background())
return s.Server.Shutdown(context.TODO())
}

View File

@@ -9,6 +9,9 @@ import (
"AynaLivePlayer/logger"
"AynaLivePlayer/player"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/aynakeya/go-mpv"
)
@@ -20,6 +23,7 @@ var lg = logger.Logger.WithField("Module", MODULE_PLGUIN_WEBINFO)
type WebInfo struct {
Port int
server *WebInfoServer
panel fyne.CanvasObject
}
func NewWebInfo() *WebInfo {
@@ -132,9 +136,79 @@ func (t *WebInfo) registerHandlers() {
OutInfo{Lyric: t.server.Info.Lyric},
)
})
}
func (w *WebInfo) getServerStatusText() string {
if w.server.Running {
return i18n.T("plugin.webinfo.server_status.running")
} else {
return i18n.T("plugin.webinfo.server_status.stopped")
}
}
func (w *WebInfo) CreatePanel() fyne.CanvasObject {
return widget.NewLabel("No Setting")
if w.panel != nil {
return w.panel
}
statusText := widget.NewLabel("")
serverStatus := container.NewHBox(
widget.NewLabel(i18n.T("plugin.webinfo.server_status")),
statusText,
)
statusText.SetText(w.getServerStatusText())
serverPort := container.NewBorder(nil, nil,
widget.NewLabel(i18n.T("plugin.webinfo.port")), nil,
widget.NewEntryWithData(binding.IntToString(binding.BindInt(&w.Port))),
)
stopBtn := gui.NewAsyncButtonWithIcon(
i18n.T("plugin.webinfo.server_control.stop"),
theme.MediaStopIcon(),
func() {
if !w.server.Running {
return
}
lg.Info("User try stop webinfo server")
err := w.server.Stop()
if err != nil {
lg.Warnf("stop server have error: %s", err)
return
}
statusText.SetText(w.getServerStatusText())
},
)
startBtn := gui.NewAsyncButtonWithIcon(
i18n.T("plugin.webinfo.server_control.start"),
theme.MediaPlayIcon(),
func() {
if w.server.Running {
return
}
lg.Infof("User try start webinfo server with port %d", w.Port)
w.server.Port = w.Port
w.server.Start()
statusText.SetText(w.getServerStatusText())
},
)
restartBtn := gui.NewAsyncButtonWithIcon(
i18n.T("plugin.webinfo.server_control.restart"),
theme.MediaReplayIcon(),
func() {
lg.Infof("User try restart webinfo server with port %d", w.Port)
if w.server.Running {
if err := w.server.Stop(); err != nil {
lg.Warnf("stop server have error: %s", err)
return
}
}
w.server.Port = w.Port
w.server.Start()
statusText.SetText(w.getServerStatusText())
},
)
ctrlBtns := container.NewHBox(
widget.NewLabel(i18n.T("plugin.webinfo.server_control")),
startBtn, stopBtn, restartBtn,
)
w.panel = container.NewVBox(serverStatus, serverPort, ctrlBtns)
return w.panel
}