This repository was archived by the owner on Feb 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathswagger.go
More file actions
134 lines (110 loc) · 2.51 KB
/
swagger.go
File metadata and controls
134 lines (110 loc) · 2.51 KB
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
package swagger
import (
"errors"
"fmt"
"html/template"
"io"
"io/fs"
"path"
"strings"
"sync"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/utils/v2"
swaggerFiles "github.com/swaggo/files/v2"
"github.com/swaggo/swag"
)
const (
defaultDocURL = "doc.json"
defaultIndex = "index.html"
)
var HandlerDefault = New()
// New returns custom handler
func New(config ...Config) fiber.Handler {
cfg := configDefault(config...)
index, err := template.New("swagger_index.html").Parse(indexTmpl)
if err != nil {
panic(fmt.Errorf("fiber: swagger middleware error -> %w", err))
}
var (
basePrefix string
once sync.Once
)
return func(c fiber.Ctx) error {
once.Do(func() {
basePrefix = strings.ReplaceAll(c.Route().Path, "*", "")
})
prefix := basePrefix
if forwardedPrefix := getForwardedPrefix(c); forwardedPrefix != "" {
prefix = forwardedPrefix + prefix
}
cfgCopy := cfg
if len(cfgCopy.URL) == 0 {
cfgCopy.URL = path.Join(prefix, defaultDocURL)
}
p := c.Path(utils.CopyString(c.Params("*")))
switch p {
case defaultIndex:
c.Type("html")
return index.Execute(c, cfgCopy)
case defaultDocURL:
var doc string
if doc, err = swag.ReadDoc(cfgCopy.InstanceName); err != nil {
return err
}
return c.Type("json").SendString(doc)
case "", "/":
return c.Redirect().Status(fiber.StatusMovedPermanently).To(path.Join(prefix, defaultIndex))
default:
filePath := path.Clean("/" + p)
filePath = strings.TrimPrefix(filePath, "/")
if filePath == "" {
return fiber.ErrNotFound
}
file, err := swaggerFiles.FS.Open(filePath)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return fiber.ErrNotFound
}
return err
}
defer file.Close()
info, err := file.Stat()
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return fiber.ErrNotFound
}
return err
}
if info.IsDir() {
return fiber.ErrNotFound
}
data, err := io.ReadAll(file)
if err != nil {
return err
}
if ext := strings.TrimPrefix(path.Ext(filePath), "."); ext != "" {
c.Type(ext)
}
return c.Send(data)
}
}
}
func getForwardedPrefix(c fiber.Ctx) string {
header := c.GetReqHeaders()["X-Forwarded-Prefix"]
if len(header) == 0 {
return ""
}
prefix := ""
for _, rawPrefix := range header {
endIndex := len(rawPrefix)
for endIndex > 1 && rawPrefix[endIndex-1] == '/' {
endIndex--
}
if endIndex != len(rawPrefix) {
prefix += rawPrefix[:endIndex]
} else {
prefix += rawPrefix
}
}
return prefix
}