-
Notifications
You must be signed in to change notification settings - Fork 309
/
ui.go
138 lines (122 loc) · 3.23 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package ui
import (
"crypto/tls"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
responsewriter "github.com/rancher/apiserver/pkg/middleware"
"github.com/sirupsen/logrus"
"github.com/harvester/harvester/pkg/settings"
)
var (
insecureClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
Vue = newHandler(settings.UIIndex.Get,
settings.UIPath.Get,
settings.UISource.Get)
VueIndex = Vue.IndexFile()
)
func newHandler(
indexSetting func() string,
pathSetting func() string,
offlineSetting func() string) *handler {
return &handler{
indexSetting: indexSetting,
offlineSetting: offlineSetting,
pathSetting: pathSetting,
middleware: responsewriter.Chain{
responsewriter.Gzip,
responsewriter.FrameOptions,
responsewriter.CacheMiddleware("json", "js", "css"),
}.Handler,
indexMiddleware: responsewriter.Chain{
responsewriter.Gzip,
responsewriter.NoCache,
responsewriter.FrameOptions,
responsewriter.ContentType,
}.Handler,
}
}
type handler struct {
pathSetting func() string
indexSetting func() string
offlineSetting func() string
middleware func(http.Handler) http.Handler
indexMiddleware func(http.Handler) http.Handler
downloadOnce sync.Once
downloadSuccess bool
}
func (u *handler) canDownload(url string) bool {
u.downloadOnce.Do(func() {
if err := serveIndex(ioutil.Discard, url); err == nil {
u.downloadSuccess = true
} else {
logrus.Errorf("Failed to download %s, falling back to packaged UI", url)
}
})
return u.downloadSuccess
}
func (u *handler) path() (path string, isURL bool) {
switch u.offlineSetting() {
case "auto":
if settings.IsRelease() {
return u.pathSetting(), false
}
if u.canDownload(u.indexSetting()) {
return u.indexSetting(), true
}
return u.pathSetting(), false
case "bundled":
return u.pathSetting(), false
default:
return u.indexSetting(), true
}
}
func (u *handler) ServeAsset() http.Handler {
return u.middleware(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
http.FileServer(http.Dir(u.pathSetting())).ServeHTTP(rw, req)
}))
}
func (u *handler) IndexFileOnNotFound() http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
req.URL.Path = strings.TrimPrefix(req.URL.Path, "/dashboard")
if _, err := os.Stat(filepath.Join(u.pathSetting(), req.URL.Path)); err == nil {
u.ServeAsset().ServeHTTP(rw, req)
} else {
u.IndexFile().ServeHTTP(rw, req)
}
})
}
func (u *handler) IndexFile() http.Handler {
return u.indexMiddleware(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if path, isURL := u.path(); isURL {
_ = serveIndex(rw, path)
} else {
http.ServeFile(rw, req, filepath.Join(path, "index.html"))
}
}))
}
func serveIndex(resp io.Writer, url string) error {
r, err := insecureClient.Get(url)
if err != nil {
return err
}
defer r.Body.Close()
_, err = io.Copy(resp, r.Body)
return err
}
func (u *handler) PluginServeAsset() http.Handler {
return u.middleware(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
http.FileServer(http.Dir(u.pathSetting())).ServeHTTP(rw, req)
}))
}