blasphem/pkg/wsapi/api.go

195 lines
3.3 KiB
Go
Raw Normal View History

2022-12-19 19:24:01 -05:00
package wsapi
import (
2022-12-20 19:05:45 -05:00
"encoding/json"
"errors"
2022-12-20 11:34:25 -05:00
"io"
2022-12-20 20:30:58 -05:00
"strings"
2022-12-19 19:24:01 -05:00
"dynatron.me/x/blasphem/pkg/auth"
2022-12-20 19:05:45 -05:00
"dynatron.me/x/blasphem/pkg/blas/core"
2022-12-19 19:24:01 -05:00
"github.com/gorilla/websocket"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
)
2022-12-20 19:05:45 -05:00
var (
NoSuchHandlerErr = errors.New("bad websocket command")
2022-12-20 20:34:53 -05:00
NoMessageIDErr = errors.New("no message ID")
2022-12-20 19:05:45 -05:00
)
2022-12-19 19:24:01 -05:00
type Type string
type MsgBase struct {
Type Type `json:"type"`
}
type (
wsSession struct {
*websocket.Conn
2022-12-20 19:05:45 -05:00
b core.Blas
2022-12-19 19:24:01 -05:00
ec echo.Context
2022-12-20 13:16:30 -05:00
h phaseHandler
2022-12-19 19:24:01 -05:00
2022-12-20 13:16:30 -05:00
user *auth.User
2022-12-19 19:24:01 -05:00
refreshToken *auth.RefreshToken
}
phaseHandler interface {
2022-12-20 11:34:25 -05:00
handleMsg(io.Reader) error
2022-12-19 19:24:01 -05:00
}
cmdHandler struct {
*wsSession
}
2022-12-20 19:05:45 -05:00
wsEntry struct {
dataNew core.NewData
hnd core.Handler
}
wsRegistry map[string]wsEntry
wsManager struct {
r wsRegistry
}
2022-12-19 19:24:01 -05:00
)
2022-12-20 19:05:45 -05:00
func (wsm *wsManager) RegisterWSCommand(cmd string, hnd core.Handler, dataNew core.NewData) {
wsm.r[cmd] = wsEntry{
dataNew: dataNew,
hnd: hnd,
}
}
2022-12-20 20:34:45 -05:00
func (wsm *wsManager) WSCommandHandler(cmd string, cmdSplit []string) (core.NewData, core.Handler, error) {
if wse, ok := wsm.r[cmd]; ok {
return wse.dataNew, wse.hnd, nil
}
if wse, ok := wsm.r[cmdSplit[0]]; ok {
return wse.dataNew, wse.hnd, nil
2022-12-20 19:05:45 -05:00
}
2022-12-20 20:34:45 -05:00
return nil, nil, NoSuchHandlerErr
2022-12-20 19:05:45 -05:00
}
func NewManager() core.WebSocketManager {
return &wsManager{
r: make(wsRegistry),
}
}
func NewSession(s core.Blas, c echo.Context, conn *websocket.Conn) core.WebSocketSession {
2022-12-19 19:24:01 -05:00
ws := &wsSession{
Conn: conn,
2022-12-20 13:16:30 -05:00
b: s,
ec: c,
2022-12-19 19:24:01 -05:00
}
2022-12-20 19:05:45 -05:00
ws.h = &authPhase{ws}
2022-12-19 19:24:01 -05:00
return ws
}
2022-12-20 20:30:58 -05:00
func (ws *wsSession) WSConn() *websocket.Conn {
return ws.Conn
}
2022-12-20 19:05:45 -05:00
func (ws *wsSession) Blas() core.Blas { return ws.b }
func (ws *wsSession) Go() error {
2022-12-19 19:24:01 -05:00
err := ws.sendAuthRequired()
if err != nil {
return err
}
for {
2022-12-20 11:34:25 -05:00
_, rdr, err := ws.NextReader()
2022-12-19 19:24:01 -05:00
if err != nil {
log.Error().Err(err).Str("remote", ws.ec.Request().RemoteAddr).Msg("websocket read fail")
2022-12-20 11:34:25 -05:00
return err
2022-12-19 19:24:01 -05:00
}
2022-12-20 11:34:25 -05:00
err = ws.h.handleMsg(rdr)
2022-12-19 19:24:01 -05:00
if err != nil {
return err
}
}
}
type cmdMsg struct {
}
type MsgType string
2022-12-20 20:34:53 -05:00
2022-12-20 20:11:11 -05:00
const (
ResultMsgType MsgType = "result"
)
type Error struct {
2022-12-20 20:34:53 -05:00
Code string `json:"code"`
2022-12-20 20:11:11 -05:00
Message string `json:"message"`
}
type WSError struct {
2022-12-20 20:34:53 -05:00
ID *int `json:"id,omitempty"`
Type MsgType `json:"type"`
Success bool `json:"success"`
Error Error `json:"error"`
2022-12-20 20:11:11 -05:00
}
func (ws *cmdHandler) writeError(id int, err Error) error {
return ws.WriteJSON(WSError{
2022-12-20 20:34:53 -05:00
ID: &id,
Type: ResultMsgType,
2022-12-20 20:11:11 -05:00
Success: false,
2022-12-20 20:34:53 -05:00
Error: err,
2022-12-20 20:11:11 -05:00
})
}
2022-12-19 19:24:01 -05:00
2022-12-20 11:34:25 -05:00
func (ws *cmdHandler) handleMsg(r io.Reader) error {
2022-12-20 19:05:45 -05:00
var msgMap map[string]interface{}
err := json.NewDecoder(r).Decode(&msgMap)
if err != nil {
return err
}
msgType, ok := msgMap["type"].(string)
if !ok {
return NoSuchHandlerErr
}
2022-12-20 20:11:11 -05:00
idFl, ok := msgMap["id"].(float64)
2022-12-20 19:05:45 -05:00
if !ok {
2022-12-20 20:11:11 -05:00
return ws.WriteJSON(
WSError{
2022-12-20 20:34:53 -05:00
Type: ResultMsgType,
2022-12-20 20:11:11 -05:00
Success: false,
Error: Error{
2022-12-20 20:34:53 -05:00
Code: "invalid_id",
2022-12-20 20:11:11 -05:00
Message: "command has no ID",
},
})
}
id := int(idFl)
2022-12-20 20:30:58 -05:00
cmd := strings.Split(msgType, "/")
2022-12-20 20:34:45 -05:00
newData, hand, err := ws.b.WSCommandHandler(cmd[0], cmd)
2022-12-20 20:11:11 -05:00
switch err {
case nil:
case NoSuchHandlerErr:
return ws.writeError(id, Error{
2022-12-20 20:34:53 -05:00
Code: "invalid_type",
2022-12-20 20:11:11 -05:00
Message: "no such command",
})
default:
2022-12-20 19:05:45 -05:00
return err
}
2022-12-20 20:30:58 -05:00
nd := newData(cmd)
return hand(ws, id, cmd, nd)
2022-12-19 19:24:01 -05:00
}