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

View file

@ -52,7 +52,15 @@ func (fe *Frontend) AliasHandler(toFile string) echo.HandlerFunc {
func (*Frontend) Shutdown() {} 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{} fe := &Frontend{}
var err error var err error
@ -63,6 +71,8 @@ func Setup(_ core.Blas) (components.Component, error) {
fe.fsHandler = echo.StaticDirectoryHandler(fe.rootFS, false) fe.fsHandler = echo.StaticDirectoryHandler(fe.rootFS, false)
b.RegisterWSCommand("frontend", wsHand, newData)
return fe, nil return fe, nil
} }

View file

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