-
Notifications
You must be signed in to change notification settings - Fork 0
/
swagger.go
267 lines (232 loc) · 4.68 KB
/
swagger.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
package swagger
import (
"encoding/json"
"errors"
"strings"
"sync"
)
type (
// Base object for describing the builded API
BaseAPI struct {
Version string `json:"swagger,omitempty"`
Info IInfo `json:"info,omitempty"`
// Base path to API, it consist the prefix of endpoint path
BasePath string `json:"basePath,omitempty"`
}
// High level object for describing the builded API
Doc struct {
BaseAPI
// List of paths to endpoints
Paths map[string]Methods `json:"paths,omitempty"`
// List of definitions
Definitions map[string]*Definition `json:"definitions,omitempty"`
}
// Information about the created swagger
Info struct {
Description string `json:"description,omitempty"`
Title string `json:"title,omitempty"`
TermsOfService string `json:"termsOfService,omitempty"`
Contact IContact `json:"contact,omitempty"`
License ILicense `json:"license,omitempty"`
Version string `json:"version,omitempty"`
}
Contact struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Email string `json:"email,omitempty"`
}
License struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
}
)
type ISwaggerAPI interface{}
type BasePather interface {
SetBasePath(p string) Informer
}
type Informer interface {
SetInfo(i IInfo) ISwaggerAPI
}
func NewSwagger() BasePather {
return &BaseAPI{
Version: "2.0",
}
}
func (s *Doc) ReadDoc() string {
return string(s.JSON())
}
func (s *Doc) JSON() (jsonData []byte) {
jsonData, err := json.MarshalIndent(s, "", " ")
if err != nil {
return
}
return
}
func (s *BaseAPI) NewSwagger() BasePather {
return &BaseAPI{
Version: "2.0",
}
}
func (s *BaseAPI) SetBasePath(p string) Informer {
if s == nil {
return nil
}
s.BasePath = strings.TrimRight(p, "/")
return s
}
func (s *BaseAPI) SetInfo(i IInfo) ISwaggerAPI {
if s == nil {
return nil
}
s.Info = i
return s
}
type IInfo interface {
SetVersion(v string) IInfo
SetContact(c IContact) IInfo
SetDescription(d string) IInfo
SetTitle(t string) IInfo
SetLicense(l ILicense) IInfo
SetTermOfService(tos string) IInfo
}
func NewInfo() IInfo {
return &Info{
Title: "Swagger API",
Description: "This is an embedded Swagger-server.",
TermsOfService: "http://swagger.io/terms/",
Version: "0.0.1",
Contact: NewContact(),
License: NewLicense(),
}
}
func (i *Info) SetVersion(v string) IInfo {
if i == nil {
return nil
}
i.Version = v
return i
}
func (i *Info) SetContact(c IContact) IInfo {
if i == nil {
return nil
}
i.Contact = c
return i
}
func (i *Info) SetDescription(d string) IInfo {
if i == nil {
return nil
}
i.Description = d
return i
}
func (i *Info) SetTitle(t string) IInfo {
if i == nil {
return nil
}
i.Title = t
return i
}
func (i *Info) SetLicense(l ILicense) IInfo {
if i == nil {
return nil
}
i.License = l
return i
}
func (i *Info) SetTermOfService(tos string) IInfo {
if i == nil {
return nil
}
i.TermsOfService = tos
return i
}
type ILicense interface {
SetURL(url string) ILicense
SetName(n string) ILicense
}
func NewLicense() ILicense {
return &License{
Name: "Apache 2.0",
URL: "http://www.apache.org/licenses/LICENSE-2.0.html",
}
}
func (l *License) SetURL(url string) ILicense {
if l == nil {
return nil
}
l.URL = url
return l
}
func (l *License) SetName(n string) ILicense {
if l == nil {
return nil
}
l.Name = n
return l
}
type IContact interface {
SetEmail(e string) IContact
SetName(n string) IContact
SetURL(url string) IContact
}
func NewContact() IContact {
return &Contact{
Name: "API Support",
URL: "https://test.test",
Email: "support@test.test",
}
}
func (c *Contact) SetName(n string) IContact {
if c == nil {
return nil
}
c.Name = n
return c
}
func (c *Contact) SetURL(url string) IContact {
if c == nil {
return nil
}
c.URL = url
return c
}
func (c *Contact) SetEmail(e string) IContact {
if c == nil {
return nil
}
c.Email = e
return c
}
var (
swaggerMu sync.RWMutex
swag map[string]Swagger
)
// Swagger is a interface to read swagger document.
type Swagger interface {
ReadDoc() string
}
// Register registers swagger for given name.
func Register(name string, swagger Swagger) {
if swagger == nil {
panic("swagger is nil")
}
if swag == nil {
swag = make(map[string]Swagger)
}
swaggerMu.Lock()
defer swaggerMu.Unlock()
if _, ok := swag[name]; ok {
return
}
swag[name] = swagger
}
// ReadDoc reads swagger document.
func ReadDoc(name string) (string, error) {
if swag != nil {
swaggerMu.RLock()
defer swaggerMu.RUnlock()
return swag[name].ReadDoc(), nil
}
return "", errors.New("not yet registered swag")
}