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

+15
View File
@@ -45,6 +45,21 @@ func unsubscribe(path string, ch chan string) {
room.mu.Unlock()
}
// JoinRoom subscribes to a named room (G-WS1). Same backing store as path broadcast.
func JoinRoom(room string) chan string {
return subscribe(room)
}
// LeaveRoom unsubscribes from a named room (G-WS1).
func LeaveRoom(room string, ch chan string) {
unsubscribe(room, ch)
}
// PublishRoom fans out text to all subscribers in a named room (G-WS1).
func PublishRoom(room string, text string) {
Publish(room, text)
}
// Publish fans out text to all subscribers on path (Rust ws_hub::publish).
func Publish(path string, text string) {
globalHub.mu.Lock()
+15
View File
@@ -5,6 +5,21 @@ import (
"time"
)
func TestNamedRoomJoinPublishLeave(t *testing.T) {
room := "chat.room.42"
ch := JoinRoom(room)
ch2 := JoinRoom(room)
PublishRoom(room, "hello-room")
if got := readOne(ch); got != "hello-room" {
t.Fatalf("ch1=%q", got)
}
if got := readOne(ch2); got != "hello-room" {
t.Fatalf("ch2=%q", got)
}
LeaveRoom(room, ch)
LeaveRoom(room, ch2)
}
func TestBroadcastFanout(t *testing.T) {
path := "/t-fanout-go"
ch := subscribe(path)
+3
View File
@@ -12,6 +12,7 @@ const (
ModeEcho Mode = "echo"
ModeBroadcast Mode = "broadcast"
ModeDrain Mode = "drain"
ModeRoom Mode = "room"
)
// ParseMode mirrors Rust ws_hub::WsMode::parse.
@@ -26,6 +27,8 @@ func ParseMode(v any) Mode {
switch strings.TrimSpace(strings.ToLower(t)) {
case "broadcast", "广播":
return ModeBroadcast
case "room", "房间":
return ModeRoom
case "drain", "false", "0", "no", "drain_only":
return ModeDrain
default:
+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
}
+36
View File
@@ -68,6 +68,8 @@ func TestParseModeVariants(t *testing.T) {
{"broadcast", ws.ModeBroadcast},
{"广播", ws.ModeBroadcast},
{"drain", ws.ModeDrain},
{"room", ws.ModeRoom},
{"房间", ws.ModeRoom},
{"nope", ws.ModeEcho},
}
for _, c := range cases {
@@ -77,6 +79,40 @@ func TestParseModeVariants(t *testing.T) {
}
}
func TestRouteWSRoomModeWithKey(t *testing.T) {
a := app.New(nil)
out, err := ws.RouteWS(a, "/chat", map[string]any{
"mode": "room",
"room_key": "chat.room.{id}",
})
if err != nil {
t.Fatal(err)
}
spec, err := ws.RouteSpecOf(out, "/chat")
if err != nil {
t.Fatal(err)
}
if spec.Mode != ws.ModeRoom {
t.Fatalf("mode=%q want room", spec.Mode)
}
if spec.RoomKey != "chat.room.{id}" {
t.Fatalf("room_key=%q", spec.RoomKey)
}
routes := out["ws_routes"].(map[string]any)
room := routes["/chat"].(map[string]any)
if room["mode"] != "room" || room["room_key"] != "chat.room.{id}" {
t.Fatalf("bag=%v", room)
}
}
func TestRouteWSRoomRequiresKey(t *testing.T) {
a := app.New(nil)
_, err := ws.RouteWS(a, "/chat", map[string]any{"mode": "room"})
if err == nil {
t.Fatal("expected error without room_key")
}
}
func TestConnectRefusesBadURL(t *testing.T) {
out := ws.Connect("http://127.0.0.1:1", "hi", nil, 1)
if out["ok"] == true {