-
Notifications
You must be signed in to change notification settings - Fork 8
/
router.go
181 lines (164 loc) · 4.91 KB
/
router.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
package crud
import (
_ "embed"
"fmt"
"github.com/jakecoffman/crud/option"
"regexp"
"strings"
)
// Router is the main object that is used to generate swagger and holds the underlying router.
type Router struct {
// Swagger is exposed so the user can edit additional optional fields.
Swagger Swagger
// The underlying router being used behind Adapter interface.
adapter Adapter
// used for automatically incrementing the model name, e.g. Model 1, Model 2.
modelCounter int
// options
stripUnknown bool
allowUnknown bool
}
type Adapter interface {
Install(router *Router, spec *Spec) error
Serve(swagger *Swagger, addr string) error
}
// NewRouter initializes a router.
func NewRouter(title, version string, adapter Adapter, options ...option.Option) *Router {
r := &Router{
Swagger: Swagger{
Swagger: "2.0",
Info: Info{Title: title, Version: version},
Paths: map[string]*Path{},
Definitions: map[string]JsonSchema{},
},
adapter: adapter,
modelCounter: 1,
stripUnknown: true,
allowUnknown: true,
}
for _, o := range options {
if o.StripUnknown != nil {
r.stripUnknown = *o.StripUnknown
} else if o.AllowUnknown != nil {
r.allowUnknown = *o.AllowUnknown
}
}
return r
}
// Add routes to the swagger spec and installs a handler with built-in validation. Some validation of the
// route itself occurs on Add so this is the kind of error that can be returned.
func (r *Router) Add(specs ...Spec) error {
for i := range specs {
spec := specs[i]
if err := spec.Valid(); err != nil {
return err
}
if _, ok := r.Swagger.Paths[spec.Path]; !ok {
r.Swagger.Paths[spec.Path] = &Path{}
}
path := r.Swagger.Paths[spec.Path]
var operation *Operation
switch strings.ToLower(spec.Method) {
case "get":
if path.Get != nil {
return fmt.Errorf("duplicate GET on route %v", spec.Path)
}
path.Get = &Operation{}
operation = path.Get
case "post":
if path.Post != nil {
return fmt.Errorf("duplicate POST on route %v", spec.Path)
}
path.Post = &Operation{}
operation = path.Post
case "put":
if path.Put != nil {
return fmt.Errorf("duplicate PUT on route %v", spec.Path)
}
path.Put = &Operation{}
operation = path.Put
case "patch":
if path.Patch != nil {
return fmt.Errorf("duplicate PATCH on route %v", spec.Path)
}
path.Patch = &Operation{}
operation = path.Patch
case "options":
if path.Options != nil {
return fmt.Errorf("duplicate PATCH on route %v", spec.Path)
}
path.Options = &Operation{}
operation = path.Options
case "delete":
if path.Delete != nil {
return fmt.Errorf("duplicate DELETE on route %v", spec.Path)
}
path.Delete = &Operation{}
operation = path.Delete
default:
panic("Unhandled method " + spec.Method)
}
operation.Responses = defaultResponse
if spec.Responses != nil {
operation.Responses = spec.Responses
}
operation.Tags = spec.Tags
operation.Description = spec.Description
operation.Summary = spec.Summary
if spec.Validate.Path.Initialized() {
params := spec.Validate.Path.ToSwaggerParameters("path")
operation.Parameters = append(operation.Parameters, params...)
}
if spec.Validate.Query.Initialized() {
params := spec.Validate.Query.ToSwaggerParameters("query")
operation.Parameters = append(operation.Parameters, params...)
}
if spec.Validate.Header.Initialized() {
params := spec.Validate.Header.ToSwaggerParameters("header")
operation.Parameters = append(operation.Parameters, params...)
}
if spec.Validate.FormData.Initialized() {
params := spec.Validate.FormData.ToSwaggerParameters("formData")
operation.Parameters = append(operation.Parameters, params...)
}
if spec.Validate.Body.Initialized() {
modelName := fmt.Sprintf("Model-%v", r.modelCounter)
parameter := Parameter{
In: "body",
Name: "body",
Schema: &Ref{fmt.Sprint("#/definitions/", modelName)},
}
r.Swagger.Definitions[modelName] = spec.Validate.Body.ToJsonSchema()
r.modelCounter++
operation.Parameters = append(operation.Parameters, parameter)
}
if err := r.adapter.Install(r, &spec); err != nil {
return err
}
}
return nil
}
// Validate are optional fields that will be used during validation. Leave unneeded
// properties nil and they will be ignored.
type Validate struct {
Query Field
Body Field
Path Field
FormData Field
Header Field
}
// Serve installs the swagger and the swagger-ui and runs the server.
func (r *Router) Serve(addr string) error {
return r.adapter.Serve(&r.Swagger, addr)
}
// SwaggerPathPattern regex captures swagger path params.
var SwaggerPathPattern = regexp.MustCompile("\\{([^}]+)\\}")
func pathParms(swaggerUrl string) (params []string) {
for _, p := range SwaggerPathPattern.FindAllString(swaggerUrl, -1) {
params = append(params, p[1:len(p)-1])
}
return
}
// SwaggerUiTemplate contains the html for swagger UI.
//go:embed swaggerui.html
var SwaggerUiTemplate []byte