-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.go
More file actions
294 lines (267 loc) · 7 KB
/
Copy pathroute.go
File metadata and controls
294 lines (267 loc) · 7 KB
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
package web
import (
"net/http"
"strconv"
"strings"
"github.com/Instantan/web/internal/openapi"
)
type Use func(next http.Handler) http.Handler
type Api struct {
Method string
Path string
OperationId string
Summary string
Description string
Parameter Parameter
Responses Responses
Handler http.Handler
}
type Group struct {
routes *[]route
}
type Static struct {
PathPrefix string
Tags []string
Summary string
Description string
FS http.FileSystem
SpaMode bool
}
type route struct {
use *Use
api *Api
static *Static
tag *Tag
group *Group
}
type tags struct {
tags *map[string]Tag
references []string
}
func (g Group) Use(use Use) {
*g.routes = append(*g.routes, route{
use: &use,
})
}
func (g Group) Tag(tag Tag) {
*g.routes = append(*g.routes, route{
tag: &tag,
})
}
func (g Group) Api(api Api) {
assertIsOneOf(strings.ToUpper(api.Method), []string{
http.MethodGet,
http.MethodHead,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
http.MethodOptions,
http.MethodTrace,
})
assertIsNotEmpty("Api.Path", api.Path)
assertIsNotNil("Api.Handler", api.Handler)
*g.routes = append(*g.routes, route{
api: &api,
})
}
func (g Group) Static(static Static) {
*g.routes = append(*g.routes, route{
static: &static,
})
}
func (g Group) Group(group func(Group)) {
gr := Group{
routes: &[]route{},
}
group(gr)
*g.routes = append(*g.routes, route{
group: &gr,
})
}
func (t *tags) clone() *tags {
return &tags{
tags: t.tags,
references: append([]string{}, t.references...),
}
}
func (t *tags) add(tag Tag) {
(*t.tags)[tag.Name] = tag
t.references = append(t.references, tag.Name)
}
func (t *tags) openapiTags() []openapi.Tag {
tags := []openapi.Tag{}
for _, tag := range *t.tags {
tags = append(tags, openapi.Tag{
Name: tag.Name,
Description: tag.Description,
})
}
return tags
}
func (g Group) openapiPaths(mux *http.ServeMux, components *openapi.Components, use Use, tags *tags) *openapi.Paths {
paths := &openapi.Paths{}
for i := range *g.routes {
r := (*g.routes)[i]
if r.api != nil {
api := r.api
p, _ := paths.Get(api.Path)
operation := &openapi.Operation{
OperationId: api.OperationId,
Tags: tags.references,
Summary: api.Summary,
Description: api.Description,
Responses: openapi.Responses{
HTTPStatusCodeResponses: map[string]openapi.Response{},
},
Parameters: []openapi.Parameter{},
}
if api.Parameter.Body.Value != nil {
operation.RequestBody.Required = !api.Parameter.Body.Optional
operation.RequestBody.Description = api.Parameter.Body.Description
content := operation.RequestBody.Content["text/*"]
content.Example = api.Parameter.Body.Value
s := *openapi.ValueToSchema(api.Parameter.Body.Value)
if s.TypeName == "" {
content.Schema = s
} else {
content.Schema.Ref = "#/components/schemas/" + s.TypeName
components.Schemas[s.TypeName] = s
}
operation.RequestBody.Content["application/json"] = content
}
if len(api.Parameter.Cookie) > 0 {
for key, value := range api.Parameter.Cookie {
operation.Parameters = append(operation.Parameters, openapi.Parameter{
Name: key,
In: "cookie",
Description: value.Description,
Required: !value.Optional,
Schema: *openapi.ValueToSchema(value.Value),
Example: value.Value,
})
}
}
if len(api.Parameter.Path) > 0 {
for key, value := range api.Parameter.Path {
operation.Parameters = append(operation.Parameters, openapi.Parameter{
Name: key,
In: "path",
Description: value.Description,
Required: true,
Schema: *openapi.ValueToSchema(value.Value),
Example: value.Value,
})
}
}
if len(api.Parameter.Query) > 0 {
for key, value := range api.Parameter.Query {
operation.Parameters = append(operation.Parameters, openapi.Parameter{
Name: key,
In: "query",
Description: value.Description,
Required: !value.Optional,
Schema: *openapi.ValueToSchema(value.Value),
Example: value.Value,
})
}
}
if len(api.Parameter.Header) > 0 {
for key, value := range api.Parameter.Header {
operation.Parameters = append(operation.Parameters, openapi.Parameter{
Name: key,
In: "header",
Description: value.Description,
Required: !value.Optional,
Schema: *openapi.ValueToSchema(value.Value),
Example: value.Value,
})
}
}
createResponse := func(description string, value any) openapi.Response {
c := isContentType(value)
if c == nil {
c = &ContentType{
ApplicationJson: value,
}
}
mediaTypes := map[string]openapi.MediaType{}
for contentType, value := range c.Iterate() {
content := openapi.MediaType{}
s := *openapi.ValueToSchema(value)
content.Example = value
if s.TypeName == "" {
content.Schema = s
} else {
content.Schema.Ref = "#/components/schemas/" + s.TypeName
components.Schemas[s.TypeName] = s
}
mediaTypes[contentType] = content
}
return openapi.Response{
Description: description,
Content: mediaTypes,
}
}
for status, value := range api.Responses.Iterate() {
if status == 0 {
operation.Responses.Default = createResponse("Default", value)
continue
}
operation.Responses.HTTPStatusCodeResponses[strconv.Itoa(status)] = createResponse(http.StatusText(status), value)
}
switch api.Method {
case http.MethodGet:
p.Get = operation
case http.MethodPut:
p.Put = operation
case http.MethodPost:
p.Post = operation
case http.MethodDelete:
p.Delete = operation
case http.MethodHead:
p.Head = operation
case http.MethodPatch:
p.Patch = operation
case http.MethodTrace:
p.Trace = operation
case http.MethodConnect:
p.Trace = operation
case http.MethodOptions:
p.Options = operation
}
paths.Set(api.Path, p)
if use == nil {
mux.Handle(api.Method+" "+api.Path, api.Handler)
} else {
mux.Handle(api.Method+" "+api.Path, use(api.Handler))
}
} else if r.group != nil {
group := r.group
for path, item := range group.openapiPaths(mux, components, use, tags.clone()).Iterate() {
paths.Set(path, item)
}
} else if r.tag != nil {
tags.add(*r.tag)
} else if r.use != nil {
if use != nil {
use = Chain(use, *r.use)
} else {
use = *r.use
}
} else if r.static != nil {
var handler http.Handler
if use != nil {
handler = http.StripPrefix(r.static.PathPrefix, use(http.FileServer(r.static.FS)))
} else {
handler = http.StripPrefix(r.static.PathPrefix, http.FileServer(r.static.FS))
}
if r.static.SpaMode {
mux.Handle(http.MethodGet+" "+r.static.PathPrefix, createSpaModeRedirect(handler))
} else {
mux.Handle(http.MethodGet+" "+r.static.PathPrefix, handler)
}
}
}
return paths
}