forked from stefanprodan/podinfo
/
index.go
79 lines (68 loc) · 1.88 KB
/
index.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
package api
import (
"html/template"
"net/http"
"path"
"strings"
"fmt"
"os"
"io"
)
func extractFileName( path string ) ( string, error ) {
var err error
var fileName string
if len(path) > 1 && !strings.HasSuffix(path, "/") {
segments := strings.Split(path, "/")
fileName = segments[len(segments)-1]
} else {
err = fmt.Errorf( "path is a directory: %s", path )
}
fmt.Println( "extractFileName", fileName, err )
return fileName, err
}
// Index godoc
// @Summary Index
// @Description renders podinfo UI
// @Tags HTTP API
// @Produce html
// @Router / [get]
// @Success 200 {string} string "OK"
func (s *Server) indexHandler(w http.ResponseWriter, r *http.Request) {
_, span := s.tracer.Start(r.Context(), "indexHandler")
defer span.End()
fileName := "vue.html"
tmpl, err := template.New(fileName).ParseFiles(path.Join(s.config.UIPath, fileName))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(path.Join(s.config.UIPath, fileName) + err.Error()))
return
}
data := struct {
Title string
Logo string
}{
Title: s.config.Hostname,
Logo: s.config.UILogo,
}
if err := tmpl.Execute(w, data); err != nil {
http.Error(w, path.Join(s.config.UIPath, fileName)+err.Error(), http.StatusInternalServerError)
}
}
func (s *Server) assetHandler(w http.ResponseWriter, r *http.Request) {
_, span := s.tracer.Start(r.Context(), "assetHandler")
defer span.End()
fileName, err := extractFileName( r.URL.Path )
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(r.URL.Path + err.Error()))
return
}
source, err := os.Open(path.Join(s.config.UIPath, fileName))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(r.URL.Path + err.Error()))
return
}
defer source.Close()
io.Copy( w, source )
}