/
ui.go
116 lines (105 loc) · 2.58 KB
/
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package openapiui
import (
"bytes"
"net/http"
"net/url"
"os"
"time"
"github.com/wzshiming/go-bindata/fs"
"github.com/wzshiming/openapi/spec"
"github.com/wzshiming/openapi/util"
)
const index = "index.html"
// HandleWithFiles Replace the openapi file path
func HandleWithFiles(m map[string][]byte, asset func(path string) ([]byte, error)) http.Handler {
return HandleWith(func(path string) ([]byte, error) {
data, ok := m[path]
if ok {
return data, nil
}
return nil, os.ErrNotExist
}, asset)
}
// HandleWithFile Replace the openapi file path
func HandleWithFile(name string, data []byte, asset func(path string) ([]byte, error)) http.Handler {
return HandleWith(func(path string) ([]byte, error) {
if path == name {
return data, nil
}
return nil, os.ErrNotExist
}, asset)
}
// HandleWith Try the contents of openapi from f
func HandleWith(f, asset func(path string) ([]byte, error)) http.Handler {
fun := f
if fun == nil {
fun = asset
} else {
fun = func(path string) ([]byte, error) {
data, err := f(path)
if err == nil {
return data, nil
}
return asset(path)
}
}
afs := &fs.AssetFS{
Asset: fun,
Index: index,
}
return http.FileServer(afs)
}
// HandleURL Get the url address from the url parameter and modify the default address
func HandleURL(asset func(path string) ([]byte, error)) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
asset0 := func(path string) ([]byte, error) {
data, err := asset(path)
if err != nil {
return nil, err
}
if path == index {
srcs := r.URL.Query()["src"]
if len(srcs) != 0 {
val := url.Values{}
val["src"] = srcs
data = bytes.Replace(data, []byte("./openapi.json"), []byte("./openapi.json?"+val.Encode()), 1)
}
}
return data, nil
}
afs := &fs.AssetFS{
Asset: asset0,
Index: index,
}
http.FileServer(afs).ServeHTTP(w, r)
})
}
// HandleOpenapi Get the url address from the url parameter to combine it
func HandleOpenapi(w http.ResponseWriter, r *http.Request) {
srcs := r.URL.Query()["src"]
var root *spec.OpenAPI
for i, src := range srcs {
var api *spec.OpenAPI
file, err := util.ReadFile(src)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
err = util.Unmarshal(file, &api)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
if i == 0 {
root = api
} else {
util.Merge(root, api)
}
}
data, err := util.Marshal(root, util.JSON)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
http.ServeContent(w, r, "openapi.json", time.Now(), bytes.NewReader(data))
}