-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathapi.go
162 lines (145 loc) Β· 4.71 KB
/
api.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
package expr
import (
"sort"
"goa.design/goa/eval"
)
type (
// APIExpr contains the global properties for a API expression.
APIExpr struct {
// DSLFunc contains the DSL used to initialize the expression.
eval.DSLFunc
// Name of API
Name string
// Title of API
Title string
// Description of API
Description string
// Version is the version of the API described by this DSL.
Version string
// Servers lists the API hosts.
Servers []*ServerExpr
// TermsOfService describes or links to the service terms of API.
TermsOfService string
// Contact provides the API users with contact information.
Contact *ContactExpr
// License describes the API license.
License *LicenseExpr
// Docs points to the API external documentation.
Docs *DocsExpr
// Meta is a list of key/value pairs.
Meta MetaExpr
// Requirements contains the security requirements that apply to
// all the API service methods. One requirement is composed of
// potentially multiple schemes. Incoming requests must validate
// at least one requirement to be authorized.
Requirements []*SecurityExpr
// HTTP contains the HTTP specific API level expressions.
HTTP *HTTPExpr
// GRPC contains the gRPC specific API level expressions.
GRPC *GRPCExpr
// random generator used to build examples for the API types.
random *Random
}
// ContactExpr contains the API contact information.
ContactExpr struct {
// Name of the contact person/organization
Name string `json:"name,omitempty"`
// Email address of the contact person/organization
Email string `json:"email,omitempty"`
// URL pointing to the contact information
URL string `json:"url,omitempty"`
}
// LicenseExpr contains the license information for the API.
LicenseExpr struct {
// Name of license used for the API
Name string `json:"name,omitempty"`
// URL to the license used for the API
URL string `json:"url,omitempty"`
}
// DocsExpr points to external documentation.
DocsExpr struct {
// Description of documentation.
Description string `json:"description,omitempty"`
// URL to documentation.
URL string `json:"url,omitempty"`
}
)
// NewAPIExpr initializes an API expression.
func NewAPIExpr(name string, dsl func()) *APIExpr {
return &APIExpr{
Name: name,
HTTP: new(HTTPExpr),
GRPC: new(GRPCExpr),
DSLFunc: dsl,
}
}
// Schemes returns the list of transport schemes used by all the API servers.
// The possible values for the elements of the returned slice are "http",
// "https", "grpc" and "grpcs".
func (a *APIExpr) Schemes() []string {
schemes := make(map[string]struct{})
for _, s := range a.Servers {
for _, sch := range s.Schemes() {
schemes[sch] = struct{}{}
}
}
ss := make([]string, len(schemes))
i := 0
for s := range schemes {
ss[i] = s
i++
}
sort.Strings(ss)
return ss
}
// Random returns the random generator associated with a. APIs with identical
// names return generators that return the same sequence of pseudo random values.
func (a *APIExpr) Random() *Random {
if a.random == nil {
a.random = NewRandom(a.Name)
}
return a.random
}
// DefaultServer returns a server expression that describes a server which
// exposes all the services in the design and listens on localhost port 80 for
// HTTP requests and port 8080 for gRPC requests.
func (a *APIExpr) DefaultServer() *ServerExpr {
svcs := make([]string, len(Root.Services))
for i, svc := range Root.Services {
svcs[i] = svc.Name
}
return &ServerExpr{
Name: a.Name,
Description: "Default server for " + a.Name,
Services: svcs,
Hosts: []*HostExpr{{
Name: "localhost",
ServerName: a.Name,
URIs: []URIExpr{URIExpr("http://localhost:80"), URIExpr("grpc://localhost:8080")},
}},
}
}
// EvalName is the qualified name of the expression.
func (a *APIExpr) EvalName() string { return "API " + a.Name }
// Hash returns a unique hash value for a.
func (a *APIExpr) Hash() string { return "_api_+" + a.Name }
// Finalize makes sure that the API name is initialized and there is at least
// one server definition (if none exists, it creates a default server). If API
// name is empty, it sets the name of the first service definition as API name.
func (a *APIExpr) Finalize() {
if a.Name == "" {
a.Name = "api"
if len(Root.Services) > 0 {
a.Name = Root.Services[0].Name
}
}
if len(a.Servers) == 0 {
a.Servers = []*ServerExpr{a.DefaultServer()}
}
}
// EvalName is the qualified name of the expression.
func (l *LicenseExpr) EvalName() string { return "License " + l.Name }
// EvalName is the qualified name of the expression.
func (d *DocsExpr) EvalName() string { return "Documentation " + d.URL }
// EvalName is the qualified name of the expression.
func (c *ContactExpr) EvalName() string { return "Contact " + c.Name }