-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
156 lines (139 loc) · 3.52 KB
/
handler.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
package swagger
import (
"fmt"
"html/template"
"io/fs"
"net/http"
"path"
"github.com/gin-gonic/gin"
"github.com/go-keg/swagger-api/dist"
"gopkg.in/yaml.v3"
)
const IndexTemp = `<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="{{.Prefix}}/swagger-ui.css"/>
<link rel="stylesheet" type="text/css" href="{{.Prefix}}/index.css"/>
<link rel="icon" type="image/png" href="{{.Prefix}}/favicon-32x32.png" sizes="32x32"/>
<link rel="icon" type="image/png" href=".{{.Prefix}}/favicon-16x16.png" sizes="16x16"/>
</head>
<body>
<div id="swagger-ui"></div>
<script src="{{.Prefix}}/swagger-ui-bundle.js" charset="UTF-8"></script>
<script src="{{.Prefix}}/swagger-ui-standalone-preset.js" charset="UTF-8"></script>
<script>
window.onload = function () {
window.ui = SwaggerUIBundle({
urls: {{.URLs}},
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});
};
</script>
</body>
</html>`
type config struct {
prefix string
swaggerUIPath string
openapiPath string
urls []OpenapiURL
}
func (r config) SwaggerUIPath() string {
return path.Join("/", r.prefix, r.swaggerUIPath)
}
func (r config) OpenapiPath() string {
return path.Join("/", r.prefix, r.openapiPath)
}
type Option func(cfg *config)
func SetPrefix(prefix string) Option {
return func(cfg *config) {
cfg.prefix = prefix
}
}
func SetSwaggerUIPath(path string) Option {
return func(cfg *config) {
cfg.swaggerUIPath = path
}
}
func SetOpenapiPath(path string) Option {
return func(cfg *config) {
cfg.openapiPath = path
}
}
func SetURLs(urls []OpenapiURL) Option {
return func(cfg *config) {
for i := range urls {
urls[i].URL = path.Join(cfg.OpenapiPath(), urls[i].URL)
}
cfg.urls = urls
}
}
type Openapi struct {
Info struct {
Title string
}
}
// Handler swagger ui
func Handler(apis fs.FS, opts ...Option) http.Handler {
cfg := config{
prefix: "swagger",
swaggerUIPath: "ui",
openapiPath: "apis",
}
for _, opt := range opts {
opt(&cfg)
}
if len(cfg.urls) == 0 {
err := fs.WalkDir(apis, ".", func(filePath string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
content, err := apis.Open(filePath)
if err != nil {
return err
}
var openapi Openapi
decoder := yaml.NewDecoder(content)
if err := decoder.Decode(&openapi); err != nil {
panic(fmt.Errorf("解析配置文件失败: %v", err))
}
cfg.urls = append(cfg.urls, OpenapiURL{
URL: path.Join(cfg.OpenapiPath(), filePath),
Name: openapi.Info.Title,
})
}
return nil
})
if err != nil {
panic(err)
}
}
router := gin.New()
router.SetHTMLTemplate(template.Must(template.New("swagger-ui").Parse(IndexTemp)))
gin.SetMode(gin.ReleaseMode)
router.GET(cfg.SwaggerUIPath(), func(c *gin.Context) {
c.HTML(200, "swagger-ui", map[string]any{
"URLs": cfg.urls,
"Prefix": cfg.SwaggerUIPath() + "/public",
})
})
router.StaticFS(cfg.SwaggerUIPath()+"/public", http.FS(dist.SwagFS))
router.StaticFS(cfg.OpenapiPath(), http.FS(apis))
return router
}
type OpenapiURL struct {
Name string `json:"name"`
URL string `json:"url"`
}