-
Notifications
You must be signed in to change notification settings - Fork 16
/
handler.go
53 lines (43 loc) · 1.37 KB
/
handler.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
package swgui
import (
"html/template"
"net/http"
)
var staticServer = http.FileServer(assetFS())
// Handler handle swagger UI request
type Handler struct {
Title string // title of index file
SwaggerJSON string // path to swagger.json document specification
BasePath string // base path to docs
JsonEditor bool // Enable visual json editor support (experimental, can fail with complex schemas)
tpl *template.Template
staticServer http.Handler
}
// NewHandler returns a HTTP handler for swagger UI
func NewHandler(title, swaggerJSONPath string, basePath string) *Handler {
hdl := &Handler{
Title: title,
SwaggerJSON: swaggerJSONPath,
BasePath: basePath,
}
hdl.tpl, _ = template.New("index").Parse(indexTpl)
hdl.staticServer = http.StripPrefix(basePath, staticServer)
return hdl
}
// NewHandlerWithConfig returns a HTTP handler for swagger UI
func NewHandlerWithConfig(handler Handler) *Handler {
hdl := &handler
hdl.tpl, _ = template.New("index").Parse(indexTpl)
hdl.staticServer = http.StripPrefix(handler.BasePath, staticServer)
return hdl
}
// ServeHTTP implement http.Handler interface, to handle swagger UI request
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != h.BasePath {
h.staticServer.ServeHTTP(w, r)
return
}
if err := h.tpl.Execute(w, h); err != nil {
http.NotFound(w, r)
}
}