newdata can switch on command

This commit is contained in:
Daniel Ponte 2022-12-20 20:30:58 -05:00
parent caa371eff1
commit 76f95a422b
3 changed files with 26 additions and 6 deletions

View file

@ -8,6 +8,8 @@ import (
"dynatron.me/x/blasphem/pkg/components"
"dynatron.me/x/blasphem/pkg/config"
"dynatron.me/x/blasphem/pkg/storage"
"github.com/gorilla/websocket"
)
type Blas interface {
@ -32,12 +34,13 @@ type WebSocketManager interface {
}
type WebSocketSession interface {
WSConn() *websocket.Conn
Go() error
Blas() Blas
}
type Handler func(wss WebSocketSession, msgID int, msg interface{}) error
type NewData func() interface{}
type Handler func(wss WebSocketSession, msgID int, cmd []string, msg interface{}) error
type NewData func(cmd []string) interface{}
type Shutdowner interface {
ShutdownBlas(context.Context) error

View file

@ -52,7 +52,15 @@ func (fe *Frontend) AliasHandler(toFile string) echo.HandlerFunc {
func (*Frontend) Shutdown() {}
func Setup(_ core.Blas) (components.Component, error) {
func newData(_ []string) interface{} {
return map[string]interface{}{}
}
func wsHand(wss core.WebSocketSession, msgID int, cmd []string, msg interface{}) error {
return nil
}
func Setup(b core.Blas) (components.Component, error) {
fe := &Frontend{}
var err error
@ -63,6 +71,8 @@ func Setup(_ core.Blas) (components.Component, error) {
fe.fsHandler = echo.StaticDirectoryHandler(fe.rootFS, false)
b.RegisterWSCommand("frontend", wsHand, newData)
return fe, nil
}

View file

@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"io"
"strings"
"dynatron.me/x/blasphem/pkg/auth"
"dynatron.me/x/blasphem/pkg/blas/core"
@ -88,6 +89,10 @@ func NewSession(s core.Blas, c echo.Context, conn *websocket.Conn) core.WebSocke
return ws
}
func (ws *wsSession) WSConn() *websocket.Conn {
return ws.Conn
}
func (ws *wsSession) Blas() core.Blas { return ws.b }
func (ws *wsSession) Go() error {
@ -166,7 +171,9 @@ func (ws *cmdHandler) handleMsg(r io.Reader) error {
id := int(idFl)
newData, hand, err := ws.b.WSCommandHandler(msgType)
cmd := strings.Split(msgType, "/")
newData, hand, err := ws.b.WSCommandHandler(cmd[0])
switch err {
case nil:
case NoSuchHandlerErr:
@ -178,6 +185,6 @@ func (ws *cmdHandler) handleMsg(r io.Reader) error {
return err
}
nd := newData()
return hand(ws, id, nd)
nd := newData(cmd)
return hand(ws, id, cmd, nd)
}