From beaaaacafd1a7d4a4c2fd042829e6fc53fea6608 Mon Sep 17 00:00:00 2001 From: akash-akya Date: Fri, 22 Jun 2018 10:52:40 +0530 Subject: [PATCH 1/7] Basic implementation --- api.go | 14 +-- api_test.go | 25 +---- dashboard/index.html | 215 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 227 insertions(+), 27 deletions(-) create mode 100644 dashboard/index.html diff --git a/api.go b/api.go index 02edd9aa..96a88c8d 100644 --- a/api.go +++ b/api.go @@ -7,7 +7,6 @@ import ( "net" "net/http" "os" - "strings" "github.com/Shopify/toxiproxy/toxics" "github.com/gorilla/mux" @@ -49,11 +48,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/") { - http.Error(w, "User agent not allowed", 403) - } else { - h.ServeHTTP(w, r) - } + h.ServeHTTP(w, r) }) } @@ -73,6 +68,7 @@ func (server *ApiServer) Listen(host string, port string) { r.HandleFunc("/proxies/{proxy}/toxics/{toxic}", server.ToxicDelete).Methods("DELETE") r.HandleFunc("/version", server.Version).Methods("GET") + r.HandleFunc("/dashboard/", server.Dashboard).Methods("GET") http.Handle("/", StopBrowsersMiddleware(r)) @@ -389,6 +385,12 @@ func (server *ApiServer) Version(response http.ResponseWriter, request *http.Req } } +func (server *ApiServer) Dashboard(response http.ResponseWriter, request *http.Request) { + logrus.Info(request.URL.Path[1:]) + response.Header().Set("Content-Type", "text/html;charset=utf-8") + http.ServeFile(response, request, request.URL.Path[1:]) +} + type ApiError struct { Message string `json:"error"` StatusCode int `json:"status"` diff --git a/api_test.go b/api_test.go index 306c49bf..c12d731e 100644 --- a/api_test.go +++ b/api_test.go @@ -36,32 +36,15 @@ func WithServer(t *testing.T, f func(string)) { f("http://localhost:8475") } -func TestBrowserGets403(t *testing.T) { +func TestDashboardIsAccessible(t *testing.T) { WithServer(t, func(addr string) { client := http.Client{} - req, _ := http.NewRequest("GET", "http://localhost:8475/proxies", nil) - req.Header.Add("User-Agent", "Mozilla/5.0 (Linux; Android 4.4.2); Nexus 5 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Mobile Safari/537.36 OPR/20.0.1396.72047") - - resp, _ := client.Do(req) - - if resp.StatusCode != 403 { - t.Fatal("Browser-like UserAgent was not denied access to Toxiproxy") - } - }) -} - -func TestNonBrowserGets200(t *testing.T) { - WithServer(t, func(addr string) { - client := http.Client{} - - req, _ := http.NewRequest("GET", "http://localhost:8475/proxies", nil) - req.Header.Add("User-Agent", "Wget/2.1") - + req, _ := http.NewRequest("GET", "http://localhost:8475/dashboard/", nil) resp, _ := client.Do(req) - if resp.StatusCode == 403 { - t.Fatal("Non-Browser-like UserAgent was denied access to Toxiproxy") + if resp.StatusCode != 200 { + t.Fatal("Dashboard is not accessible at /dashboard") } }) } diff --git a/dashboard/index.html b/dashboard/index.html new file mode 100644 index 00000000..3129c159 --- /dev/null +++ b/dashboard/index.html @@ -0,0 +1,215 @@ + + + + My first Vue app + + + + + + + + + + + + +
+
+
    +
  • +
    +
    +
    +
    +
    + {{ proxy.name }} +
    + +
    + {{ proxy.listen }} +
    + +
    + {{ proxy.upstream }} +
    + +
    + {{ proxy.enabled }} +
    + +
    + + +
    +
    + + + + +
    +
    +
    +
  • +
+
+
+ + + + + + From 83be3e2b822bf398890fc3d1f290432100193233 Mon Sep 17 00:00:00 2001 From: akash-akya Date: Thu, 28 Jun 2018 16:23:48 +0530 Subject: [PATCH 2/7] Added rest commands & code cleanup --- .gitignore | 1 + Makefile | 2 + api.go | 32 +- api_test.go | 2 +- dashboard/index.html | 1121 ++++++++++++++++++++++++++++++++++++------ 5 files changed, 993 insertions(+), 165 deletions(-) diff --git a/.gitignore b/.gitignore index bb390eaf..e92da80c 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ cover*.out coverage.html *.deb tmp/ +statik/* \ No newline at end of file diff --git a/Makefile b/Makefile index cae6554e..fbd3f410 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,7 @@ COMBINED_GOPATH=$(GODEP_PATH):$(ORIGINAL_PATH) .PHONY: packages deb test linux darwin windows build: + statik -src=./dashboard GOPATH=$(COMBINED_GOPATH) go build -ldflags="-X github.com/Shopify/toxiproxy.Version=git-$(shell git rev-parse --short HEAD)" -o $(SERVER_NAME) ./cmd GOPATH=$(COMBINED_GOPATH) go build -ldflags="-X github.com/Shopify/toxiproxy.Version=git-$(shell git rev-parse --short HEAD)" -o $(CLI_NAME) ./cli @@ -26,6 +27,7 @@ clean: rm -f $(SERVER_NAME) rm -f $(CLI_NAME) rm -f *.deb + rm -f statik/* test: echo "Testing with" `go version` diff --git a/api.go b/api.go index 96a88c8d..ceda2889 100644 --- a/api.go +++ b/api.go @@ -8,21 +8,39 @@ import ( "net/http" "os" + _ "github.com/Shopify/toxiproxy/statik" "github.com/Shopify/toxiproxy/toxics" "github.com/gorilla/mux" + "github.com/rakyll/statik/fs" "github.com/sirupsen/logrus" ) type ApiServer struct { Collection *ProxyCollection + Dashboard http.File } func NewServer() *ApiServer { return &ApiServer{ Collection: NewProxyCollection(), + Dashboard: LoadDashboard(), } } +func LoadDashboard() http.File { + statikFS, e := fs.New() + if e != nil { + log.Fatal(e) + } + + file, e := statikFS.Open("/index.html") + if e != nil { + log.Fatal(e) + } + + return file +} + func (server *ApiServer) PopulateConfig(filename string) { file, err := os.Open(filename) if err != nil { @@ -68,7 +86,7 @@ func (server *ApiServer) Listen(host string, port string) { r.HandleFunc("/proxies/{proxy}/toxics/{toxic}", server.ToxicDelete).Methods("DELETE") r.HandleFunc("/version", server.Version).Methods("GET") - r.HandleFunc("/dashboard/", server.Dashboard).Methods("GET") + r.HandleFunc("/dashboard", server.ServeDashboard).Methods("GET") http.Handle("/", StopBrowsersMiddleware(r)) @@ -385,10 +403,16 @@ func (server *ApiServer) Version(response http.ResponseWriter, request *http.Req } } -func (server *ApiServer) Dashboard(response http.ResponseWriter, request *http.Request) { - logrus.Info(request.URL.Path[1:]) +func (server *ApiServer) ServeDashboard(response http.ResponseWriter, request *http.Request) { + f := server.Dashboard + + info, e := f.Stat() + if e != nil { + log.Fatal(e) + } + response.Header().Set("Content-Type", "text/html;charset=utf-8") - http.ServeFile(response, request, request.URL.Path[1:]) + http.ServeContent(response, request, info.Name(), info.ModTime(), f) } type ApiError struct { diff --git a/api_test.go b/api_test.go index c12d731e..5235ba6e 100644 --- a/api_test.go +++ b/api_test.go @@ -40,7 +40,7 @@ func TestDashboardIsAccessible(t *testing.T) { WithServer(t, func(addr string) { client := http.Client{} - req, _ := http.NewRequest("GET", "http://localhost:8475/dashboard/", nil) + req, _ := http.NewRequest("GET", "http://localhost:8475/dashboard", nil) resp, _ := client.Do(req) if resp.StatusCode != 200 { diff --git a/dashboard/index.html b/dashboard/index.html index 3129c159..48cc564d 100644 --- a/dashboard/index.html +++ b/dashboard/index.html @@ -1,215 +1,1016 @@ - My first Vue app + Toxiproxy + + - +
+ +
+
+
    +
    +
    +
    +
    + +
    + Name +
    -
    - {{ proxy.listen }} -
    +
    + Listen +
    -
    - {{ proxy.upstream }} -
    +
    + Upstream +
    -
    - {{ proxy.enabled }} -
    +
    + Enabled +
    -
    - - +
    + +
    +
    +
    + +
  • + + +
  • +
+
+ + + + + + + + +
+ + {{ message }} +
+ +
+ + + + + From 55e946494cc9245ced3542d53d7b9d2f14ceec57 Mon Sep 17 00:00:00 2001 From: akash-akya Date: Thu, 28 Jun 2018 16:29:55 +0530 Subject: [PATCH 3/7] Remove jquery. Use production Vue --- dashboard/index.html | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/dashboard/index.html b/dashboard/index.html index 48cc564d..9f94c8c7 100644 --- a/dashboard/index.html +++ b/dashboard/index.html @@ -6,10 +6,7 @@ - - - - +