-
-
Notifications
You must be signed in to change notification settings - Fork 559
/
endpoint.go
387 lines (366 loc) Β· 12.5 KB
/
endpoint.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
package service
import (
"fmt"
"path/filepath"
"strings"
"goa.design/goa/v3/codegen"
"goa.design/goa/v3/expr"
)
type (
// EndpointsData contains the data necessary to render the
// service endpoints struct template.
EndpointsData struct {
// Name is the service name.
Name string
// Description is the service description.
Description string
// VarName is the endpoint struct name.
VarName string
// ClientVarName is the client struct name.
ClientVarName string
// ServiceVarName is the service interface name.
ServiceVarName string
// Methods lists the endpoint struct methods.
Methods []*EndpointMethodData
// ClientInitArgs lists the arguments needed to instantiate the client.
ClientInitArgs string
// Schemes contains the security schemes types used by the
// all the endpoints.
Schemes SchemesData
}
// EndpointMethodData describes a single endpoint method.
EndpointMethodData struct {
*MethodData
// ArgName is the name of the argument used to initialize the client
// struct method field.
ArgName string
// ClientVarName is the corresponding client struct field name.
ClientVarName string
// ServiceName is the name of the owner service.
ServiceName string
// ServiceVarName is the name of the owner service Go interface.
ServiceVarName string
}
)
const (
// endpointsStructName is the name of the generated endpoints data
// structure.
endpointsStructName = "Endpoints"
// serviceInterfaceName is the name of the generated service interface.
serviceInterfaceName = "Service"
)
// EndpointFile returns the endpoint file for the given service.
func EndpointFile(genpkg string, service *expr.ServiceExpr) *codegen.File {
svc := Services.Get(service.Name)
svcName := svc.PathName
path := filepath.Join(codegen.Gendir, svcName, "endpoints.go")
data := endpointData(service)
var (
sections []*codegen.SectionTemplate
)
{
imports := []*codegen.ImportSpec{
{Path: "context"},
{Path: "io"},
{Path: "fmt"},
codegen.GoaImport(""),
codegen.GoaImport("security"),
{Path: genpkg + "/" + svcName + "/" + "views", Name: svc.ViewsPkg},
}
imports = append(imports, svc.UserTypeImports...)
header := codegen.Header(service.Name+" endpoints", svc.PkgName, imports)
def := &codegen.SectionTemplate{
Name: "endpoints-struct",
Source: serviceEndpointsT,
Data: data,
}
sections = []*codegen.SectionTemplate{header, def}
for _, m := range data.Methods {
if m.ServerStream != nil {
sections = append(sections, &codegen.SectionTemplate{
Name: "endpoint-input-struct",
Source: serviceEndpointStreamStructT,
Data: m,
})
}
if m.SkipRequestBodyEncodeDecode {
sections = append(sections, &codegen.SectionTemplate{
Name: "request-body-struct",
Source: serviceRequestBodyStructT,
Data: m,
})
}
if m.SkipResponseBodyEncodeDecode {
sections = append(sections, &codegen.SectionTemplate{
Name: "response-body-struct",
Source: serviceResponseBodyStructT,
Data: m,
})
}
}
sections = append(sections, &codegen.SectionTemplate{
Name: "endpoints-init",
Source: serviceEndpointsInitT,
Data: data,
})
sections = append(sections, &codegen.SectionTemplate{
Name: "endpoints-use",
Source: serviceEndpointsUseT,
Data: data,
})
for _, m := range data.Methods {
sections = append(sections, &codegen.SectionTemplate{
Name: "endpoint-method",
Source: serviceEndpointMethodT,
Data: m,
FuncMap: map[string]any{"payloadVar": payloadVar},
})
}
}
return &codegen.File{Path: path, SectionTemplates: sections}
}
func endpointData(service *expr.ServiceExpr) *EndpointsData {
svc := Services.Get(service.Name)
methods := make([]*EndpointMethodData, len(svc.Methods))
names := make([]string, len(svc.Methods))
for i, m := range svc.Methods {
methods[i] = &EndpointMethodData{
MethodData: m,
ArgName: codegen.Goify(m.VarName, false),
ServiceName: svc.Name,
ServiceVarName: serviceInterfaceName,
ClientVarName: clientStructName,
}
names[i] = codegen.Goify(m.VarName, false)
}
desc := fmt.Sprintf("%s wraps the %q service endpoints.", endpointsStructName, service.Name)
return &EndpointsData{
Name: service.Name,
Description: desc,
VarName: endpointsStructName,
ClientVarName: clientStructName,
ServiceVarName: serviceInterfaceName,
ClientInitArgs: strings.Join(names, ", "),
Methods: methods,
Schemes: svc.Schemes,
}
}
func payloadVar(e *EndpointMethodData) string {
if e.ServerStream != nil || e.SkipRequestBodyEncodeDecode {
return "ep.Payload"
}
return "p"
}
// input: endpointsData
const serviceEndpointsT = `{{ comment .Description }}
type {{ .VarName }} struct {
{{- range .Methods}}
{{ .VarName }} goa.Endpoint
{{- end }}
}
`
// input: endpointsData
const serviceEndpointsInitT = `{{ printf "New%s wraps the methods of the %q service with endpoints." .VarName .Name | comment }}
func New{{ .VarName }}(s {{ .ServiceVarName }}) *{{ .VarName }} {
{{- if .Schemes }}
// Casting service to Auther interface
a := s.(Auther)
{{- end }}
return &{{ .VarName }}{
{{- range .Methods }}
{{ .VarName }}: New{{ .VarName }}Endpoint(s{{ range .Schemes }}, a.{{ .Type }}Auth{{ end }}),
{{- end }}
}
}
`
// input: endpointMethodData
const serviceEndpointStreamStructT = `{{ printf "%s holds both the payload and the server stream of the %q method." .ServerStream.EndpointStruct .Name | comment }}
type {{ .ServerStream.EndpointStruct }} struct {
{{- if .PayloadRef }}
{{ comment "Payload is the method payload." }}
Payload {{ .PayloadRef }}
{{- end }}
{{ printf "Stream is the server stream used by the %q method to send data." .Name | comment }}
Stream {{ .ServerStream.Interface }}
}
`
// input: endpointMethodData
const serviceRequestBodyStructT = `{{ printf "%s holds both the payload and the HTTP request body reader of the %q method." .RequestStruct .Name | comment }}
type {{ .RequestStruct }} struct {
{{- if .PayloadRef }}
{{ comment "Payload is the method payload." }}
Payload {{ .PayloadRef }}
{{- end }}
{{ comment "Body streams the HTTP request body." }}
Body io.ReadCloser
}
`
// input: endpointMethodData
const serviceResponseBodyStructT = `{{ printf "%s holds both the result and the HTTP response body reader of the %q method." .ResponseStruct .Name | comment }}
type {{ .ResponseStruct }} struct {
{{- if .ResultRef }}
{{ comment "Result is the method result." }}
Result {{ .ResultRef }}
{{- end }}
{{ comment "Body streams the HTTP response body." }}
Body io.ReadCloser
}
`
// input: endpointMethodData
const serviceEndpointMethodT = `{{ printf "New%sEndpoint returns an endpoint function that calls the method %q of service %q." .VarName .Name .ServiceName | comment }}
func New{{ .VarName }}Endpoint(s {{ .ServiceVarName }}{{ range .Schemes }}, auth{{ .Type }}Fn security.Auth{{ .Type }}Func{{ end }}) goa.Endpoint {
return func(ctx context.Context, req any) (any, error) {
{{- if or .ServerStream }}
ep := req.(*{{ .ServerStream.EndpointStruct }})
{{- else if .SkipRequestBodyEncodeDecode }}
ep := req.(*{{ .RequestStruct }})
{{- else if .PayloadRef }}
p := req.({{ .PayloadRef }})
{{- end }}
{{- $payload := payloadVar . }}
{{- if .Requirements }}
var err error
{{- range $ridx, $r := .Requirements }}
{{- if ne $ridx 0 }}
if err != nil {
{{- end }}
{{- range $sidx, $s := .Schemes }}
{{- if ne $sidx 0 }}
if err == nil {
{{- end }}
{{- if eq .Type "Basic" }}
sc := security.BasicScheme{
Name: {{ printf "%q" .SchemeName }},
Scopes: []string{ {{- range .Scopes }}{{ printf "%q" . }}, {{ end }} },
RequiredScopes: []string{ {{- range $r.Scopes }}{{ printf "%q" . }}, {{ end }} },
}
{{- if .UsernamePointer }}
var user string
if {{ $payload }}.{{ .UsernameField }} != nil {
user = *{{ $payload }}.{{ .UsernameField }}
}
{{- end }}
{{- if .PasswordPointer }}
var pass string
if {{ $payload }}.{{ .PasswordField }} != nil {
pass = *{{ $payload }}.{{ .PasswordField }}
}
{{- end }}
ctx, err = auth{{ .Type }}Fn(ctx, {{ if .UsernamePointer }}user{{ else }}{{ $payload }}.{{ .UsernameField }}{{ end }},
{{- if .PasswordPointer }}pass{{ else }}{{ $payload }}.{{ .PasswordField }}{{ end }}, &sc)
{{- else if eq .Type "APIKey" }}
sc := security.APIKeyScheme{
Name: {{ printf "%q" .SchemeName }},
Scopes: []string{ {{- range .Scopes }}{{ printf "%q" . }}, {{ end }} },
RequiredScopes: []string{ {{- range $r.Scopes }}{{ printf "%q" . }}, {{ end }} },
}
{{- if $s.CredPointer }}
var key string
if {{ $payload }}.{{ $s.CredField }} != nil {
key = *{{ $payload }}.{{ $s.CredField }}
}
{{- end }}
ctx, err = auth{{ .Type }}Fn(ctx, {{ if $s.CredPointer }}key{{ else }}{{ $payload }}.{{ $s.CredField }}{{ end }}, &sc)
{{- else if eq .Type "JWT" }}
sc := security.JWTScheme{
Name: {{ printf "%q" .SchemeName }},
Scopes: []string{ {{- range .Scopes }}{{ printf "%q" . }}, {{ end }} },
RequiredScopes: []string{ {{- range $r.Scopes }}{{ printf "%q" . }}, {{ end }} },
}
{{- if $s.CredPointer }}
var token string
if {{ $payload }}.{{ $s.CredField }} != nil {
token = *{{ $payload }}.{{ $s.CredField }}
}
{{- end }}
ctx, err = auth{{ .Type }}Fn(ctx, {{ if $s.CredPointer }}token{{ else }}{{ $payload }}.{{ $s.CredField }}{{ end }}, &sc)
{{- else if eq .Type "OAuth2" }}
sc := security.OAuth2Scheme{
Name: {{ printf "%q" .SchemeName }},
Scopes: []string{ {{- range .Scopes }}{{ printf "%q" . }}, {{ end }} },
RequiredScopes: []string{ {{- range $r.Scopes }}{{ printf "%q" . }}, {{ end }} },
{{- if .Flows }}
Flows: []*security.OAuthFlow{
{{- range .Flows }}
&security.OAuthFlow{
Type: "{{ .Type }}",
{{- if .AuthorizationURL }}
AuthorizationURL: {{ printf "%q" .AuthorizationURL }},
{{- end }}
{{- if .TokenURL }}
TokenURL: {{ printf "%q" .TokenURL }},
{{- end }}
{{- if .RefreshURL }}
RefreshURL: {{ printf "%q" .RefreshURL }},
{{- end }}
},
{{- end }}
},
{{- end }}
}
{{- if $s.CredPointer }}
var token string
if {{ $payload }}.{{ $s.CredField }} != nil {
token = *{{ $payload }}.{{ $s.CredField }}
}
{{- end }}
ctx, err = auth{{ .Type }}Fn(ctx, {{ if $s.CredPointer }}token{{ else }}{{ $payload }}.{{ $s.CredField }}{{ end }}, &sc)
{{- end }}
{{- if ne $sidx 0 }}
}
{{- end }}
{{- end }}
{{- if ne $ridx 0 }}
}
{{- end }}
{{- end }}
if err != nil {
return nil, err
}
{{- end }}
{{- if .ServerStream }}
return nil, s.{{ .VarName }}(ctx, {{ if .PayloadRef }}{{ $payload }}, {{ end }}ep.Stream)
{{- else if .SkipRequestBodyEncodeDecode }}
{{- if .SkipResponseBodyEncodeDecode }}
{{ if .ResultRef }}res, {{ end }}body, err := s.{{ .VarName }}(ctx, {{ if .PayloadRef }}ep.Payload, {{ end }}ep.Body)
if err != nil {
return nil, err
}
return &{{ .ResponseStruct }}{ {{ if .ResultRef }}Result: res, {{ end }}Body: body }, nil
{{- else if .ViewedResult }}
res, {{ if not .ViewedResult.ViewName }}view, {{ end }}err := s.{{ .VarName }}(ctx, {{ if .PayloadRef }}ep.Payload, {{ end }}ep.Body)
if err != nil {
return nil, err
}
vres := {{ $.ViewedResult.Init.Name }}(res, {{ if .ViewedResult.ViewName }}{{ printf "%q" .ViewedResult.ViewName }}{{ else }}view{{ end }})
return vres, nil
{{- else }}
return {{ if not .ResultRef }}nil, {{ end }}s.{{ .VarName }}(ctx, {{ if .PayloadRef }}ep.Payload, {{ end }}ep.Body)
{{- end }}
{{- else if .ViewedResult }}
res, {{ if not .ViewedResult.ViewName }}view, {{ end }}err := s.{{ .VarName }}(ctx{{ if .PayloadRef }}, {{ $payload }}{{ end }})
if err != nil {
return nil, err
}
vres := {{ $.ViewedResult.Init.Name }}(res, {{ if .ViewedResult.ViewName }}{{ printf "%q" .ViewedResult.ViewName }}{{ else }}view{{ end }})
return vres, nil
{{- else if .SkipResponseBodyEncodeDecode }}
{{ if .ResultRef }}res, {{ end }}body, err := s.{{ .VarName }}(ctx{{ if .PayloadRef }}, {{ $payload}}{{ end }})
if err != nil {
return nil, err
}
return &{{ .ResponseStruct }}{ {{ if .ResultRef }}Result: res, {{ end }}Body: body }, nil
{{- else }}
return {{ if not .ResultRef }}nil, {{ end }}s.{{ .VarName }}(ctx{{ if .PayloadRef }}, {{ $payload }}{{ end }})
{{- end }}
}
}
`
// input: endpointMethodData
const serviceEndpointsUseT = `{{ printf "Use applies the given middleware to all the %q service endpoints." .Name | comment }}
func (e *{{ .VarName }}) Use(m func(goa.Endpoint) goa.Endpoint) {
{{- range .Methods }}
e.{{ .VarName }} = m(e.{{ .VarName }})
{{- end }}
}
`