-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.go
40 lines (32 loc) · 770 Bytes
/
docs.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
package docs
import (
"embed"
httptemplate "html/template"
"net/http"
"github.com/gorilla/mux"
)
const (
apiFile = "/static/openapi.yml"
indexFile = "template/index.tpl"
)
//go:embed static
var Static embed.FS
//go:embed template
var template embed.FS
func RegisterOpenAPIService(appName string, rtr *mux.Router) {
rtr.Handle(apiFile, http.FileServer(http.FS(Static)))
rtr.HandleFunc("/", handler(appName))
}
// handler returns an http handler that servers OpenAPI console for an OpenAPI spec at specURL.
func handler(title string) http.HandlerFunc {
t, _ := httptemplate.ParseFS(template, indexFile)
return func(w http.ResponseWriter, req *http.Request) {
t.Execute(w, struct {
Title string
URL string
}{
title,
apiFile,
})
}
}