Skip to content

Commit 4115b38

Browse files
authored
feat: add instance name (#6)
1 parent ff22574 commit 4115b38

File tree

2 files changed

+63
-39
lines changed

2 files changed

+63
-39
lines changed

swagger.go

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ import (
1717
type Config struct {
1818
// The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `mockedSwag.json`.
1919
URL string
20-
DeepLinking bool
20+
InstanceName string
2121
DocExpansion string
2222
DomID string
23+
DeepLinking bool
2324
PersistAuthorization bool
2425
}
2526

@@ -51,8 +52,16 @@ func DomID(domID string) func(c *Config) {
5152
}
5253
}
5354

54-
// If set to true, it persists authorization data and it would not be lost on browser close/refresh
55-
// Defaults to false
55+
// InstanceName set the instance name that was used to generate the swagger documents
56+
// Defaults to swag.Name ("swagger").
57+
func InstanceName(name string) func(*Config) {
58+
return func(c *Config) {
59+
c.InstanceName = name
60+
}
61+
}
62+
63+
// PersistAuthorization Persist authorization information over browser close/refresh.
64+
// Defaults to false.
5665
func PersistAuthorization(persistAuthorization bool) func(c *Config) {
5766
return func(c *Config) {
5867
c.PersistAuthorization = persistAuthorization
@@ -66,59 +75,60 @@ var WrapHandler = FiberWrapHandler()
6675
func FiberWrapHandler(configFns ...func(c *Config)) fiber.Handler {
6776
var once sync.Once
6877

69-
h := swaggerFiles.Handler
78+
handler := swaggerFiles.Handler
7079

71-
config := &Config{
80+
config := Config{
7281
URL: "doc.json",
73-
DeepLinking: true,
7482
DocExpansion: "list",
7583
DomID: "#swagger-ui",
84+
InstanceName: swag.Name,
85+
DeepLinking: true,
7686
}
7787

7888
for _, configFn := range configFns {
79-
configFn(config)
89+
configFn(&config)
8090
}
8191

8292
// create a template with name
83-
t := template.New("swagger_index.html")
84-
index, _ := t.Parse(indexTemplate)
93+
index, _ := template.New("swagger_index.html").Parse(indexTemplate)
8594

8695
var re = regexp.MustCompile(`^(.*/)([^?].*)?[?|.]*$`)
8796

88-
return func(c *fiber.Ctx) error {
89-
matches := re.FindStringSubmatch(string(c.Request().URI().Path()))
97+
return func(ctx *fiber.Ctx) error {
98+
matches := re.FindStringSubmatch(string(ctx.Request().URI().Path()))
9099
path := matches[2]
91100

92101
once.Do(func() {
93-
h.Prefix = matches[1]
102+
handler.Prefix = matches[1]
94103
})
95104

96105
fileExt := filepath.Ext(path)
97106
switch path {
98107
case "":
99-
return c.Redirect(filepath.Join(h.Prefix, "index.html"), fiber.StatusMovedPermanently)
108+
return ctx.Redirect(filepath.Join(handler.Prefix, "index.html"), fiber.StatusMovedPermanently)
100109

101110
case "index.html":
102-
c.Type(fileExt[0:], "utf-8")
103-
return index.Execute(c, config)
111+
ctx.Type(fileExt[0:], "utf-8")
112+
113+
return index.Execute(ctx, config)
104114
case "doc.json":
105-
doc, err := swag.ReadDoc()
115+
doc, err := swag.ReadDoc(config.InstanceName)
106116
if err != nil {
107-
_, err := c.Status(http.StatusInternalServerError).WriteString(http.StatusText(http.StatusInternalServerError))
117+
_, err := ctx.Status(http.StatusInternalServerError).WriteString(http.StatusText(http.StatusInternalServerError))
118+
108119
return err
109120
}
110121

111-
c.Type(fileExt[0:], "utf-8")
112-
return c.SendString(doc)
122+
ctx.Type(fileExt[0:], "utf-8")
123+
return ctx.SendString(doc)
113124
default:
114-
handler := fasthttpadaptor.NewFastHTTPHandler(h)
115-
handler(c.Context())
125+
fasthttpadaptor.NewFastHTTPHandler(handler)(ctx.Context())
116126

117127
switch fileExt {
118128
case ".css":
119-
c.Type(fileExt[0:], "utf-8")
129+
ctx.Type(fileExt[0:], "utf-8")
120130
case ".png", ".js":
121-
c.Type(fileExt[0:])
131+
ctx.Type(fileExt[0:])
122132
}
123133

124134
return nil

swagger_test.go

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -237,35 +237,33 @@ func TestWrapHandler(t *testing.T) {
237237

238238
router.Get("/*", FiberWrapHandler(DocExpansion("none"), DomID("#swagger-ui")))
239239

240-
w1 := performRequest("GET", "/index.html", router)
241-
assert.Equal(t, 200, w1.StatusCode)
240+
w1 := performRequest(http.MethodGet, "/index.html", router)
241+
assert.Equal(t, http.StatusOK, w1.StatusCode)
242242
assert.Equal(t, w1.Header.Get("Content-Type"), "text/html; charset=utf-8")
243243

244-
w2 := performRequest("GET", "/doc.json", router)
245-
assert.Equal(t, 500, w2.StatusCode)
244+
w2 := performRequest(http.MethodGet, "/doc.json", router)
245+
assert.Equal(t, http.StatusInternalServerError, w2.StatusCode)
246246

247247
swag.Register(swag.Name, &mockedSwag{})
248-
w2 = performRequest("GET", "/doc.json", router)
249-
assert.Equal(t, 200, w2.StatusCode)
248+
w2 = performRequest(http.MethodGet, "/doc.json", router)
249+
assert.Equal(t, http.StatusOK, w2.StatusCode)
250250
assert.Equal(t, w2.Header.Get("Content-Type"), fiber.MIMEApplicationJSONCharsetUTF8)
251251

252-
w3 := performRequest("GET", "/favicon-16x16.png", router)
253-
assert.Equal(t, 200, w3.StatusCode)
252+
w3 := performRequest(http.MethodGet, "/favicon-16x16.png", router)
253+
assert.Equal(t, http.StatusOK, w3.StatusCode)
254254
assert.Equal(t, w3.Header.Get("Content-Type"), "image/png")
255255

256-
w4 := performRequest("GET", "/swagger-ui.css", router)
257-
assert.Equal(t, 200, w4.StatusCode)
256+
w4 := performRequest(http.MethodGet, "/swagger-ui.css", router)
257+
assert.Equal(t, http.StatusOK, w4.StatusCode)
258258
assert.Equal(t, w4.Header.Get("Content-Type"), "text/css; charset=utf-8")
259259

260-
w5 := performRequest("GET", "/swagger-ui-bundle.js", router)
261-
assert.Equal(t, 200, w5.StatusCode)
260+
w5 := performRequest(http.MethodGet, "/swagger-ui-bundle.js", router)
261+
assert.Equal(t, http.StatusOK, w5.StatusCode)
262262
assert.Equal(t, w5.Header.Get("Content-Type"), fiber.MIMEApplicationJavaScript)
263263

264-
w6 := performRequest("GET", "/notfound", router)
265-
assert.Equal(t, 404, w6.StatusCode)
264+
assert.Equal(t, http.StatusNotFound, performRequest(http.MethodGet, "/notfound", router).StatusCode)
266265

267-
w7 := performRequest("GET", "/swagger/", router)
268-
assert.Equal(t, 301, w7.StatusCode)
266+
assert.Equal(t, 301, performRequest(http.MethodGet, "/swagger/", router).StatusCode)
269267
}
270268

271269
func performRequest(method, target string, e *fiber.App) *http.Response {
@@ -315,3 +313,19 @@ func TestPersistAuthorization(t *testing.T) {
315313
configFunc(&cfg)
316314
assert.Equal(t, expected, cfg.PersistAuthorization)
317315
}
316+
317+
func TestInstanceName(t *testing.T) {
318+
var cfg Config
319+
320+
assert.Equal(t, "", cfg.InstanceName)
321+
322+
expected := swag.Name
323+
configFunc := InstanceName(expected)
324+
configFunc(&cfg)
325+
assert.Equal(t, expected, cfg.InstanceName)
326+
327+
expected = "custom_name"
328+
configFunc = InstanceName(expected)
329+
configFunc(&cfg)
330+
assert.Equal(t, expected, cfg.InstanceName)
331+
}

0 commit comments

Comments
 (0)