Skip to content

Commit

Permalink
feat: add instance name (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubogdan authored Apr 27, 2022
1 parent ff22574 commit 4115b38
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 39 deletions.
56 changes: 33 additions & 23 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (
type Config struct {
// The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `mockedSwag.json`.
URL string
DeepLinking bool
InstanceName string
DocExpansion string
DomID string
DeepLinking bool
PersistAuthorization bool
}

Expand Down Expand Up @@ -51,8 +52,16 @@ func DomID(domID string) func(c *Config) {
}
}

// If set to true, it persists authorization data and it would not be lost on browser close/refresh
// Defaults to false
// InstanceName set the instance name that was used to generate the swagger documents
// Defaults to swag.Name ("swagger").
func InstanceName(name string) func(*Config) {
return func(c *Config) {
c.InstanceName = name
}
}

// PersistAuthorization Persist authorization information over browser close/refresh.
// Defaults to false.
func PersistAuthorization(persistAuthorization bool) func(c *Config) {
return func(c *Config) {
c.PersistAuthorization = persistAuthorization
Expand All @@ -66,59 +75,60 @@ var WrapHandler = FiberWrapHandler()
func FiberWrapHandler(configFns ...func(c *Config)) fiber.Handler {
var once sync.Once

h := swaggerFiles.Handler
handler := swaggerFiles.Handler

config := &Config{
config := Config{
URL: "doc.json",
DeepLinking: true,
DocExpansion: "list",
DomID: "#swagger-ui",
InstanceName: swag.Name,
DeepLinking: true,
}

for _, configFn := range configFns {
configFn(config)
configFn(&config)
}

// create a template with name
t := template.New("swagger_index.html")
index, _ := t.Parse(indexTemplate)
index, _ := template.New("swagger_index.html").Parse(indexTemplate)

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

return func(c *fiber.Ctx) error {
matches := re.FindStringSubmatch(string(c.Request().URI().Path()))
return func(ctx *fiber.Ctx) error {
matches := re.FindStringSubmatch(string(ctx.Request().URI().Path()))
path := matches[2]

once.Do(func() {
h.Prefix = matches[1]
handler.Prefix = matches[1]
})

fileExt := filepath.Ext(path)
switch path {
case "":
return c.Redirect(filepath.Join(h.Prefix, "index.html"), fiber.StatusMovedPermanently)
return ctx.Redirect(filepath.Join(handler.Prefix, "index.html"), fiber.StatusMovedPermanently)

case "index.html":
c.Type(fileExt[0:], "utf-8")
return index.Execute(c, config)
ctx.Type(fileExt[0:], "utf-8")

return index.Execute(ctx, config)
case "doc.json":
doc, err := swag.ReadDoc()
doc, err := swag.ReadDoc(config.InstanceName)
if err != nil {
_, err := c.Status(http.StatusInternalServerError).WriteString(http.StatusText(http.StatusInternalServerError))
_, err := ctx.Status(http.StatusInternalServerError).WriteString(http.StatusText(http.StatusInternalServerError))

return err
}

c.Type(fileExt[0:], "utf-8")
return c.SendString(doc)
ctx.Type(fileExt[0:], "utf-8")
return ctx.SendString(doc)
default:
handler := fasthttpadaptor.NewFastHTTPHandler(h)
handler(c.Context())
fasthttpadaptor.NewFastHTTPHandler(handler)(ctx.Context())

switch fileExt {
case ".css":
c.Type(fileExt[0:], "utf-8")
ctx.Type(fileExt[0:], "utf-8")
case ".png", ".js":
c.Type(fileExt[0:])
ctx.Type(fileExt[0:])
}

return nil
Expand Down
46 changes: 30 additions & 16 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,35 +237,33 @@ func TestWrapHandler(t *testing.T) {

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

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

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

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

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

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

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

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

w7 := performRequest("GET", "/swagger/", router)
assert.Equal(t, 301, w7.StatusCode)
assert.Equal(t, 301, performRequest(http.MethodGet, "/swagger/", router).StatusCode)
}

func performRequest(method, target string, e *fiber.App) *http.Response {
Expand Down Expand Up @@ -315,3 +313,19 @@ func TestPersistAuthorization(t *testing.T) {
configFunc(&cfg)
assert.Equal(t, expected, cfg.PersistAuthorization)
}

func TestInstanceName(t *testing.T) {
var cfg Config

assert.Equal(t, "", cfg.InstanceName)

expected := swag.Name
configFunc := InstanceName(expected)
configFunc(&cfg)
assert.Equal(t, expected, cfg.InstanceName)

expected = "custom_name"
configFunc = InstanceName(expected)
configFunc(&cfg)
assert.Equal(t, expected, cfg.InstanceName)
}

0 comments on commit 4115b38

Please sign in to comment.