Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.15
require (
github.com/gofiber/fiber/v2 v2.32.0
github.com/stretchr/testify v1.7.0
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe
github.com/swaggo/files/v2 v2.0.0
github.com/swaggo/swag v1.8.1
github.com/valyala/fasthttp v1.36.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe h1:K8pHPVoTgxFJt1lXuIzzOX7zZhZFldJQK/CgKx9BFIc=
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe/go.mod h1:lKJPbtWzJ9JhsTN1k1gZgleJWY/cqq0psdoMmaThG3w=
github.com/swaggo/files/v2 v2.0.0 h1:hmAt8Dkynw7Ssz46F6pn8ok6YmGZqHSVLZ+HQM7i0kw=
github.com/swaggo/files/v2 v2.0.0/go.mod h1:24kk2Y9NYEJ5lHuCra6iVwkMjIekMCaFq/0JQj66kyM=
github.com/swaggo/swag v1.8.1 h1:JuARzFX1Z1njbCGz+ZytBR15TFJwF2Q7fu8puJHhQYI=
github.com/swaggo/swag v1.8.1/go.mod h1:ugemnJsPZm/kRwFUnzBlbHRd0JY9zE1M4F+uy2pAaPQ=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
Expand Down
22 changes: 6 additions & 16 deletions swagger.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package fiberSwagger

import (
"github.com/gofiber/fiber/v2"
swaggerFiles "github.com/swaggo/files/v2"
"github.com/swaggo/swag"
"github.com/valyala/fasthttp/fasthttpadaptor"
"html/template"
"net/http"
"path/filepath"
"regexp"
"sync"

"github.com/gofiber/fiber/v2"
swaggerFiles "github.com/swaggo/files"
"github.com/swaggo/swag"
"github.com/valyala/fasthttp/fasthttpadaptor"
)

// Config stores fiberSwagger configuration variables.
Expand Down Expand Up @@ -73,10 +71,6 @@ var WrapHandler = FiberWrapHandler()

// FiberWrapHandler wraps `http.Handler` into `fiber.Handler`.
func FiberWrapHandler(configFns ...func(c *Config)) fiber.Handler {
var once sync.Once

handler := swaggerFiles.Handler

config := Config{
URL: "doc.json",
DocExpansion: "list",
Expand All @@ -98,14 +92,10 @@ func FiberWrapHandler(configFns ...func(c *Config)) fiber.Handler {
matches := re.FindStringSubmatch(string(ctx.Request().URI().Path()))
path := matches[2]

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

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

case "index.html":
ctx.Type(fileExt[0:], "utf-8")
Expand All @@ -122,7 +112,7 @@ func FiberWrapHandler(configFns ...func(c *Config)) fiber.Handler {
ctx.Type(fileExt[0:], "utf-8")
return ctx.SendString(doc)
default:
fasthttpadaptor.NewFastHTTPHandler(handler)(ctx.Context())
fasthttpadaptor.NewFastHTTPHandler(http.FileServer(http.FS(swaggerFiles.FS)))(ctx.Context())

switch fileExt {
case ".css":
Expand Down