-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
53 lines (42 loc) · 941 Bytes
/
server.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
package main
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
v1 "k8s.io/api/core/v1"
)
type Server struct {
handler Handler
}
func (s *Server) Serve(handle func(h Handler) http.HandlerFunc) {
err := http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handle(s.handler)(w, r)
}))
if err != nil {
log.Fatal(err)
}
}
type Handler struct {
index string
}
func (h *Handler) ServeError(err error) http.HandlerFunc {
log.Print(err)
return func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Error occured: %q", err)
}
}
func (h *Handler) ServePods(pods []v1.Pod) http.HandlerFunc {
html, err := ioutil.ReadFile(h.index)
if err != nil {
return h.ServeError(err)
}
tpl, err := template.New("pods").Parse(string(html))
if err != nil {
return h.ServeError(err)
}
return func(w http.ResponseWriter, r *http.Request) {
tpl.Execute(w, pods)
}
}