This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
96 lines (83 loc) · 1.85 KB
/
main.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
package main
import (
"encoding/json"
"html/template"
"log"
"net/http"
"os"
"strings"
"github.com/Clever/mesos-visualizer/ecs"
)
var (
clusters map[string]string
)
func init() {
clusters = getEnvJSON("CLUSTERS")
}
func main() {
http.HandleFunc("/resources/", resourcesHandler)
http.HandleFunc("/", indexHandler)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
log.Print("Listening on port 80...")
log.Fatal(http.ListenAndServe(":80", nil))
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.New("home").Parse(`
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>ECS Visualizations</title>
</head>
<body>
{{ range $name, $arn := . }}
<h2>{{$name}}</h2>
<ul>
<li><a href="./static/sunburst.html?{{$name}}">Resource Utilization - Sunburst</a></li>
<li><a href="./static/treemap.html?{{$name}}">Resource Utilization - Treemap</a></li>
</ul>
{{end}}
</body>
`)
if err != nil {
panic(err)
}
err = tmpl.Execute(w, clusters)
if err != nil {
panic(err)
}
}
func resourcesHandler(w http.ResponseWriter, req *http.Request) {
cluster := strings.TrimPrefix(req.URL.Path, "/resources/")
arn, ok := clusters[cluster]
if !ok {
w.WriteHeader(404)
w.Write([]byte(`{"error": "unknown cluster"}`))
return
}
c := ecs.NewClient(arn)
resourceGraph, err := c.GetResourceGraph()
if err != nil {
log.Fatal(err)
}
js, err := json.Marshal(resourceGraph)
if err != nil {
log.Fatal(err)
}
w.Write(js)
}
func getEnv(envVar string) string {
val := os.Getenv(envVar)
if val == "" {
log.Fatalf("Must specify env variable %s", envVar)
}
return val
}
func getEnvJSON(envVar string) map[string]string {
data := getEnv(envVar)
var keyval map[string]string
err := json.Unmarshal([]byte(data), &keyval)
if err != nil {
log.Fatal(err.Error())
}
return keyval
}