-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswagger-ui.go
80 lines (67 loc) · 1.84 KB
/
swagger-ui.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
//go:generate go-bindata -fs -prefix swagger-ui/dist/ -o bindata/bindata.go -pkg bindata swagger-ui/dist/...
package swaggerui
import (
"bytes"
"net/http"
"path"
"sync"
"go.skymeyer.dev/swagger-ui-bindata/bindata"
)
var (
indexPage = "index.html"
staticDir = "dist/"
embeddedSpec = "spec.json"
replacePath = "./"
replaceSpec = "https://petstore.swagger.io/v2/swagger.json"
)
// New createa a new SwaggerUI instance.
func New(opts ...Option) *SwaggerUI {
s := &SwaggerUI{
prefix: "/",
}
for _, opt := range opts {
opt(s)
}
return s
}
// SwaggerUI represents a bindata based swagger-ui website handler.
type SwaggerUI struct {
init sync.Once
index []byte
specEmbed []byte
specURL string
prefix string
}
// Handler returns the swagger-ui http.Handler
func (s *SwaggerUI) Handler() http.Handler {
mux := http.NewServeMux()
// Use swagger-ui-dist static files
bindataDir := path.Join(s.prefix, staticDir) + "/"
mux.Handle(bindataDir, http.StripPrefix(bindataDir, http.FileServer(bindata.AssetFile())))
// Embedded spec support
if s.specEmbed != nil {
mux.HandleFunc(path.Join(s.prefix, embeddedSpec), func(w http.ResponseWriter, r *http.Request) {
w.Write(s.specEmbed)
})
}
// Index handler
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Customize original index.html
s.init.Do(func() {
index := bindata.MustAsset(indexPage)
// Fixup resource paths
index = bytes.ReplaceAll(index, []byte(replacePath), []byte("./"+staticDir))
// Use embedded spec if requested
if s.specEmbed != nil {
index = bytes.ReplaceAll(index, []byte(replaceSpec), []byte("./"+embeddedSpec))
}
// Fallback to use spec by URL
if s.specURL != "" {
index = bytes.ReplaceAll(index, []byte(replaceSpec), []byte(s.specURL))
}
s.index = index
})
w.Write(s.index)
})
return mux
}