forked from fabiolb/fabio
/
route.go
133 lines (112 loc) · 3.29 KB
/
route.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
package ui
import (
"html/template"
"net/http"
)
// RoutesHandler provides the UI for managing the routing table.
type RoutesHandler struct {
Color, Title, Version string
}
func (h *RoutesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
tmplRoutes.ExecuteTemplate(w, "routes", h)
}
var tmplRoutes = template.Must(template.New("routes").Parse(`
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>fabio{{if .Title}} - {{.Title}}{{end}}</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.3/css/materialize.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.3/js/materialize.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style type="text/css">
td.tags { display: none; }
.footer { padding-top: 10px; }
.logo { height: 32px; margin: 0 auto; display: block; }
@media (min-width: 78em) {
td.tags{ display: table-cell; }
}
</style>
</head>
<body>
<nav class="top-nav {{.Color}}">
<div class="container">
<div class="nav-wrapper">
<a href="/" class="brand-logo">fabio{{if .Title}} - {{.Title}}{{end}}</a>
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li><a href="/manual">Overrides</a></li>
<li><a href="https://github.com/fabiolb/fabio/blob/master/CHANGELOG.md">{{.Version}}</a></li>
<li><a href="https://github.com/fabiolb/fabio">Github</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="section">
<h5>Routing Table</h5>
<p><input type="text" id="filter" placeholder="type to filter routes"></p>
<table class="routes highlight"></table>
</div>
<div class="section footer">
<img class="logo" src="/logo.svg">
</div>
</div>
<script>
$(function(){
var params={};window.location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(str,key,value){params[key] = value;});
function renderRoutes(routes) {
var $table = $("table.routes");
var tbl = '<thead><tr>';
tbl += '<th>#</th>';
tbl += '<th>Service</th>';
tbl += '<th>Source</th>';
tbl += '<th>Dest</th>';
tbl += '<th>Options</th>';
tbl += '<th>Weight</th>';
tbl += '</tr></thead><tbody>'
tbl += '<tbody>'
for (var i=0; i < routes.length; i++) {
var r = routes[i];
tbl += '<tr>';
tbl += '<td>' + (i+1) + '</td>';
tbl += '<td>' + r.service + '</td>';
tbl += '<td>' + r.src + '</td>';
tbl += '<td>' + r.dst + '</td>';
tbl += '<td>' + r.opts + '</td>';
tbl += '<td>' + (r.weight * 100).toFixed(2) + '%</td>';
tbl += '</tr>';
}
tbl += '</tbody>';
$table.html(tbl);
}
var $filter = $('#filter');
function doFilter(v) {
$("tr").show();
if (!v) return;
var words = v.split(' ');
console.log('words: ', words);
for (var i=0; i < words.length; i++) {
var w = words[i].trim();
if (w == "") continue;
$("tbody tr:not(:contains('"+w+"'))").hide();
}
}
$filter.focus();
$filter.keyup(function() {
var v = $filter.val();
window.history.pushState(null, null, "?filter=" +v);
doFilter(v);
});
$.get("/api/routes", function(data) {
renderRoutes(data);
if (!params.filter) return;
var v = decodeURIComponent(params.filter);
$filter.val(v);
doFilter(v);
});
})
</script>
</body>
</html>
`))