-
Notifications
You must be signed in to change notification settings - Fork 4
/
socket.go
87 lines (82 loc) · 2.84 KB
/
socket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Package clib - Content managed by Project Forge, see [projectforge.md] for details.
package clib
import (
"fmt"
"net/http"
"strings"
"github.com/pkg/errors"
"projectforge.dev/projectforge/app"
"projectforge.dev/projectforge/app/controller"
"projectforge.dev/projectforge/app/controller/cutil"
"projectforge.dev/projectforge/app/lib/websocket"
"projectforge.dev/projectforge/app/util"
"projectforge.dev/projectforge/views/vadmin"
)
const socketIcon = "eye"
func socketRoute(w http.ResponseWriter, r *http.Request, as *app.State, ps *cutil.PageState, path ...string) (string, error) {
bc := func(extra ...string) []string {
return append([]string{"admin", "Sockets||/admin/sockets"}, extra...)
}
if len(path) == 0 {
chans, conns, taps := as.Services.Socket.Status()
ps.SetTitleAndData("Sockets", util.ValueMap{"channels": chans, "connections": conns})
return controller.Render(r, as, &vadmin.Sockets{Channels: chans, Connections: conns, Taps: taps}, ps, bc()...)
}
switch path[0] {
case "tap":
ps.SetTitleAndData("WebSocket Tap", "tap")
ps.DefaultNavIcon = "search"
return controller.Render(r, as, &vadmin.SocketTap{}, ps, bc("Tap")...)
case "tap-socket":
_, err := as.Services.Socket.RegisterTap(w, r, ps.Logger)
if err != nil {
return "", errors.Wrap(err, "unable to register tap socket")
}
return "", nil
case "chan":
if len(path) == 0 {
return "", errors.New("no channel in path")
}
ch := as.Services.Socket.GetChannel(path[1])
members := as.Services.Socket.GetAllMembers(path[1])
ps.Data = ch
ps.DefaultNavIcon = socketIcon
return controller.Render(r, as, &vadmin.Channel{Channel: ch, Members: members}, ps, bc("Channel", ch.Key)...)
case "conn":
if len(path) == 0 {
return "", errors.New("no connection ID in path")
}
id := util.UUIDFromString(path[1])
if id == nil {
return "", errors.Errorf("invalid connection ID [%s] in path", path[1])
}
if len(path) == 2 {
c := as.Services.Socket.GetConnection(*id)
if c == nil {
return "", errors.Errorf("no connection with ID [%s]", id.String())
}
ps.Data = c
ps.DefaultNavIcon = socketIcon
return controller.Render(r, as, &vadmin.Connection{Connection: c}, ps, bc("Connection", c.ID.String())...)
}
frm, _ := cutil.ParseForm(r, ps.RequestBody)
fromStr := frm.GetStringOpt("from")
from := util.UUIDFromString(fromStr)
channel := frm.GetStringOpt("channel")
cmd := frm.GetStringOpt("cmd")
param := frm.GetStringOpt("param")
m := &websocket.Message{
From: from,
Channel: channel,
Cmd: cmd,
Param: []byte(param),
}
err := as.Services.Socket.WriteMessage(*id, m, ps.Logger)
if err != nil {
return "", err
}
return controller.FlashAndRedir(true, "sent message", fmt.Sprintf("/admin/sockets/conn/%s", id.String()), ps)
default:
return "", errors.Errorf("invalid path [%s]", strings.Join(path, "/"))
}
}