-
-
Notifications
You must be signed in to change notification settings - Fork 554
/
websocket.go
735 lines (711 loc) Β· 25.2 KB
/
websocket.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
package codegen
import (
"fmt"
"path/filepath"
"strings"
"goa.design/goa/v3/codegen"
"goa.design/goa/v3/expr"
)
type (
// WebSocketData contains the data needed to render struct type that
// implements the server and client stream interfaces.
WebSocketData struct {
// VarName is the name of the struct.
VarName string
// Type is type of the stream (server or client).
Type string
// Interface is the fully qualified name of the interface that
// the struct implements.
Interface string
// Endpoint is endpoint data that defines streaming
// payload/result.
Endpoint *EndpointData
// Payload is the streaming payload type sent via the stream.
Payload *TypeData
// Response is the successful response data for the streaming
// endpoint.
Response *ResponseData
// SendName is the name of the send function.
SendName string
// SendDesc is the description for the send function.
SendDesc string
// SendTypeName is the fully qualified type name sent through
// the stream.
SendTypeName string
// SendTypeRef is the fully qualified type ref sent through the
// stream.
SendTypeRef string
// RecvName is the name of the receive function.
RecvName string
// RecvDesc is the description for the recv function.
RecvDesc string
// RecvTypeName is the fully qualified type name received from
// the stream.
RecvTypeName string
// RecvTypeRef is the fully qualified type ref received from the
// stream.
RecvTypeRef string
// RecvTypeIsPointer is true if the type received from the stream is a
// array or map. This is needed so that the code reading the stream can
// use a pointer reference when needed to check whether anything was
// read (check against the nil value) and in this case return EOF.
RecvTypeIsPointer bool
// MustClose indicates whether to generate the Close() function
// for the stream.
MustClose bool
// PkgName is the service package name.
PkgName string
// Kind is the kind of the stream (payload, result or
// bidirectional).
Kind expr.StreamKind
}
)
// initWebSocketData initializes the WebSocket related data in ed.
func initWebSocketData(ed *EndpointData, e *expr.HTTPEndpointExpr, sd *ServiceData) {
var (
svrSendTypeName string
svrSendTypeRef string
svrRecvTypeName string
svrRecvTypeRef string
svrSendDesc string
svrRecvDesc string
svrPayload *TypeData
cliSendDesc string
cliRecvDesc string
cliPayload *TypeData
md = ed.Method
svc = sd.Service
svcctx = serviceContext(sd.Service.PkgName, sd.Service.Scope)
)
{
svrSendTypeName = ed.Result.Name
svrSendTypeRef = ed.Result.Ref
svrSendDesc = fmt.Sprintf("%s streams instances of %q to the %q endpoint websocket connection.", md.ServerStream.SendName, svrSendTypeName, md.Name)
cliRecvDesc = fmt.Sprintf("%s reads instances of %q from the %q endpoint websocket connection.", md.ClientStream.RecvName, svrSendTypeName, md.Name)
if e.MethodExpr.Stream == expr.ClientStreamKind || e.MethodExpr.Stream == expr.BidirectionalStreamKind {
svrRecvTypeName = sd.Scope.GoFullTypeName(e.MethodExpr.StreamingPayload, svc.PkgName)
svrRecvTypeRef = sd.Scope.GoFullTypeRef(e.MethodExpr.StreamingPayload, svc.PkgName)
svrPayload = buildRequestBodyType(e.StreamingBody, e.MethodExpr.StreamingPayload, e, true, sd)
if needInit(e.MethodExpr.StreamingPayload.Type) {
makeHTTPType(e.StreamingBody)
body := e.StreamingBody.Type
// generate constructor function to transform request body,
// into the method streaming payload type
var (
name string
desc string
serverArgs []*InitArgData
serverCode string
err error
)
{
n := codegen.Goify(e.MethodExpr.Name, true)
p := codegen.Goify(svrPayload.Name, true)
// Raw payload object has type name prefixed with endpoint name. No need to
// prefix the type name again.
if strings.HasPrefix(p, n) {
name = fmt.Sprintf("New%s", p)
} else {
name = fmt.Sprintf("New%s%s", n, p)
}
desc = fmt.Sprintf("%s builds a %s service %s endpoint payload.", name, svc.Name, e.MethodExpr.Name)
if body != expr.Empty {
var (
ref string
svcode string
)
{
ref = "body"
if expr.IsObject(body) {
ref = "&body"
}
if ut, ok := body.(expr.UserType); ok {
if val := ut.Attribute().Validation; val != nil {
httpctx := httpContext("", sd.Scope, true, true)
svcode = codegen.RecursiveValidationCode(ut.Attribute(), httpctx, true, expr.IsAlias(ut), "body")
}
}
}
serverArgs = []*InitArgData{{
Ref: ref,
AttributeData: &AttributeData{
VarName: "body",
TypeName: sd.Scope.GoTypeName(e.StreamingBody),
TypeRef: sd.Scope.GoTypeRef(e.StreamingBody),
Type: e.StreamingBody.Type,
Required: true,
Example: e.Body.Example(expr.Root.API.Random()),
Validate: svcode,
},
}}
}
if body != expr.Empty {
var helpers []*codegen.TransformFunctionData
httpctx := httpContext("", sd.Scope, true, true)
serverCode, helpers, err = marshal(e.StreamingBody, e.MethodExpr.StreamingPayload, "body", "v", httpctx, svcctx)
if err == nil {
sd.ServerTransformHelpers = codegen.AppendHelpers(sd.ServerTransformHelpers, helpers)
}
}
if err != nil {
fmt.Println(err.Error()) // TBD validate DSL so errors are not possible
}
}
svrPayload.Init = &InitData{
Name: name,
Description: desc,
ServerArgs: serverArgs,
ReturnTypeName: svc.Scope.GoFullTypeName(e.MethodExpr.StreamingPayload, svc.PkgName),
ReturnTypeRef: svc.Scope.GoFullTypeRef(e.MethodExpr.StreamingPayload, svc.PkgName),
ReturnIsStruct: expr.IsObject(e.MethodExpr.StreamingPayload.Type),
ReturnTypePkg: svc.PkgName,
ServerCode: serverCode,
}
}
cliPayload = buildRequestBodyType(e.StreamingBody, e.MethodExpr.StreamingPayload, e, false, sd)
if cliPayload != nil {
sd.ClientTypeNames[cliPayload.Name] = false
sd.ServerTypeNames[cliPayload.Name] = false
}
if e.MethodExpr.Stream == expr.ClientStreamKind {
svrSendDesc = fmt.Sprintf("%s streams instances of %q to the %q endpoint websocket connection and closes the connection.", md.ServerStream.SendName, svrSendTypeName, md.Name)
cliRecvDesc = fmt.Sprintf("%s stops sending messages to the %q endpoint websocket connection and reads instances of %q from the connection.", md.ClientStream.RecvName, md.Name, svrSendTypeName)
}
svrRecvDesc = fmt.Sprintf("%s reads instances of %q from the %q endpoint websocket connection.", md.ServerStream.RecvName, svrRecvTypeName, md.Name)
cliSendDesc = fmt.Sprintf("%s streams instances of %q to the %q endpoint websocket connection.", md.ClientStream.SendName, svrRecvTypeName, md.Name)
}
}
ed.ServerWebSocket = &WebSocketData{
VarName: md.ServerStream.VarName,
Interface: fmt.Sprintf("%s.%s", svc.PkgName, md.ServerStream.Interface),
Endpoint: ed,
Payload: svrPayload,
Response: ed.Result.Responses[0],
PkgName: svc.PkgName,
Type: "server",
Kind: md.ServerStream.Kind,
SendName: md.ServerStream.SendName,
SendDesc: svrSendDesc,
SendTypeName: svrSendTypeName,
SendTypeRef: svrSendTypeRef,
RecvName: md.ServerStream.RecvName,
RecvDesc: svrRecvDesc,
RecvTypeName: svrRecvTypeName,
RecvTypeRef: svrRecvTypeRef,
RecvTypeIsPointer: expr.IsArray(e.MethodExpr.StreamingPayload.Type) || expr.IsMap(e.MethodExpr.StreamingPayload.Type),
MustClose: md.ServerStream.MustClose,
}
ed.ClientWebSocket = &WebSocketData{
VarName: md.ClientStream.VarName,
Interface: fmt.Sprintf("%s.%s", svc.PkgName, md.ClientStream.Interface),
Endpoint: ed,
Payload: cliPayload,
Response: ed.Result.Responses[0],
PkgName: svc.PkgName,
Type: "client",
Kind: md.ClientStream.Kind,
SendName: md.ClientStream.SendName,
SendDesc: cliSendDesc,
SendTypeName: svrRecvTypeName,
SendTypeRef: svrRecvTypeRef,
RecvName: md.ClientStream.RecvName,
RecvDesc: cliRecvDesc,
RecvTypeName: svrSendTypeName,
RecvTypeRef: svrSendTypeRef,
MustClose: md.ClientStream.MustClose,
}
}
// websocketServerFile returns the file implementing the WebSocket server
// streaming implementation if any.
func websocketServerFile(genpkg string, svc *expr.HTTPServiceExpr) *codegen.File {
data := HTTPServices.Get(svc.Name())
if !hasWebSocket(data) {
return nil
}
svcName := data.Service.PathName
title := fmt.Sprintf("%s WebSocket server streaming", svc.Name())
sections := []*codegen.SectionTemplate{
codegen.Header(title, "server", []*codegen.ImportSpec{
{Path: "context"},
{Path: "io"},
{Path: "net/http"},
{Path: "sync"},
{Path: "time"},
{Path: "github.com/gorilla/websocket"},
codegen.GoaImport(""),
codegen.GoaNamedImport("http", "goahttp"),
{Path: genpkg + "/" + svcName, Name: data.Service.PkgName},
}),
}
sections = append(sections, serverStructWSSections(data)...)
sections = append(sections, serverWSSections(data)...)
return &codegen.File{
Path: filepath.Join(codegen.Gendir, "http", svcName, "server", "websocket.go"),
SectionTemplates: sections,
}
}
// websocketClientFile returns the file implementing the WebSocket client
// streaming implementation if any.
func websocketClientFile(genpkg string, svc *expr.HTTPServiceExpr) *codegen.File {
data := HTTPServices.Get(svc.Name())
if !hasWebSocket(data) {
return nil
}
svcName := data.Service.PathName
title := fmt.Sprintf("%s WebSocket client streaming", svc.Name())
sections := []*codegen.SectionTemplate{
codegen.Header(title, "client", []*codegen.ImportSpec{
{Path: "context"},
{Path: "io"},
{Path: "net/http"},
{Path: "sync"},
{Path: "time"},
{Path: "github.com/gorilla/websocket"},
codegen.GoaImport(""),
codegen.GoaNamedImport("http", "goahttp"),
{Path: genpkg + "/" + svcName + "/" + "views", Name: data.Service.ViewsPkg},
{Path: genpkg + "/" + svcName, Name: data.Service.PkgName},
}),
}
sections = append(sections, clientStructWSSections(data)...)
sections = append(sections, clientWSSections(data)...)
return &codegen.File{
Path: filepath.Join(codegen.Gendir, "http", svcName, "client", "websocket.go"),
SectionTemplates: sections,
}
}
// serverStructWSSections return section templates that generate WebSocket
// related struct type definitions for the server.
func serverStructWSSections(data *ServiceData) []*codegen.SectionTemplate {
var sections []*codegen.SectionTemplate
sections = append(sections, &codegen.SectionTemplate{
Name: "server-websocket-conn-configurer-struct",
Source: webSocketConnConfigurerStructT,
Data: data,
FuncMap: map[string]interface{}{"isWebSocketEndpoint": isWebSocketEndpoint},
})
for _, e := range data.Endpoints {
if e.ServerWebSocket != nil {
sections = append(sections, &codegen.SectionTemplate{
Name: "server-websocket-struct-type",
Source: webSocketStructTypeT,
Data: e.ServerWebSocket,
})
}
}
return sections
}
// serverWSSections returns section templates that contain server WebSocket
// specific code for the given service.
func serverWSSections(data *ServiceData) []*codegen.SectionTemplate {
var sections []*codegen.SectionTemplate
sections = append(sections, &codegen.SectionTemplate{
Name: "server-websocket-conn-configurer-struct-init",
Source: webSocketConnConfigurerStructInitT,
Data: data,
FuncMap: map[string]interface{}{"isWebSocketEndpoint": isWebSocketEndpoint},
})
for _, e := range data.Endpoints {
if e.ServerWebSocket != nil {
if e.ServerWebSocket.SendTypeRef != "" {
sections = append(sections, &codegen.SectionTemplate{
Name: "server-websocket-send",
Source: webSocketSendT,
Data: e.ServerWebSocket,
FuncMap: map[string]interface{}{
"upgradeParams": upgradeParams,
"viewedServerBody": viewedServerBody,
},
})
}
switch e.ServerWebSocket.Kind {
case expr.ClientStreamKind, expr.BidirectionalStreamKind:
sections = append(sections, &codegen.SectionTemplate{
Name: "server-websocket-recv",
Source: webSocketRecvT,
Data: e.ServerWebSocket,
FuncMap: map[string]interface{}{"upgradeParams": upgradeParams},
})
}
if e.ServerWebSocket.MustClose {
sections = append(sections, &codegen.SectionTemplate{
Name: "server-websocket-close",
Source: webSocketCloseT,
Data: e.ServerWebSocket,
FuncMap: map[string]interface{}{"upgradeParams": upgradeParams},
})
}
if e.Method.ViewedResult != nil && e.Method.ViewedResult.ViewName == "" {
sections = append(sections, &codegen.SectionTemplate{
Name: "server-websocket-set-view",
Source: webSocketSetViewT,
Data: e.ServerWebSocket,
})
}
}
}
return sections
}
// clientStructWSSections return section templates that generate WebSocket
// related struct type definitions for the client.
func clientStructWSSections(data *ServiceData) []*codegen.SectionTemplate {
var sections []*codegen.SectionTemplate
sections = append(sections, &codegen.SectionTemplate{
Name: "client-websocket-conn-configurer-struct",
Source: webSocketConnConfigurerStructT,
Data: data,
FuncMap: map[string]interface{}{"isWebSocketEndpoint": isWebSocketEndpoint},
})
for _, e := range data.Endpoints {
if e.ClientWebSocket != nil {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-websocket-struct-type",
Source: webSocketStructTypeT,
Data: e.ClientWebSocket,
})
}
}
return sections
}
// clientWSSections returns section templates that contain client WebSocket
// specific code for the given service.
func clientWSSections(data *ServiceData) []*codegen.SectionTemplate {
var sections []*codegen.SectionTemplate
sections = append(sections, &codegen.SectionTemplate{
Name: "client-websocket-conn-configurer-struct-init",
Source: webSocketConnConfigurerStructInitT,
Data: data,
FuncMap: map[string]interface{}{"isWebSocketEndpoint": isWebSocketEndpoint},
})
for _, e := range data.Endpoints {
if e.ClientWebSocket != nil {
if e.ClientWebSocket.RecvTypeRef != "" {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-websocket-recv",
Source: webSocketRecvT,
Data: e.ClientWebSocket,
FuncMap: map[string]interface{}{"upgradeParams": upgradeParams},
})
}
switch e.ClientWebSocket.Kind {
case expr.ClientStreamKind, expr.BidirectionalStreamKind:
sections = append(sections, &codegen.SectionTemplate{
Name: "client-websocket-send",
Source: webSocketSendT,
Data: e.ClientWebSocket,
FuncMap: map[string]interface{}{
"upgradeParams": upgradeParams,
"viewedServerBody": viewedServerBody,
},
})
}
if e.ClientWebSocket.MustClose {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-websocket-close",
Source: webSocketCloseT,
Data: e.ClientWebSocket,
FuncMap: map[string]interface{}{"upgradeParams": upgradeParams},
})
}
if e.Method.ViewedResult != nil && e.Method.ViewedResult.ViewName == "" {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-websocket-set-view",
Source: webSocketSetViewT,
Data: e.ClientWebSocket,
})
}
}
}
return sections
}
// hasWebSocket returns true if at least one of the endpoints in the service
// defines a streaming payload or result.
func hasWebSocket(sd *ServiceData) bool {
for _, e := range sd.Endpoints {
if isWebSocketEndpoint(e) {
return true
}
}
return false
}
// isWebSocketEndpoint returns true if the endpoint defines a streaming payload
// or result.
func isWebSocketEndpoint(ed *EndpointData) bool {
return ed.ServerWebSocket != nil || ed.ClientWebSocket != nil
}
const (
// webSocketStructTypeT renders the server and client struct types that
// implements the client and server stream interfaces. The data to render
// input: WebSocketData
webSocketStructTypeT = `{{ printf "%s implements the %s interface." .VarName .Interface | comment }}
type {{ .VarName }} struct {
{{- if eq .Type "server" }}
once sync.Once
{{ comment "upgrader is the websocket connection upgrader." }}
upgrader goahttp.Upgrader
{{ comment "configurer is the websocket connection configurer." }}
configurer goahttp.ConnConfigureFunc
{{ comment "cancel is the context cancellation function which cancels the request context when invoked." }}
cancel context.CancelFunc
{{ comment "w is the HTTP response writer used in upgrading the connection." }}
w http.ResponseWriter
{{ comment "r is the HTTP request." }}
r *http.Request
{{- end }}
{{ comment "conn is the underlying websocket connection." }}
conn *websocket.Conn
{{- if .Endpoint.Method.ViewedResult }}
{{- if not .Endpoint.Method.ViewedResult.ViewName }}
{{ printf "view is the view to render %s result type before sending to the websocket connection." .SendTypeName | comment }}
view string
{{- end }}
{{- end }}
}
`
// webSocketConnConfigurerStructT generates the struct type that holds the
// websocket connection configurers for all the streaming endpoints in the
// service.
// input: ServiceData
webSocketConnConfigurerStructT = `{{ printf "ConnConfigurer holds the websocket connection configurer functions for the streaming endpoints in %q service." .Service.Name | comment }}
type ConnConfigurer struct {
{{- range .Endpoints }}
{{- if isWebSocketEndpoint . }}
{{ .Method.VarName }}Fn goahttp.ConnConfigureFunc
{{- end }}
{{- end }}
}
`
// webSocketConnConfigurerStructInitT generates the constructor function to
// initialize the websocket connection configurer struct.
// input: ServiceData
webSocketConnConfigurerStructInitT = `{{ printf "NewConnConfigurer initializes the websocket connection configurer function with fn for all the streaming endpoints in %q service." .Service.Name | comment }}
func NewConnConfigurer(fn goahttp.ConnConfigureFunc) *ConnConfigurer {
return &ConnConfigurer{
{{- range .Endpoints }}
{{- if isWebSocketEndpoint . }}
{{ .Method.VarName}}Fn: fn,
{{- end }}
{{- end }}
}
}
`
// webSocketSendT renders the function implementing the Send method in
// stream interface.
// input: WebSocketData
webSocketSendT = `{{ comment .SendDesc }}
func (s *{{ .VarName }}) {{ .SendName }}(v {{ .SendTypeRef }}) error {
{{- if eq .Type "server" }}
{{- if eq .SendName "Send" }}
var err error
{{- template "websocket_upgrade" (upgradeParams .Endpoint .SendName) }}
{{- else }} {{/* SendAndClose */}}
defer s.conn.Close()
{{- end }}
{{- if .Endpoint.Method.ViewedResult }}
{{- if .Endpoint.Method.ViewedResult.ViewName }}
res := {{ .PkgName }}.{{ .Endpoint.Method.ViewedResult.Init.Name }}(v, {{ printf "%q" .Endpoint.Method.ViewedResult.ViewName }})
{{- else }}
res := {{ .PkgName }}.{{ .Endpoint.Method.ViewedResult.Init.Name }}(v, s.view)
{{- end }}
{{- else }}
res := v
{{- end }}
{{- $servBodyLen := len .Response.ServerBody }}
{{- if gt $servBodyLen 0 }}
{{- if (index .Response.ServerBody 0).Init }}
{{- if .Endpoint.Method.ViewedResult }}
{{- if .Endpoint.Method.ViewedResult.ViewName }}
{{- $vsb := (viewedServerBody $.Response.ServerBody .Endpoint.Method.ViewedResult.ViewName) }}
body := {{ $vsb.Init.Name }}({{ range $vsb.Init.ServerArgs }}{{ .Ref }}, {{ end }})
{{- else }}
var body interface{}
switch s.view {
{{- range .Endpoint.Method.ViewedResult.Views }}
case {{ printf "%q" .Name }}{{ if eq .Name "default" }}, ""{{ end }}:
{{- $vsb := (viewedServerBody $.Response.ServerBody .Name) }}
body = {{ $vsb.Init.Name }}({{ range $vsb.Init.ServerArgs }}{{ .Ref }}, {{ end }})
{{- end }}
}
{{- end }}
{{- else }}
body := {{ (index .Response.ServerBody 0).Init.Name }}({{ range (index .Response.ServerBody 0).Init.ServerArgs }}{{ .Ref }}, {{ end }})
{{- end }}
return s.conn.WriteJSON(body)
{{- else }}
return s.conn.WriteJSON(res)
{{- end }}
{{- else }}
return s.conn.WriteJSON(res)
{{- end }}
{{- else }}
{{- if .Payload.Init }}
body := {{ .Payload.Init.Name }}(v)
return s.conn.WriteJSON(body)
{{- else }}
return s.conn.WriteJSON(v)
{{- end }}
{{- end }}
}
` + upgradeT
// webSocketRecvT renders the function implementing the Recv method in
// stream interface.
// input: WebSocketData
webSocketRecvT = `{{ comment .RecvDesc }}
func (s *{{ .VarName }}) {{ .RecvName }}() ({{ .RecvTypeRef }}, error) {
var (
rv {{ .RecvTypeRef }}
{{- if eq .Type "server" }}
{{- if .RecvTypeIsPointer }}
body {{ .Payload.VarName }}
{{- else }}
msg *{{ .Payload.VarName }}
{{- end }}
{{- else }}
body {{ .Response.ClientBody.VarName }}
{{- end }}
err error
)
{{- if eq .Type "server" }}
{{- template "websocket_upgrade" (upgradeParams .Endpoint .RecvName) }}
{{- if .RecvTypeIsPointer }}
if err = s.conn.ReadJSON(&body); err != nil {
{{- else }}
if err = s.conn.ReadJSON(&msg); err != nil {
{{- end }}
return rv, err
}
{{- if .RecvTypeIsPointer }}
if body == nil {
{{- else }}
if msg == nil {
{{- end }}
return rv, io.EOF
}
{{- if .Payload.ValidateRef }}
{{- if not .RecvTypeIsPointer }}
body := *msg
{{- end }}
{{ .Payload.ValidateRef }}
if err != nil {
return rv, err
}
{{- end }}
{{- if .Payload.Init }}
return {{ .Payload.Init.Name }}({{ if .RecvTypeIsPointer }}body{{ else }}msg{{ end }}), nil
{{- else }}
return {{ if .RecvTypeIsPointer }}body{{ else }}*msg{{ end }}, nil
{{- end }}
{{- else }} {{/* client side code */}}
{{- if eq .RecvName "CloseAndRecv" }}
defer s.conn.Close()
{{ comment "Send a nil payload to the server implying end of message" }}
if err = s.conn.WriteJSON(nil); err != nil {
return rv, err
}
{{- end }}
err = s.conn.ReadJSON(&body)
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
{{- if not .MustClose }}
s.conn.Close()
{{- end }}
return rv, io.EOF
}
if err != nil {
return rv, err
}
{{- if and .Response.ClientBody.ValidateRef (not .Endpoint.Method.ViewedResult) }}
{{ .Response.ClientBody.ValidateRef }}
if err != nil {
return rv, err
}
{{- end }}
{{- if .Response.ResultInit }}
res := {{ .Response.ResultInit.Name }}({{ range .Response.ResultInit.ClientArgs }}{{ .Ref }},{{ end }})
{{- if .Endpoint.Method.ViewedResult }}{{ with .Endpoint.Method.ViewedResult }}
vres := {{ if not .IsCollection }}&{{ end }}{{ .ViewsPkg }}.{{ .VarName }}{res, {{ if .ViewName }}{{ printf "%q" .ViewName }}{{ else }}s.view{{ end }} }
if err := {{ .ViewsPkg }}.Validate{{ $.Endpoint.Method.Result }}(vres); err != nil {
return rv, goahttp.ErrValidationError("{{ $.Endpoint.ServiceName }}", "{{ $.Endpoint.Method.Name }}", err)
}
return {{ $.PkgName }}.{{ .ResultInit.Name }}(vres){{ end }}, nil
{{- else }}
return res, nil
{{- end }}
{{- else }}
return body, nil
{{- end }}
{{- end }}
}
` + upgradeT
// upgradeT renders the code to upgrade the HTTP connection to a gorilla
// websocket connection.
upgradeT = `{{- define "websocket_upgrade" }}
{{ printf "Upgrade the HTTP connection to a websocket connection only once. Connection upgrade is done here so that authorization logic in the endpoint is executed before calling the actual service method which may call %s()." .Function | comment }}
s.once.Do(func() {
{{- if and .ViewedResult (eq .Function "Send") }}
{{- if not .ViewedResult.ViewName }}
respHdr := make(http.Header)
respHdr.Add("goa-view", s.view)
{{- end }}
{{- end }}
var conn *websocket.Conn
{{- if eq .Function "Send" }}
{{- if .ViewedResult }}
{{- if not .ViewedResult.ViewName }}
conn, err = s.upgrader.Upgrade(s.w, s.r, respHdr)
{{- else }}
conn, err = s.upgrader.Upgrade(s.w, s.r, nil)
{{- end }}
{{- else }}
conn, err = s.upgrader.Upgrade(s.w, s.r, nil)
{{- end }}
{{- else }}
conn, err = s.upgrader.Upgrade(s.w, s.r, nil)
{{- end }}
if err != nil {
return
}
if s.configurer != nil {
conn = s.configurer(conn, s.cancel)
}
s.conn = conn
})
if err != nil {
return {{ if eq .Function "Recv" }}rv, {{ end }}err
}
{{- end }}
`
// webSocketCloseT renders the function implementing the Close method in
// stream interface.
// input: WebSocketData
webSocketCloseT = `{{ printf "Close closes the %q endpoint websocket connection." .Endpoint.Method.Name | comment }}
func (s *{{ .VarName }}) Close() error {
var err error
{{- if eq .Type "server" }}
if s.conn == nil {
return nil
}
if err = s.conn.WriteControl(
websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, "server closing connection"),
time.Now().Add(time.Second),
); err != nil {
return err
}
{{- else }} {{/* client side code */}}
{{ comment "Send a nil payload to the server implying client closing connection." }}
if err = s.conn.WriteJSON(nil); err != nil {
return err
}
{{- end }}
return s.conn.Close()
}
` + upgradeT
// webSocketSetViewT renders the function implementing the SetView method in
// server stream interface.
// input: WebSocketData
webSocketSetViewT = `{{ printf "SetView sets the view to render the %s type before sending to the %q endpoint websocket connection." .SendTypeName .Endpoint.Method.Name | comment }}
func (s *{{ .VarName }}) SetView(view string) {
s.view = view
}
`
)