-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswagger.go
164 lines (149 loc) · 4 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
package fiber_swagger
import (
"github.com/getkin/kin-openapi/openapi3"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/peanut-cc/fiber_swagger/router"
"mime/multipart"
"sync"
"time"
)
type Swagger struct {
routersMap sync.Map
Title string
Description string
Version string
License *openapi3.License
Contact *openapi3.Contact
Components []interface{}
OpenAPI *openapi3.T
Schemas map[string]*openapi3.SchemaRef
Paths map[string]map[string]*router.Router
PathResponseTypes map[string]map[int]interface{}
OpenAPIYamlFile string
}
func NewSwagger(title, description, version string, options ...Option) *Swagger {
swagger := &Swagger{
Title: title,
Description: description,
Version: version,
Components: nil,
Schemas: make(map[string]*openapi3.SchemaRef),
Paths: make(map[string]map[string]*router.Router),
PathResponseTypes: make(map[string]map[int]interface{}),
}
for _, option := range options {
option(swagger)
}
return swagger
}
func (s *Swagger) buildOpenAPI() {
openapi := &openapi3.T{
Info: &openapi3.Info{
Title: s.Title,
Description: s.Description,
Contact: s.Contact,
License: s.License,
Version: s.Version,
},
OpenAPI: "3.0.0",
Components: &openapi3.Components{},
Tags: openapi3.Tags{},
Paths: map[string]*openapi3.PathItem{},
Security: openapi3.SecurityRequirements{map[string][]string{"http": {}}},
}
s.OpenAPI = openapi
}
type HttpRequestResponse struct {
URLName string
Request interface{}
Responses map[int]interface{}
}
func (s *Swagger) Bind(name string, request interface{}, responses map[int]interface{}) {
httpReqRes := &HttpRequestResponse{
URLName: name,
Request: request,
Responses: responses,
}
s.store(httpReqRes)
}
func (s *Swagger) store(args *HttpRequestResponse) {
s.routersMap.Store(args.URLName, args)
}
func (s *Swagger) Generate(app *fiber.App) {
s.buildOpenAPI()
for _, route := range app.GetRoutes() {
if route.Name == "" {
continue
}
reqRep := s.load(route.Name)
req := reqRep.Request
s.addResponseComponent(reqRep.Responses)
s.AddComponents(req)
s.addPath(route, req, reqRep.Responses)
}
s.buildComponents()
s.buildPaths()
err := s.WriteToYaml()
if err != nil {
panic(err)
}
}
func (s *Swagger) addResponseComponent(responses map[int]interface{}) {
for _, response := range responses {
if response != nil {
s.AddComponents(response)
}
}
}
func (s *Swagger) load(name string) (httpResRep *HttpRequestResponse) {
result, _ := s.routersMap.Load(name)
return result.(*HttpRequestResponse)
}
func (s *Swagger) getSchemaFromBaseType(field interface{}) *openapi3.Schema {
var schema *openapi3.Schema
var m float64
m = float64(0)
switch field.(type) {
case int, int8, int16, *int, *int8, *int16:
schema = openapi3.NewIntegerSchema()
case uint, uint8, uint16, *uint, *uint8, *uint16:
schema = openapi3.NewIntegerSchema()
schema.Min = &m
case int32, *int32:
schema = openapi3.NewInt32Schema()
case uint32, *uint32:
schema = openapi3.NewInt32Schema()
schema.Min = &m
case int64, *int64:
schema = openapi3.NewInt64Schema()
case uint64, *uint64:
schema = openapi3.NewInt64Schema()
schema.Min = &m
case string, *string:
schema = openapi3.NewStringSchema()
case time.Time, *time.Time:
schema = openapi3.NewDateTimeSchema()
case uuid.UUID, *uuid.UUID:
schema = openapi3.NewUUIDSchema()
case float32, float64, *float32, *float64:
schema = openapi3.NewFloat64Schema()
case bool, *bool:
schema = openapi3.NewBoolSchema()
case []byte:
schema = openapi3.NewBytesSchema()
case *multipart.FileHeader:
schema = openapi3.NewStringSchema()
schema.Format = "binary"
case []*multipart.FileHeader:
schema = openapi3.NewArraySchema()
schema.Items = &openapi3.SchemaRef{
Value: &openapi3.Schema{
Type: "string",
Format: "binary",
},
}
default:
}
return schema
}