forked from flowchartsman/swaggerui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswaggerui.go
36 lines (29 loc) · 926 Bytes
/
swaggerui.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
package swaggerui
import (
"embed"
"io/fs"
"net/http"
"github.com/labstack/echo/v4"
)
//go:generate go run generate.go
//go:embed embed
var swagfs embed.FS
func byteHandler(b []byte) http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
w.Write(b)
}
}
// Handler returns a handler that will serve a self-hosted Swagger UI with your spec embedded
func Handler(spec []byte) http.Handler {
// render the index template with the proper spec name inserted
static, _ := fs.Sub(swagfs, "embed")
mux := http.NewServeMux()
mux.HandleFunc("/swagger_spec", byteHandler(spec))
mux.Handle("/", http.FileServer(http.FS(static)))
return mux
}
// EchoHandler returns an echo.HandlerFunc that will serve a self-hosted Swagger UI with your spec embedded
func EchoHandler(strip string, spec []byte) echo.HandlerFunc {
handler := Handler(spec)
return echo.WrapHandler(http.StripPrefix(strip, handler))
}