/
ui.go
114 lines (100 loc) · 2.52 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
package ui
import (
"crypto/tls"
"io"
"io/fs"
"net/http"
"os"
"path"
"strings"
)
const (
localUI = "./static"
uiDefaultIndex = "https://autok3s-ui.s3-ap-southeast-2.amazonaws.com/static/index.html"
)
var (
insecureClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
)
type fsFunc func(name string) (fs.File, error)
func (f fsFunc) Open(name string) (fs.File, error) {
return f(name)
}
// Serve serve ui component.
func Serve() http.Handler {
if uiMode() == "dev" {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
_ = serveIndex(writer)
})
}
return serveAsset()
}
// ServeNotFound server ui component, not found will be return index.html.
func ServeNotFound(next http.Handler) http.Handler {
if uiMode() == "dev" {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
_ = serveIndex(writer)
})
}
return serveAssetNotFound(next)
}
func ServeJavascript(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
// This is used to enforce application/javascript MIME on Windows (https://github.com/cnrancher/autok3s/issues/426)
// refer to: https://github.com/golang/go/issues/32350
if strings.HasSuffix(r.URL.Path, ".js") {
rw.Header().Set("Content-Type", "application/javascript")
}
next.ServeHTTP(rw, r)
})
}
func serveAsset() http.Handler {
handler := fsFunc(func(name string) (fs.File, error) {
assetPath := path.Join(localUI, name)
file, err := assets.Open(assetPath)
if err != nil {
return nil, err
}
return file, err
})
return http.FileServer(http.FS(handler))
}
func serveAssetNotFound(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
p := strings.TrimPrefix(req.URL.Path, "/ui/")
_, err := assets.Open(path.Join(localUI, p))
if err != nil && os.IsNotExist(err) {
f, _ := assets.Open(path.Join(localUI, "index.html"))
_, _ = io.Copy(rw, f)
_ = f.Close()
return
}
next.ServeHTTP(rw, req)
})
}
func serveIndex(resp io.Writer) error {
uiIndex := os.Getenv("AUTOK3S_UI_INDEX")
if uiIndex == "" {
uiIndex = uiDefaultIndex
}
r, err := insecureClient.Get(uiIndex)
if err != nil {
return err
}
defer r.Body.Close()
_, err = io.Copy(resp, r.Body)
return err
}
func uiMode() string {
mode := os.Getenv("AUTOK3S_UI_MODE")
if mode == "" {
return DefaultMode
}
return mode
}