/
channel.go
120 lines (94 loc) · 2.78 KB
/
channel.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package evil
import (
"encoding/json"
"github.com/chrisseto/evil/channel"
"github.com/cockroachdb/errors"
"github.com/dgrijalva/jwt-go"
"github.com/olahol/melody"
)
type LiveViewChannel struct {
Views map[string]View
Secret []byte
Sessions map[string]*session
broadcast func(string, string, interface{}) error
}
var _ channel.Channel = &LiveViewChannel{}
func (c *LiveViewChannel) SpawnInstance(id string, view View) (*session, error) {
if session, ok := c.Sessions[id]; ok {
return session, nil
}
session := newSession(id, view, func(event string, data interface{}) error {
// not the best way to handle this
return c.broadcast(id, event, data)
})
c.Sessions[id] = session
return session, errors.Wrap(
session.start(),
"starting session",
)
}
func (c *LiveViewChannel) RegisterView(view View) {
// TODO error if already exists
c.Views[viewName(view)] = view
}
func (c *LiveViewChannel) verifySession(signed string) (*SessionClaims, error) {
token, err := jwt.ParseWithClaims(signed, &SessionClaims{}, func(t *jwt.Token) (interface{}, error) {
_ = t.Method.(*jwt.SigningMethodHMAC)
return c.Secret, nil
})
if err != nil {
return nil, err
}
claims := token.Claims.(*SessionClaims)
if err := token.Claims.Valid(); err != nil {
return nil, err
}
return claims, nil
}
func (c *LiveViewChannel) Join(session *melody.Session, m *channel.Message) (interface{}, error) {
// TODO move join in evil
var j channel.Join
if err := json.Unmarshal(m.Payload, &j); err != nil {
return nil, errors.Wrap(err, "unmarshaling into .Join")
}
claims, err := c.verifySession(j.Session)
if err != nil {
return nil, errors.Wrap(err, "verifying session")
}
view := c.Views[claims.View]
instance, err := c.SpawnInstance(claims.ID, view)
if err != nil {
return nil, errors.Wrap(err, "spawning instance")
}
session.Set("id", claims.ID)
session.Set("view", claims.View)
diff, err := instance.RenderDiff()
// TODO make me a struct
return map[string]interface{}{
"rendered": diff,
}, errors.Wrap(err, "executing template")
}
func (c *LiveViewChannel) Handle(session *melody.Session, m *channel.Message) (interface{}, error) {
// TODO move event in evil
var e channel.Event
if err := json.Unmarshal(m.Payload, &e); err != nil {
return nil, errors.Wrap(err, "unmarshaling into an event")
}
instance, ok := c.Sessions[session.MustGet("id").(string)]
if !ok {
panic("no session")
}
if err := instance.RootView.HandleEvent(instance, &e); err != nil {
return nil, errors.Wrap(err, "handling event")
}
diff, err := instance.RenderDiff()
// TODO make me a struct
// TODO exclude statics
return map[string]interface{}{
"diff": diff,
}, errors.Wrap(err, "executing template")
}
func (c *LiveViewChannel) Leave(session *melody.Session) error {
// TODO
return nil
}