blob: afdd04b3e9fd9f0ea3c8eac977f6caec03e2bbd8 [file] [log] [blame]
Serge Bazanski8d7843c2018-10-04 10:37:36 +01001package main
2
Serge Bazanski46765082018-10-06 12:32:01 +01003import (
Serge Bazanskic7be4a12018-10-06 13:18:05 +01004 "context"
5 "fmt"
Serge Bazanski46765082018-10-06 12:32:01 +01006 "net/http"
Serge Bazanski8d7843c2018-10-04 10:37:36 +01007
Serge Bazanski46765082018-10-06 12:32:01 +01008 "code.hackerspace.pl/q3k/topo/graph"
Serge Bazanskic7be4a12018-10-06 13:18:05 +01009 "github.com/gobuffalo/packr"
Serge Bazanski46765082018-10-06 12:32:01 +010010 "github.com/golang/glog"
Serge Bazanskic7be4a12018-10-06 13:18:05 +010011 "github.com/q3k/statusz"
Serge Bazanski46765082018-10-06 12:32:01 +010012)
13
14type ServiceConfig struct {
15 DebugListen string
16}
17
18type Service struct {
19 gr *graph.Graph
20 config ServiceConfig
21}
22
23func NewService(gr *graph.Graph, c ServiceConfig) *Service {
24 return &Service{
25 gr: gr,
26 config: c,
27 }
28}
29
Serge Bazanskic7be4a12018-10-06 13:18:05 +010030type TopologyStatus struct {
31}
32
33const topologyFragment = `
34 <script src="/assets/viz.js"></script>
35 <script>
36
37 var viz = new Viz({ workerURL: '/assets/full.render.js' });
38 var xmlhttp = new XMLHttpRequest();
39 xmlhttp.onreadystatechange = function() {
40 if (this.readyState == 4 && this.status == 200) {
41 var dot = this.responseText;
42 viz.renderSVGElement(dot)
43 .then(function(element) {
44 document.body.appendChild(element);
45 });
46 }
47 };
48 xmlhttp.open('GET', '/debug/graphviz');
49 xmlhttp.send();
50
51 </script>
52`
53
Serge Bazanski46765082018-10-06 12:32:01 +010054func (s *Service) Run() {
Serge Bazanskic7be4a12018-10-06 13:18:05 +010055 assets := packr.NewBox("./assets")
56 http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(assets)))
57
Serge Bazanski46765082018-10-06 12:32:01 +010058 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
59 http.Redirect(w, r, "/debug/status", http.StatusSeeOther)
60 })
Serge Bazanskic7be4a12018-10-06 13:18:05 +010061
62 http.HandleFunc("/debug/graphviz", func(w http.ResponseWriter, r *http.Request) {
63 fmt.Fprintf(w, "graph G {\n")
64 for _, machine := range s.gr.Machines {
65 fmt.Fprintf(w, " subgraph cluster%s {\n", machine.Name)
66 fmt.Fprintf(w, " label = %s\n", machine.Name)
67 for _, port := range machine.Ports {
68 a := machine.Name + "|" + port.Name
69 fmt.Fprintf(w, " %q [label = %q]\n", a, port.Name)
70 }
71 fmt.Fprintf(w, " }\n")
72 }
73 for _, sw := range s.gr.Switches {
74 fmt.Fprintf(w, " subgraph cluster%s {\n", sw.Name)
75 fmt.Fprintf(w, " label = %s\n", sw.Name)
76 for _, port := range sw.Ports {
77 a := sw.Name + "|" + port.Name
78 fmt.Fprintf(w, " %q [label = %q]\n", a, port.Name)
79 }
80 fmt.Fprintf(w, " }\n")
81 }
82 for _, machine := range s.gr.Machines {
83 for _, port := range machine.Ports {
84 if port.OtherEnd == nil {
85 continue
86 }
87 a := machine.Name + "|" + port.Name
88 b := port.OtherEnd.Switch.Name + "|" + port.OtherEnd.Name
89 fmt.Fprintf(w, " %q -- %q\n", a, b)
90 }
91 }
92 fmt.Fprintf(w, "}\n")
93 })
94
95 statusz.AddStatusPart("Topology", topologyFragment, func(ctx context.Context) interface{} {
96 return &TopologyStatus{}
97 })
Serge Bazanski46765082018-10-06 12:32:01 +010098 glog.Infof("Debug listening on %s....", s.config.DebugListen)
99 http.ListenAndServe(s.config.DebugListen, nil)
Serge Bazanski8d7843c2018-10-04 10:37:36 +0100100}