-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.go
135 lines (113 loc) · 2.91 KB
/
http.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
package metalbond
import (
"encoding/json"
"fmt"
"net/http"
"sort"
"text/template"
"time"
yaml "gopkg.in/yaml.v2"
)
type jsonRoutes struct {
Date string `json:"date"`
VNet map[uint32]map[Destination][]NextHop `json:"vnet"`
MetalBondVersion string `json:"metalbondVersion"`
}
type jsonServer struct {
m *MetalBond
}
func serveJsonRouteTable(m *MetalBond, listen string) {
js := jsonServer{
m: m,
}
http.HandleFunc("/", js.mainHandler)
http.HandleFunc("/routes.json", js.jsonHandler)
http.HandleFunc("/routes.yaml", js.yamlHandler)
if err := http.ListenAndServe(listen, nil); err != nil {
if m != nil {
m.log().Errorf("Failed to list and serve: %v", err)
}
}
}
var METALBOND_VERSION string
func (j *jsonServer) getJsonRoutes() (jsonRoutes, error) {
js := jsonRoutes{
MetalBondVersion: METALBOND_VERSION,
Date: time.Now().Format("2006-01-02 15:04:05"),
VNet: make(map[uint32]map[Destination][]NextHop),
}
for _, vni := range j.m.routeTable.GetVNIs() {
js.VNet[uint32(vni)] = make(map[Destination][]NextHop)
for dst, hops := range j.m.routeTable.GetDestinationsByVNI(vni) {
js.VNet[uint32(vni)][dst] = append(js.VNet[uint32(vni)][dst], hops...)
}
}
for _, vnets := range js.VNet {
for _, hops := range vnets {
sort.Slice(hops, func(i, j int) bool {
return hops[i].String() < hops[j].String()
})
}
}
return js, nil
}
func (j *jsonServer) mainHandler(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.New("index.html").ParseFiles("html/index.html")
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Error: %v", err)
return
}
js, err := j.getJsonRoutes()
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Error: %v", err)
return
}
err = tmpl.Execute(w, js)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Error: %v", err)
return
}
}
func (j *jsonServer) jsonHandler(w http.ResponseWriter, r *http.Request) {
js, err := j.getJsonRoutes()
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Error: %v", err)
return
}
out, err := json.MarshalIndent(js, "", " ")
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Error: %v", err)
return
}
w.Header().Add("Content-Type", "application/json")
_, err = w.Write(out)
if err != nil {
fmt.Fprintf(w, "Error: %v", err)
}
}
func (j *jsonServer) yamlHandler(w http.ResponseWriter, r *http.Request) {
js, err := j.getJsonRoutes()
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Error: %v", err)
return
}
out, err := yaml.Marshal(js)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Error: %v", err)
return
}
w.Header().Add("Content-Type", "text/yaml")
_, err = w.Write(out)
if err != nil {
fmt.Fprintf(w, "Error: %v", err)
}
}