forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
75 lines (63 loc) · 1.37 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
package ui
import (
"io"
"net/http"
"os"
"path/filepath"
"strings"
"crypto/tls"
"github.com/rancher/norman/parse"
"github.com/rancher/rancher/pkg/settings"
"github.com/sirupsen/logrus"
)
var (
insecureClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
)
func Content() http.Handler {
return http.FileServer(http.Dir(settings.UIPath.Get()))
}
func UI(next http.Handler) http.Handler {
local := false
_, err := os.Stat(indexHTML())
if err == nil {
local = true
}
if local && !strings.HasPrefix(settings.ServerVersion.Get(), "v") {
local = false
}
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
if parse.IsBrowser(req, true) {
if local {
http.ServeFile(resp, req, indexHTML())
} else {
ui(resp, req)
}
} else {
next.ServeHTTP(resp, req)
}
})
}
func indexHTML() string {
return filepath.Join(settings.UIPath.Get(), "index.html")
}
func ui(resp http.ResponseWriter, req *http.Request) {
if err := serveIndex(resp, req); err != nil {
logrus.Errorf("failed to serve UI: %v", err)
resp.WriteHeader(500)
}
}
func serveIndex(resp http.ResponseWriter, req *http.Request) error {
r, err := insecureClient.Get(settings.UIIndex.Get())
if err != nil {
return err
}
defer r.Body.Close()
_, err = io.Copy(resp, r.Body)
return err
}