-
Notifications
You must be signed in to change notification settings - Fork 175
/
handler.go
181 lines (161 loc) · 3.97 KB
/
handler.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package grpc
import (
"context"
"io"
"github.com/micro/go-micro/errors"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/views"
)
type TreeHandler struct {
router views.Handler
}
func (t *TreeHandler) ReadNodeStream(ctx context.Context, s tree.NodeProviderStreamer_ReadNodeStreamStream) error {
router := t.getRouter()
var err error
for {
r, e := s.Recv()
if e != nil {
if e != io.EOF {
s.SendMsg(e)
err = e
}
break
}
resp, _ := router.ReadNode(ctx, r)
if resp == nil {
resp = &tree.ReadNodeResponse{Success: false}
} else {
resp.Success = true
}
sE := s.Send(resp)
if sE != nil {
// Error while sending
break
}
}
s.Close()
return err
}
func (t *TreeHandler) CreateNodeStream(ctx context.Context, s tree.NodeReceiverStream_CreateNodeStreamStream) error {
router := t.getRouter()
var err error
for {
r, e := s.Recv()
if e != nil {
if e != io.EOF {
s.SendMsg(e)
err = e
}
break
}
resp, err := router.CreateNode(ctx, r)
if err != nil {
s.SendMsg(err)
err = e
break
}
if err = s.Send(resp); err != nil {
break
}
}
s.Close()
return err //errors.BadRequest("not.implemented", "CreateNodeStream not implemented yet")
}
func (t *TreeHandler) UpdateNodeStream(context.Context, tree.NodeReceiverStream_UpdateNodeStreamStream) error {
return errors.BadRequest("not.implemented", "UpdateNodeStream not implemented yet")
}
func (t *TreeHandler) DeleteNodeStream(context.Context, tree.NodeReceiverStream_DeleteNodeStreamStream) error {
return errors.BadRequest("not.implemented", "DeleteNodeStream not implemented yet")
}
// ReadNode forwards to router
func (t *TreeHandler) ReadNode(ctx context.Context, request *tree.ReadNodeRequest, response *tree.ReadNodeResponse) error {
r, e := t.getRouter().ReadNode(ctx, request)
if e != nil {
response.Success = false
return e
} else {
response.Node = r.Node
response.Success = r.Success
return nil
}
}
// ListNodes forwards to router
func (t *TreeHandler) ListNodes(ctx context.Context, request *tree.ListNodesRequest, stream tree.NodeProvider_ListNodesStream) error {
st, e := t.getRouter().ListNodes(ctx, request)
if e != nil {
return e
}
defer st.Close()
for {
r, e := st.Recv()
if e == io.EOF || e == io.ErrUnexpectedEOF {
break
}
if e != nil {
return e
}
stream.Send(r)
}
return nil
}
// StreamChanges sends events to the client
func (t *TreeHandler) StreamChanges(ctx context.Context, req *tree.StreamChangesRequest, resp tree.NodeChangesStreamer_StreamChangesStream) error {
streamer, err := t.getRouter().StreamChanges(ctx, req)
if err != nil {
return err
}
defer streamer.Close()
for {
r, e := streamer.Recv()
if e != nil {
break
}
resp.Send(r)
}
return nil
}
// CreateNode is used for creating folders
func (t *TreeHandler) CreateNode(ctx context.Context, req *tree.CreateNodeRequest, resp *tree.CreateNodeResponse) error {
r, e := t.getRouter().CreateNode(ctx, req)
if e != nil {
resp.Success = false
return e
}
resp.Node = r.Node
resp.Success = r.Success
return nil
}
// UpdateNode is used for moving nodes paths
func (t *TreeHandler) UpdateNode(ctx context.Context, req *tree.UpdateNodeRequest, resp *tree.UpdateNodeResponse) error {
r, e := t.getRouter().UpdateNode(ctx, req)
if e != nil {
resp.Success = false
return e
}
resp.Success = r.Success
resp.Node = r.Node
return nil
}
// DeleteNode is used to delete nodes
func (t *TreeHandler) DeleteNode(ctx context.Context, req *tree.DeleteNodeRequest, resp *tree.DeleteNodeResponse) error {
r, e := t.getRouter().DeleteNode(ctx, req)
if e != nil {
resp.Success = false
return e
}
resp.Success = r.Success
return nil
}
func (t *TreeHandler) getRouter() views.Handler {
if t.router != nil {
return t.router
}
t.router = views.NewStandardRouter(views.RouterOptions{
AdminView: false,
WatchRegistry: true,
LogReadEvents: false,
AuditEvent: false,
SynchronousTasks: true,
})
return t.router
}