Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dashboard #194

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package toxiproxy

import (
"encoding/json"
"html/template"
"fmt"
"log"
"net"
Expand Down Expand Up @@ -49,7 +50,7 @@ func (server *ApiServer) PopulateConfig(filename string) {

func StopBrowsersMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.UserAgent(), "Mozilla/") {
if strings.HasPrefix(r.UserAgent(), "Mozilla/") && r.URL.Path != "/dashboard" {
http.Error(w, "User agent not allowed", 403)
} else {
h.ServeHTTP(w, r)
Expand All @@ -74,6 +75,8 @@ func (server *ApiServer) Listen(host string, port string) {

r.HandleFunc("/version", server.Version).Methods("GET")

r.HandleFunc("/dashboard", server.Dashboard).Methods("GET")

http.Handle("/", StopBrowsersMiddleware(r))

logrus.WithFields(logrus.Fields{
Expand Down Expand Up @@ -389,6 +392,16 @@ func (server *ApiServer) Version(response http.ResponseWriter, request *http.Req
}
}

func (server *ApiServer) Dashboard(response http.ResponseWriter, request *http.Request) {
var dashboardData []proxyToxics
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a decision early on to not use the existing HTTP API for these since we already have a couple of functions handy for fetching them.

for _, proxyName := range server.Collection.Proxies() {
dashboardData = append(dashboardData, proxyWithToxics(proxyName))
}

tmpl := template.Must(template.ParseFiles("templates/dashboard.tmpl"))
tmpl.Execute(response, dashboardData)
}

type ApiError struct {
Message string `json:"error"`
StatusCode int `json:"status"`
Expand Down
13 changes: 13 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ func WithServer(t *testing.T, f func(string)) {
f("http://localhost:8475")
}

func TestDashboardIsAccessible(t *testing.T) {
WithServer(t, func(addr string) {
client := http.Client{}

req, _ := http.NewRequest("GET", "http://localhost:8475/dashboard", nil)
resp, _ := client.Do(req)

if resp.StatusCode != 200 {
t.Fatal("Dashboard is not accessible at /dashboard")
}
})
}

func TestBrowserGets403(t *testing.T) {
WithServer(t, func(addr string) {
client := http.Client{}
Expand Down
55 changes: 55 additions & 0 deletions templates/dashboard.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<table>
{{ range . }}
<tr>
<td><strong>{{ .Proxy.Name }}</strong> listen: {{ .Proxy.Listen }} upstream: {{ .Proxy.Upstream }}
Enabled? <input type="checkbox" {{ if .Proxy.Enabled }} checked{{ end }} /></td>
</tr>
{{ range .Toxics }}
<tr>
<td>
name: {{ .Name }}
</td><td>
type: {{ .Type }}
</td><td>
stream: {{ .Stream }}
</td><td>
toxicity: {{ .Toxicity }}
</td><td>
direction: {{ .Direction }}
</td><td>
index: {{ .Index }}
</td><td>
buffer: {{ .BufferSize }}
</td><td>
</td><td>
{{ if eq .Type "latency" }}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these if blocks will probably go away once the updating work comes into it.

jitter: <input type="text" name="jitter" value="{{ .Toxic.Jitter }}" size="6" />ms<br>
latency: <input type="text" name="latency" value="{{ .Toxic.Latency}}" size="6" />ms
{{ end }}

{{ if eq .Type "bandwidth" }}
rate: <input type="text" name="bandwidth_rate" value="{{ .Toxic.Rate }}" size="6" />KB/s
{{ end }}

{{ if eq .Type "limit_data" }}
bytes: <input type="text" name="limit_data_bytes" value="{{ .Toxic.Bytes }}" size="6" /> bytes
{{ end }}

{{ if eq .Type "slicer" }}
average size: <input type="text" name="slicer_average_size" value="{{ .Toxic.AverageSize }}" size="6" /> bytes<br>
size variation: <input type="text" name="slicer_size_variation" value="{{ .Toxic.SizeVariation }}" size="6" /> bytes<br>
delay: <input type="text" name="slicer_delay" value="{{ .Toxic.Delay }}" size="6" /> microseconds
{{ end }}

{{ if eq .Type "slow_close" }}
delay: <input type="text" name="slow_close_delay" value="{{ .Toxic.Delay }}" size="6" />ms
{{ end }}

{{ if eq .Type "timeout" }}
timeout: <input type="text" name="timeout" value="{{ .Toxic.Timeout }}" size="6" />ms
{{ end }}
</td>
{{ end }}
{{ end }}
</tr>
</table>