-
Notifications
You must be signed in to change notification settings - Fork 3
/
swagger.go
135 lines (113 loc) · 4.26 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
package atreugoswagger
import (
"html/template"
"regexp"
"strings"
"github.com/Nerzal/atreugo-swagger/v4/assets"
"github.com/savsgio/atreugo/v11"
"github.com/swaggo/swag/v2"
"github.com/valyala/fasthttp"
)
const fileRegex = `(.*)(redoc\.html|index\.html|doc\.json|favicon-16x16\.png|favicon-32x32\.png|/oauth2-redirect\.html|swagger-ui\.css|swagger-ui\.css\.map|swagger-ui\.js|swagger-ui\.js\.map|swagger-ui-bundle\.js|swagger-ui-bundle\.js\.map|swagger-ui-standalone-preset\.js|swagger-ui-standalone-preset\.js\.map)[\?|.]*`
// Config stores atreugoswagger configuration variables.
type Config struct {
// The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`.
URL string
// Title is used for the redoc documentation
Title string
// InstanceName is used to name different swagger document instances. Defaults to 'swagger'.
InstanceName string
}
// Title presents the title of the tab
func Title(title string) func(c *Config) {
return func(c *Config) {
c.Title = title
}
}
// URL presents the url pointing to API definition (normally swagger.json or swagger.yaml).
func URL(url string) func(c *Config) {
return func(c *Config) {
c.URL = url
}
}
// InstanceName presents the name of the document.
func InstanceName(instanceName string) func(c *Config) {
return func(c *Config) {
c.InstanceName = instanceName
}
}
// AtreugoWrapHandler is a handler which serves swagger files
func AtreugoWrapHandler(confs ...func(c *Config)) func(ctx *atreugo.RequestCtx) error {
config := &Config{
URL: "doc.json",
InstanceName: swag.Name,
}
for _, c := range confs {
c(config)
}
// create a template with name
t := template.New("swagger_index.html")
index, _ := t.Parse(indexTempl)
type pro struct {
Host string
}
re := regexp.MustCompile(fileRegex)
return func(ctx *atreugo.RequestCtx) error {
var matches []string
if matches = re.FindStringSubmatch(string(ctx.RequestURI())); len(matches) != 3 {
return ctx.TextResponse("404 page not found", fasthttp.StatusNotFound)
}
path := matches[2]
setContentType(ctx, path)
switch path {
case "index.html":
return index.Execute(ctx.Response.BodyWriter(), config)
case "redoc.html":
redocHTML := strings.Replace(assets.RedocDocumentation, "{title}", config.Title, 1)
return ctx.HTTPResponse(redocHTML, fasthttp.StatusOK)
case "doc.json":
doc, _ := swag.ReadDoc(config.InstanceName)
return ctx.TextResponse(doc, fasthttp.StatusOK)
case "favicon-16x16.png":
return ctx.RawResponseBytes(assets.FileFavicon16x16Png, fasthttp.StatusOK)
case "favicon-32x32.png":
return ctx.RawResponseBytes(assets.FileFavicon32x32Png, fasthttp.StatusOK)
case "oauth2-redirect.html":
return ctx.RawResponseBytes(assets.FileOauth2RedirectHTML, fasthttp.StatusOK)
case "swagger-ui-bundle.js":
return ctx.RawResponseBytes(assets.FileSwaggerUIBundleJs, fasthttp.StatusOK)
case "swagger-ui-bundle.js.map":
return ctx.RawResponseBytes(assets.FileSwaggerUIBundleJsMap, fasthttp.StatusOK)
case "swagger-ui-standalone-preset.js":
return ctx.RawResponseBytes(assets.FileSwaggerUIStandalonePresetJs, fasthttp.StatusOK)
case "swagger-ui-standalone-preset.js.map":
return ctx.RawResponseBytes(assets.FileSwaggerUIStandalonePresetJsMap, fasthttp.StatusOK)
case "swagger-ui.css":
return ctx.RawResponseBytes(assets.FileSwaggerUICSS, fasthttp.StatusOK)
case "swagger-ui.css.map":
return ctx.RawResponseBytes(assets.FileSwaggerUICSSMap, fasthttp.StatusOK)
case "swagger-ui.js":
return ctx.RawResponseBytes(assets.FileSwaggerUIJs, fasthttp.StatusOK)
case "swagger-ui.js.map":
return ctx.RawResponseBytes(assets.FileSwaggerUIJsMap, fasthttp.StatusOK)
}
return ctx.TextResponse("404 page not found", fasthttp.StatusNotFound)
}
}
func setContentType(ctx *atreugo.RequestCtx, path string) {
if strings.Contains(path, "png") {
ctx.Response.Header.Add("Content-Type", "image/png")
}
if strings.Contains(path, "jpg") {
ctx.Response.Header.Add("Content-Type", "image/jpeg")
}
if strings.Contains(path, "html") {
ctx.Response.Header.Add("Content-Type", "text/html")
}
if strings.Contains(path, "css") {
ctx.Response.Header.Add("Content-Type", "text/css")
}
if strings.Contains(path, "js") {
ctx.Response.Header.Add("Content-Type", "text/javascript")
}
}