1
// Copyright Istio Authors
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
26
"github.com/gorilla/mux"
28
"istio.io/istio/pkg/ctrlz/assets"
29
"istio.io/istio/pkg/ctrlz/fw"
32
var mimeTypes = map[string]string{
33
".css": "text/css; charset=utf-8",
34
".svg": "image/svg+xml; charset=utf-8",
35
".ico": "image/x-icon",
37
".js": "application/javascript",
49
func getHomeInfo() *homeInfo {
50
var ms runtime.MemStats
51
runtime.ReadMemStats(&ms)
53
hostName, _ := os.Hostname()
56
ProcessName: os.Args[0],
57
HeapSize: ms.HeapAlloc,
59
CurrentTime: time.Now().UnixNano(),
65
func registerHome(router *mux.Router, layout *template.Template) {
66
homeTmpl := assets.ParseTemplate(template.Must(layout.Clone()), "templates/home.html")
67
errorTmpl := assets.ParseTemplate(template.Must(layout.Clone()), "templates/404.html")
69
_ = router.NewRoute().PathPrefix("/").Methods("GET").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
70
if req.URL.Path == "/" {
72
fw.RenderHTML(w, homeTmpl, getHomeInfo())
73
} else if req.URL.Path == "/homej" || req.URL.Path == "/homej/" {
74
fw.RenderJSON(w, http.StatusOK, getHomeInfo())
75
} else if a, err := assets.FS.ReadFile("static" + req.URL.Path); err == nil {
77
ext := strings.ToLower(filepath.Ext(req.URL.Path))
78
if mime, ok := mimeTypes[ext]; ok {
79
w.Header().Set("Content-Type", mime)
84
w.WriteHeader(http.StatusNotFound)
85
fw.RenderHTML(w, errorTmpl, nil)
89
_ = router.NewRoute().Methods("PUT").Path("/homej/exit").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
90
w.WriteHeader(http.StatusAccepted)
91
time.AfterFunc(1*time.Second, func() {