-
Notifications
You must be signed in to change notification settings - Fork 0
/
swaggerui.go
61 lines (54 loc) · 1.64 KB
/
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
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
package swaggerui
import (
"net/http"
"regexp"
"strings"
"github.com/getkin/kin-openapi/openapi3"
"github.com/go-chi/chi"
"github.com/opencredo/venafi-cloud-ab-poc/go/internal/pkg/ledgerserver"
"github.com/rakyll/statik/fs"
)
func Handler() http.Handler {
s, _ := ledgerserver.GetSwagger()
r := chi.NewMux()
r.MethodFunc(http.MethodGet, "/swaggerui/swagger.json", func(w http.ResponseWriter, req *http.Request) {
serviceName := req.Header.Get("X-Ocvab-Service")
namespace := req.Header.Get("X-Ocvab-Namespace")
url := "/"
if serviceName != "" && namespace != "" {
url = "/" + namespace + "/" + serviceName + "/"
}
s.Servers = []*openapi3.Server{
{
URL: url,
Description: "This service",
Variables: s.Servers[0].Variables,
},
}
s.AddServer(&openapi3.Server{})
b, _ := s.MarshalJSON()
w.Write(b)
})
fs, _ := fs.New()
fileServer(r, fs)
return r
}
// FileServer conveniently sets up a http.FileServer handler to serve
// static files from a http.FileSystem.
func fileServer(r chi.Router, root http.FileSystem) {
r.Get("/*", func(w http.ResponseWriter, req *http.Request) {
isFileName, _ := regexp.MatchString(`^.*/[^./]*\..*[^/]$`, req.RequestURI)
if isFileName || strings.HasSuffix(req.RequestURI, "/") {
rctx := chi.RouteContext(req.Context())
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
fs := http.StripPrefix(pathPrefix, http.FileServer(root))
fs.ServeHTTP(w, req)
} else {
redirectDest := req.Header.Get("X-Original-Uri")
if redirectDest == "" {
redirectDest = req.RequestURI
}
http.Redirect(w, req, redirectDest+"/", http.StatusTemporaryRedirect)
}
})
}