forked from long2ice/swagin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswagin.go
187 lines (172 loc) · 4.7 KB
/
swagin.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
package swagin
import (
"embed"
"encoding/json"
"html/template"
"net/http"
"strings"
"github.com/getkin/kin-openapi/openapi3"
"github.com/gin-gonic/gin"
"github.com/long2ice/swagin/router"
"github.com/long2ice/swagin/swagger"
)
//go:embed templates/*
var templates embed.FS
type SwaGin struct {
*gin.Engine
Swagger *swagger.Swagger
Routers map[string]map[string]*router.Router
subApps map[string]*SwaGin
rootPath string
ErrorHandler router.ErrorHandlerFunc
}
func New(swagger *swagger.Swagger) *SwaGin {
f := &SwaGin{Engine: gin.New(), Swagger: swagger, Routers: make(map[string]map[string]*router.Router), subApps: make(map[string]*SwaGin)}
f.SetHTMLTemplate(template.Must(template.ParseFS(templates, "templates/*.html")))
if swagger != nil {
swagger.Routers = f.Routers
}
return f
}
func (g *SwaGin) WithErrorHandler(handler router.ErrorHandlerFunc) *SwaGin {
g.ErrorHandler = handler
return g
}
func (g *SwaGin) Mount(path string, app *SwaGin) {
app.rootPath = path
app.Engine = g.Engine
if app.ErrorHandler == nil {
app.ErrorHandler = g.ErrorHandler
}
app.Swagger.Servers = append(app.Swagger.Servers, &openapi3.Server{
URL: path,
})
g.subApps[path] = app
}
func (g *SwaGin) Group(path string, options ...Option) *Group {
group := &Group{
SwaGin: g,
Path: path,
}
for _, option := range options {
option(group)
}
return group
}
func (g *SwaGin) Handle(path string, method string, r *router.Router) {
r.Method = method
r.Path = path
if g.Routers[path] == nil {
g.Routers[path] = make(map[string]*router.Router)
}
g.Routers[path][method] = r
}
func (g *SwaGin) GET(path string, router *router.Router) {
g.Handle(path, http.MethodGet, router)
}
func (g *SwaGin) POST(path string, router *router.Router) {
g.Handle(path, http.MethodPost, router)
}
func (g *SwaGin) HEAD(path string, router *router.Router) {
g.Handle(path, http.MethodHead, router)
}
func (g *SwaGin) PATCH(path string, router *router.Router) {
g.Handle(path, http.MethodPatch, router)
}
func (g *SwaGin) DELETE(path string, router *router.Router) {
g.Handle(path, http.MethodDelete, router)
}
func (g *SwaGin) PUT(path string, router *router.Router) {
g.Handle(path, http.MethodPut, router)
}
func (g *SwaGin) OPTIONS(path string, router *router.Router) {
g.Handle(path, http.MethodOptions, router)
}
func (g *SwaGin) init() {
g.initRouters()
if g.Swagger == nil {
return
}
gin.DisableBindValidation()
g.Engine.GET(g.fullPath(g.Swagger.OpenAPIUrl), func(c *gin.Context) {
if strings.HasSuffix(g.Swagger.OpenAPIUrl, ".yml") ||
strings.HasSuffix(g.Swagger.OpenAPIUrl, ".yaml") {
y, err := g.Swagger.MarshalYAML()
if err != nil {
c.JSON(http.StatusInternalServerError, map[string]string{"status": err.Error()})
}
c.String(http.StatusOK, string(y))
} else {
c.JSON(http.StatusOK, g.Swagger)
}
})
g.Engine.GET(g.fullPath(g.Swagger.DocsUrl), func(c *gin.Context) {
options := `{}`
if g.Swagger.SwaggerOptions != nil {
data, err := json.Marshal(g.Swagger.SwaggerOptions)
if err != nil {
panic(err)
}
options = string(data)
}
c.HTML(http.StatusOK, "swagger.html", gin.H{
"openapi_url": g.fullPath(g.Swagger.OpenAPIUrl),
"title": g.Swagger.Title,
"swagger_options": options,
})
})
g.Engine.GET(g.fullPath(g.Swagger.RedocUrl), func(c *gin.Context) {
options := `{}`
if g.Swagger.RedocOptions != nil {
data, err := json.Marshal(g.Swagger.RedocOptions)
if err != nil {
panic(err)
}
options = string(data)
}
c.HTML(http.StatusOK, "redoc.html", gin.H{
"openapi_url": g.fullPath(g.Swagger.OpenAPIUrl),
"title": g.Swagger.Title,
"redoc_options": options,
})
})
g.Swagger.BuildOpenAPI()
}
func (g *SwaGin) initRouters() {
for path, m := range g.Routers {
path = g.fullPath(path)
for method, r := range m {
if r.ErrorHandler == nil {
r.WithErrorHandler(g.ErrorHandler)
}
handlers := r.GetHandlers()
if method == http.MethodGet {
g.Engine.GET(path, handlers...)
} else if method == http.MethodPost {
g.Engine.POST(path, handlers...)
} else if method == http.MethodHead {
g.Engine.HEAD(path, handlers...)
} else if method == http.MethodPatch {
g.Engine.PATCH(path, handlers...)
} else if method == http.MethodDelete {
g.Engine.DELETE(path, handlers...)
} else if method == http.MethodPut {
g.Engine.PUT(path, handlers...)
} else if method == http.MethodOptions {
g.Engine.OPTIONS(path, handlers...)
} else {
g.Engine.Any(path, handlers...)
}
}
}
}
func (g *SwaGin) fullPath(path string) string {
return g.rootPath + path
}
func (g *SwaGin) Run(addr ...string) error {
g.init()
for _, s := range g.subApps {
s.init()
}
return g.Engine.Run(addr...)
}