feat(web): Go W-G12 rooms, Redis session, and API keys

Add mode=room with room_key hub APIs, optional redis:// session
backend, and web_api_key_check (sha256 pepper:key) for anlian primitives.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-10 20:10:43 +08:00
1 parent 26955f4f36
commit 12d2c7be69
15 files changed
+541 -13

No files matched your search

+55 -9
View File
@@ -2,27 +2,50 @@ package ws
import (
"fmt"
"strings"
"github.com/marqdo/marqdo/plugins/web/internal/app"
)
// RouteWS registers path → {mode} on app.ws_routes (Rust web_app_route_ws).
// RouteSpec is one ws_routes entry (mode + optional room_key template).
type RouteSpec struct {
Mode Mode
RoomKey string
}
// RouteWS registers path → {mode, room_key?} on app.ws_routes (Rust web_app_route_ws).
func RouteWS(appBag map[string]any, path string, args map[string]any) (map[string]any, error) {
path, err := app.NormalizeRoutePath(path, appBag)
if err != nil {
return nil, err
}
mode := ModeFromArgs(args)
roomKey := RoomKeyFromArgs(args)
if mode == ModeRoom && roomKey == "" {
return nil, fmt.Errorf("mode=room requires room_key")
}
out := clone(appBag)
wsRoutes := map[string]any{}
if r, ok := out["ws_routes"].(map[string]any); ok {
wsRoutes = clone(r)
}
wsRoutes[path] = map[string]any{"mode": string(mode)}
spec := map[string]any{"mode": string(mode)}
if roomKey != "" {
spec["room_key"] = roomKey
}
wsRoutes[path] = spec
out["ws_routes"] = wsRoutes
return out, nil
}
// RoomKeyFromArgs reads room_key / 房间 from route_ws ABI args.
func RoomKeyFromArgs(args map[string]any) string {
if v, ok := first(args, "room_key", "房间"); ok {
return strings.TrimSpace(fmt.Sprint(v))
}
return ""
}
// RoutesOf reads ws_routes from an app bag.
func RoutesOf(appBag map[string]any) map[string]Mode {
out := map[string]Mode{}
@@ -57,19 +80,42 @@ func clone(m map[string]any) map[string]any {
// ModeOf returns the mode for one ws route path, or error if missing.
func ModeOf(appBag map[string]any, path string) (Mode, error) {
spec, err := RouteSpecOf(appBag, path)
if err != nil {
return "", err
}
return spec.Mode, nil
}
// RouteSpecOf returns mode and room_key for one ws route path.
func RouteSpecOf(appBag map[string]any, path string) (RouteSpec, error) {
routes, ok := appBag["ws_routes"].(map[string]any)
if !ok {
return "", fmt.Errorf("missing ws route %s", path)
return RouteSpec{}, fmt.Errorf("missing ws route %s", path)
}
spec, ok := routes[path]
if !ok {
return "", fmt.Errorf("missing ws route %s", path)
return RouteSpec{}, fmt.Errorf("missing ws route %s", path)
}
if m, ok := spec.(map[string]any); ok {
if mode, ok := m["mode"]; ok {
return ParseMode(mode), nil
return parseRouteSpec(spec), nil
}
func parseRouteSpec(spec any) RouteSpec {
out := RouteSpec{Mode: ModeEcho}
switch t := spec.(type) {
case map[string]any:
if mode, ok := t["mode"]; ok {
out.Mode = ParseMode(mode)
} else {
out.Mode = ParseMode(t)
}
return ParseMode(m), nil
if rk, ok := t["room_key"]; ok {
out.RoomKey = strings.TrimSpace(fmt.Sprint(rk))
}
case string:
out.Mode = ParseMode(t)
default:
out.Mode = ParseMode(spec)
}
return ParseMode(spec), nil
return out
}