From effddfd9387fc70fbbef31ab6c8482bcf78b9b62 Mon Sep 17 00:00:00 2001 From: cmfatih Date: Thu, 13 Aug 2015 21:51:03 -0400 Subject: [PATCH 01/12] Add JSONP support for monitoring routes --- server/monitor.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/server/monitor.go b/server/monitor.go index d0be11ec..6927789c 100644 --- a/server/monitor.go +++ b/server/monitor.go @@ -141,7 +141,12 @@ func (s *Server) HandleConnz(w http.ResponseWriter, r *http.Request) { Errorf("Error marshalling response to /connz request: %v", err) } w.Header().Set("Content-Type", "application/json") - w.Write(b) + callback := r.FormValue("callback") + if callback != "" { + fmt.Fprintf(w, "%s(%s)", callback, b) + } else { + w.Write(b) + } } func castToSliceString(input []interface{}) []string { @@ -221,7 +226,12 @@ func (s *Server) HandleRoutez(w http.ResponseWriter, r *http.Request) { Errorf("Error marshalling response to /routez request: %v", err) } w.Header().Set("Content-Type", "application/json") - w.Write(b) + callback := r.FormValue("callback") + if callback != "" { + fmt.Fprintf(w, "%s(%s)", callback, b) + } else { + w.Write(b) + } } // HandleStats process HTTP requests for subjects stats. @@ -233,7 +243,12 @@ func (s *Server) HandleSubsz(w http.ResponseWriter, r *http.Request) { Errorf("Error marshalling response to /subscriptionsz request: %v", err) } w.Header().Set("Content-Type", "application/json") - w.Write(b) + callback := r.FormValue("callback") + if callback != "" { + fmt.Fprintf(w, "%s(%s)", callback, b) + } else { + w.Write(b) + } } // Varz will output server information on the monitoring port at /varz. @@ -323,7 +338,12 @@ func (s *Server) HandleVarz(w http.ResponseWriter, r *http.Request) { Errorf("Error marshalling response to /varz request: %v", err) } w.Header().Set("Content-Type", "application/json") - w.Write(b) + callback := r.FormValue("callback") + if callback != "" { + fmt.Fprintf(w, "%s(%s)", callback, b) + } else { + w.Write(b) + } } // Grab RSS and PCPU From b641b9430013fab94521889698b9b87578c0863f Mon Sep 17 00:00:00 2001 From: cmfatih Date: Sat, 15 Aug 2015 17:29:37 -0400 Subject: [PATCH 02/12] Add ResponseHandler --- server/monitor.go | 56 +++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/server/monitor.go b/server/monitor.go index 6927789c..80bbbc42 100644 --- a/server/monitor.go +++ b/server/monitor.go @@ -140,13 +140,9 @@ func (s *Server) HandleConnz(w http.ResponseWriter, r *http.Request) { if err != nil { Errorf("Error marshalling response to /connz request: %v", err) } - w.Header().Set("Content-Type", "application/json") - callback := r.FormValue("callback") - if callback != "" { - fmt.Fprintf(w, "%s(%s)", callback, b) - } else { - w.Write(b) - } + + // Handle response + ResponseHandler(w, r, b); } func castToSliceString(input []interface{}) []string { @@ -225,13 +221,9 @@ func (s *Server) HandleRoutez(w http.ResponseWriter, r *http.Request) { if err != nil { Errorf("Error marshalling response to /routez request: %v", err) } - w.Header().Set("Content-Type", "application/json") - callback := r.FormValue("callback") - if callback != "" { - fmt.Fprintf(w, "%s(%s)", callback, b) - } else { - w.Write(b) - } + + // Handle response + ResponseHandler(w, r, b); } // HandleStats process HTTP requests for subjects stats. @@ -242,13 +234,9 @@ func (s *Server) HandleSubsz(w http.ResponseWriter, r *http.Request) { if err != nil { Errorf("Error marshalling response to /subscriptionsz request: %v", err) } - w.Header().Set("Content-Type", "application/json") - callback := r.FormValue("callback") - if callback != "" { - fmt.Fprintf(w, "%s(%s)", callback, b) - } else { - w.Write(b) - } + + // Handle response + ResponseHandler(w, r, b); } // Varz will output server information on the monitoring port at /varz. @@ -337,13 +325,9 @@ func (s *Server) HandleVarz(w http.ResponseWriter, r *http.Request) { if err != nil { Errorf("Error marshalling response to /varz request: %v", err) } - w.Header().Set("Content-Type", "application/json") - callback := r.FormValue("callback") - if callback != "" { - fmt.Fprintf(w, "%s(%s)", callback, b) - } else { - w.Write(b) - } + + // Handle response + ResponseHandler(w, r, b); } // Grab RSS and PCPU @@ -357,3 +341,19 @@ func updateUsage(v *Varz) { v.CPU = pcpu v.Cores = numCores } + +// ResponseHandler handles responses for monitoring routes +func ResponseHandler(w http.ResponseWriter, r *http.Request, data []byte) { + // Get callback from request + callback := r.FormValue("callback") + // If callback is not empty then + if callback != "" { + // Response for JSONP + w.Header().Set("Content-Type", "application/javascript") + fmt.Fprintf(w, "%s(%s)", callback, data) + } else { + // Otherwise JSON + w.Header().Set("Content-Type", "application/json") + w.Write(data) + } +} From 001d42b1ea808e83c5d9489e5e25f63dbc1661e0 Mon Sep 17 00:00:00 2001 From: cmfatih Date: Sat, 15 Aug 2015 17:39:35 -0400 Subject: [PATCH 03/12] Tidy change for HandleRoot --- server/monitor.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/server/monitor.go b/server/monitor.go index 80bbbc42..9c25564e 100644 --- a/server/monitor.go +++ b/server/monitor.go @@ -290,16 +290,16 @@ func myUptime(d time.Duration) string { // HandleRoot will show basic info and links to others handlers. func (s *Server) HandleRoot(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, "gnatsd monitoring

") - vlink := fmt.Sprintf("http://%s/varz", r.Host) - fmt.Fprintf(w, "%s
", vlink, vlink) - clink := fmt.Sprintf("http://%s/connz", r.Host) - fmt.Fprintf(w, "%s
", clink, clink) - rlink := fmt.Sprintf("http://%s/routez", r.Host) - fmt.Fprintf(w, "%s
", rlink, rlink) - slink := fmt.Sprintf("http://%s/subscriptionsz", r.Host) - fmt.Fprintf(w, "%s
", slink, slink) - fmt.Fprint(w, "") + fmt.Fprintf(w, ` + + gnatsd monitoring +

+ http://%s/varz
+ http://%s/connz
+ http://%s/routez
+ http://%s/subscriptionsz
+ + `, r.Host, r.Host, r.Host, r.Host, r.Host, r.Host, r.Host, r.Host); } // HandleVarz will process HTTP requests for server information. From 7c07d5994c83c44fcb2f483b26cda3d328b3b725 Mon Sep 17 00:00:00 2001 From: cmfatih Date: Sat, 15 Aug 2015 17:44:37 -0400 Subject: [PATCH 04/12] Updates due to formting --- server/monitor.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/server/monitor.go b/server/monitor.go index 9c25564e..cb39bb66 100644 --- a/server/monitor.go +++ b/server/monitor.go @@ -142,7 +142,7 @@ func (s *Server) HandleConnz(w http.ResponseWriter, r *http.Request) { } // Handle response - ResponseHandler(w, r, b); + ResponseHandler(w, r, b) } func castToSliceString(input []interface{}) []string { @@ -223,7 +223,7 @@ func (s *Server) HandleRoutez(w http.ResponseWriter, r *http.Request) { } // Handle response - ResponseHandler(w, r, b); + ResponseHandler(w, r, b) } // HandleStats process HTTP requests for subjects stats. @@ -236,7 +236,7 @@ func (s *Server) HandleSubsz(w http.ResponseWriter, r *http.Request) { } // Handle response - ResponseHandler(w, r, b); + ResponseHandler(w, r, b) } // Varz will output server information on the monitoring port at /varz. @@ -299,7 +299,7 @@ func (s *Server) HandleRoot(w http.ResponseWriter, r *http.Request) { http://%s/routez
http://%s/subscriptionsz
- `, r.Host, r.Host, r.Host, r.Host, r.Host, r.Host, r.Host, r.Host); + `, r.Host, r.Host, r.Host, r.Host, r.Host, r.Host, r.Host, r.Host) } // HandleVarz will process HTTP requests for server information. @@ -327,7 +327,7 @@ func (s *Server) HandleVarz(w http.ResponseWriter, r *http.Request) { } // Handle response - ResponseHandler(w, r, b); + ResponseHandler(w, r, b) } // Grab RSS and PCPU From 5cf02d76dcb4ed33acac6b0251c1cbf6fe456f43 Mon Sep 17 00:00:00 2001 From: cmfatih Date: Sat, 15 Aug 2015 18:09:47 -0400 Subject: [PATCH 05/12] Add tests for content-type --- server/monitor_test.go | 107 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/server/monitor_test.go b/server/monitor_test.go index 8909a6f4..1d2d8735 100644 --- a/server/monitor_test.go +++ b/server/monitor_test.go @@ -151,6 +151,33 @@ func TestVarz(t *testing.T) { if v.OutBytes != 5 { t.Fatalf("Expected OutBytes of 5, got %v\n", v.OutBytes) } + + // Test JSON and JSONP + respJSON, errJSON := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "varz") + ct := respJSON.Header.Get("Content-Type"); + if errJSON != nil { + t.Fatalf("Expected no error: Got %v\n", errJSON) + } + if respJSON.StatusCode != 200 { + t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) + } + if ct != "application/json" { + t.Fatalf("Expected a 200 response, got %s\n", ct) + } + defer respJSON.Body.Close() + + respJSON, errJSON = http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "varz?callback=callback") + ct = respJSON.Header.Get("Content-Type"); + if errJSON != nil { + t.Fatalf("Expected no error: Got %v\n", errJSON) + } + if respJSON.StatusCode != 200 { + t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) + } + if ct != "application/javascript" { + t.Fatalf("Expected a 200 response, got %s\n", ct) + } + defer respJSON.Body.Close() } func TestConnz(t *testing.T) { @@ -249,6 +276,33 @@ func TestConnz(t *testing.T) { if ci.OutBytes != 5 { t.Fatalf("Expected OutBytes of 1, got %v\n", ci.OutBytes) } + + // Test JSON and JSONP + respJSON, errJSON := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "connz") + ct := respJSON.Header.Get("Content-Type"); + if errJSON != nil { + t.Fatalf("Expected no error: Got %v\n", errJSON) + } + if respJSON.StatusCode != 200 { + t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) + } + if ct != "application/json" { + t.Fatalf("Expected a 200 response, got %s\n", ct) + } + defer respJSON.Body.Close() + + respJSON, errJSON = http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "connz?callback=callback") + ct = respJSON.Header.Get("Content-Type"); + if errJSON != nil { + t.Fatalf("Expected no error: Got %v\n", errJSON) + } + if respJSON.StatusCode != 200 { + t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) + } + if ct != "application/javascript" { + t.Fatalf("Expected a 200 response, got %s\n", ct) + } + defer respJSON.Body.Close() } func TestConnzWithSubs(t *testing.T) { @@ -682,6 +736,33 @@ func TestConnzWithRoutes(t *testing.T) { if route.DidSolicit != false { t.Fatalf("Expected unsolicited route, got %v\n", route.DidSolicit) } + + // Test JSON and JSONP + respJSON, errJSON := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "routez") + ct := respJSON.Header.Get("Content-Type"); + if errJSON != nil { + t.Fatalf("Expected no error: Got %v\n", errJSON) + } + if respJSON.StatusCode != 200 { + t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) + } + if ct != "application/json" { + t.Fatalf("Expected a 200 response, got %s\n", ct) + } + defer respJSON.Body.Close() + + respJSON, errJSON = http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "routez?callback=callback") + ct = respJSON.Header.Get("Content-Type"); + if errJSON != nil { + t.Fatalf("Expected no error: Got %v\n", errJSON) + } + if respJSON.StatusCode != 200 { + t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) + } + if ct != "application/javascript" { + t.Fatalf("Expected a 200 response, got %s\n", ct) + } + defer respJSON.Body.Close() } func TestSubsz(t *testing.T) { @@ -719,6 +800,32 @@ func TestSubsz(t *testing.T) { t.Fatalf("Expected NumMatches of 1, got %d\n", sl.NumMatches) } + // Test JSON and JSONP + respJSON, errJSON := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "subscriptionsz") + ct := respJSON.Header.Get("Content-Type"); + if errJSON != nil { + t.Fatalf("Expected no error: Got %v\n", errJSON) + } + if respJSON.StatusCode != 200 { + t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) + } + if ct != "application/json" { + t.Fatalf("Expected a 200 response, got %s\n", ct) + } + defer respJSON.Body.Close() + + respJSON, errJSON = http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "subscriptionsz?callback=callback") + ct = respJSON.Header.Get("Content-Type"); + if errJSON != nil { + t.Fatalf("Expected no error: Got %v\n", errJSON) + } + if respJSON.StatusCode != 200 { + t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) + } + if ct != "application/javascript" { + t.Fatalf("Expected a 200 response, got %s\n", ct) + } + defer respJSON.Body.Close() } // Create a connection to test ConnInfo From 3c513b6a8ad3d7b95463875b07234860ce52755a Mon Sep 17 00:00:00 2001 From: cmfatih Date: Sat, 15 Aug 2015 18:13:01 -0400 Subject: [PATCH 06/12] Fix format --- server/monitor_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/server/monitor_test.go b/server/monitor_test.go index 1d2d8735..4f5457b5 100644 --- a/server/monitor_test.go +++ b/server/monitor_test.go @@ -154,7 +154,7 @@ func TestVarz(t *testing.T) { // Test JSON and JSONP respJSON, errJSON := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "varz") - ct := respJSON.Header.Get("Content-Type"); + ct := respJSON.Header.Get("Content-Type") if errJSON != nil { t.Fatalf("Expected no error: Got %v\n", errJSON) } @@ -167,7 +167,7 @@ func TestVarz(t *testing.T) { defer respJSON.Body.Close() respJSON, errJSON = http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "varz?callback=callback") - ct = respJSON.Header.Get("Content-Type"); + ct = respJSON.Header.Get("Content-Type") if errJSON != nil { t.Fatalf("Expected no error: Got %v\n", errJSON) } @@ -279,7 +279,7 @@ func TestConnz(t *testing.T) { // Test JSON and JSONP respJSON, errJSON := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "connz") - ct := respJSON.Header.Get("Content-Type"); + ct := respJSON.Header.Get("Content-Type") if errJSON != nil { t.Fatalf("Expected no error: Got %v\n", errJSON) } @@ -292,7 +292,7 @@ func TestConnz(t *testing.T) { defer respJSON.Body.Close() respJSON, errJSON = http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "connz?callback=callback") - ct = respJSON.Header.Get("Content-Type"); + ct = respJSON.Header.Get("Content-Type") if errJSON != nil { t.Fatalf("Expected no error: Got %v\n", errJSON) } @@ -739,7 +739,7 @@ func TestConnzWithRoutes(t *testing.T) { // Test JSON and JSONP respJSON, errJSON := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "routez") - ct := respJSON.Header.Get("Content-Type"); + ct := respJSON.Header.Get("Content-Type") if errJSON != nil { t.Fatalf("Expected no error: Got %v\n", errJSON) } @@ -752,7 +752,7 @@ func TestConnzWithRoutes(t *testing.T) { defer respJSON.Body.Close() respJSON, errJSON = http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "routez?callback=callback") - ct = respJSON.Header.Get("Content-Type"); + ct = respJSON.Header.Get("Content-Type") if errJSON != nil { t.Fatalf("Expected no error: Got %v\n", errJSON) } @@ -802,7 +802,7 @@ func TestSubsz(t *testing.T) { // Test JSON and JSONP respJSON, errJSON := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "subscriptionsz") - ct := respJSON.Header.Get("Content-Type"); + ct := respJSON.Header.Get("Content-Type") if errJSON != nil { t.Fatalf("Expected no error: Got %v\n", errJSON) } @@ -815,7 +815,7 @@ func TestSubsz(t *testing.T) { defer respJSON.Body.Close() respJSON, errJSON = http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "subscriptionsz?callback=callback") - ct = respJSON.Header.Get("Content-Type"); + ct = respJSON.Header.Get("Content-Type") if errJSON != nil { t.Fatalf("Expected no error: Got %v\n", errJSON) } From a40489678fc7706cbc25b4b3a712aab0787a4f27 Mon Sep 17 00:00:00 2001 From: cmfatih Date: Sat, 15 Aug 2015 18:26:59 -0400 Subject: [PATCH 07/12] Typo --- server/monitor_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/server/monitor_test.go b/server/monitor_test.go index 4f5457b5..1c52fb25 100644 --- a/server/monitor_test.go +++ b/server/monitor_test.go @@ -162,7 +162,7 @@ func TestVarz(t *testing.T) { t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) } if ct != "application/json" { - t.Fatalf("Expected a 200 response, got %s\n", ct) + t.Fatalf("Expected application/json content-type, got %s\n", ct) } defer respJSON.Body.Close() @@ -175,7 +175,7 @@ func TestVarz(t *testing.T) { t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) } if ct != "application/javascript" { - t.Fatalf("Expected a 200 response, got %s\n", ct) + t.Fatalf("Expected application/javascript content-type, got %s\n", ct) } defer respJSON.Body.Close() } @@ -287,7 +287,7 @@ func TestConnz(t *testing.T) { t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) } if ct != "application/json" { - t.Fatalf("Expected a 200 response, got %s\n", ct) + t.Fatalf("Expected application/json content-type, got %s\n", ct) } defer respJSON.Body.Close() @@ -300,7 +300,7 @@ func TestConnz(t *testing.T) { t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) } if ct != "application/javascript" { - t.Fatalf("Expected a 200 response, got %s\n", ct) + t.Fatalf("Expected application/javascript content-type, got %s\n", ct) } defer respJSON.Body.Close() } @@ -747,7 +747,7 @@ func TestConnzWithRoutes(t *testing.T) { t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) } if ct != "application/json" { - t.Fatalf("Expected a 200 response, got %s\n", ct) + t.Fatalf("Expected application/json content-type, got %s\n", ct) } defer respJSON.Body.Close() @@ -760,7 +760,7 @@ func TestConnzWithRoutes(t *testing.T) { t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) } if ct != "application/javascript" { - t.Fatalf("Expected a 200 response, got %s\n", ct) + t.Fatalf("Expected application/javascript content-type, got %s\n", ct) } defer respJSON.Body.Close() } @@ -810,7 +810,7 @@ func TestSubsz(t *testing.T) { t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) } if ct != "application/json" { - t.Fatalf("Expected a 200 response, got %s\n", ct) + t.Fatalf("Expected application/json response, got %s\n", ct) } defer respJSON.Body.Close() @@ -823,7 +823,7 @@ func TestSubsz(t *testing.T) { t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) } if ct != "application/javascript" { - t.Fatalf("Expected a 200 response, got %s\n", ct) + t.Fatalf("Expected application/javascript response, got %s\n", ct) } defer respJSON.Body.Close() } From 2b6b160477dfb6b629ef037c64e69540a9a03eea Mon Sep 17 00:00:00 2001 From: cmfatih Date: Sat, 15 Aug 2015 18:45:39 -0400 Subject: [PATCH 08/12] Cleanup --- server/monitor_test.go | 130 +++++++++++++---------------------------- 1 file changed, 41 insertions(+), 89 deletions(-) diff --git a/server/monitor_test.go b/server/monitor_test.go index 1c52fb25..b2de94d9 100644 --- a/server/monitor_test.go +++ b/server/monitor_test.go @@ -99,6 +99,10 @@ func TestVarz(t *testing.T) { if resp.StatusCode != 200 { t.Fatalf("Expected a 200 response, got %d\n", resp.StatusCode) } + ct := resp.Header.Get("Content-Type") + if ct != "application/json" { + t.Fatalf("Expected application/json content-type, got %s\n", ct) + } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { @@ -152,32 +156,16 @@ func TestVarz(t *testing.T) { t.Fatalf("Expected OutBytes of 5, got %v\n", v.OutBytes) } - // Test JSON and JSONP - respJSON, errJSON := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "varz") - ct := respJSON.Header.Get("Content-Type") - if errJSON != nil { - t.Fatalf("Expected no error: Got %v\n", errJSON) - } - if respJSON.StatusCode != 200 { - t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) - } - if ct != "application/json" { - t.Fatalf("Expected application/json content-type, got %s\n", ct) - } - defer respJSON.Body.Close() - - respJSON, errJSON = http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "varz?callback=callback") - ct = respJSON.Header.Get("Content-Type") - if errJSON != nil { - t.Fatalf("Expected no error: Got %v\n", errJSON) - } - if respJSON.StatusCode != 200 { - t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) + // Test JSONP + respj, errj := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "varz?callback=callback") + if errj != nil { + t.Fatalf("Expected no error: Got %v\n", err) } + ct = respj.Header.Get("Content-Type") if ct != "application/javascript" { t.Fatalf("Expected application/javascript content-type, got %s\n", ct) } - defer respJSON.Body.Close() + defer respj.Body.Close() } func TestConnz(t *testing.T) { @@ -192,6 +180,10 @@ func TestConnz(t *testing.T) { if resp.StatusCode != 200 { t.Fatalf("Expected a 200 response, got %d\n", resp.StatusCode) } + ct := resp.Header.Get("Content-Type") + if ct != "application/json" { + t.Fatalf("Expected application/json content-type, got %s\n", ct) + } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { @@ -277,32 +269,16 @@ func TestConnz(t *testing.T) { t.Fatalf("Expected OutBytes of 1, got %v\n", ci.OutBytes) } - // Test JSON and JSONP - respJSON, errJSON := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "connz") - ct := respJSON.Header.Get("Content-Type") - if errJSON != nil { - t.Fatalf("Expected no error: Got %v\n", errJSON) - } - if respJSON.StatusCode != 200 { - t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) - } - if ct != "application/json" { - t.Fatalf("Expected application/json content-type, got %s\n", ct) - } - defer respJSON.Body.Close() - - respJSON, errJSON = http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "connz?callback=callback") - ct = respJSON.Header.Get("Content-Type") - if errJSON != nil { - t.Fatalf("Expected no error: Got %v\n", errJSON) - } - if respJSON.StatusCode != 200 { - t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) + // Test JSONP + respj, errj := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "connz?callback=callback") + if errj != nil { + t.Fatalf("Expected no error: Got %v\n", err) } + ct = respj.Header.Get("Content-Type") if ct != "application/javascript" { t.Fatalf("Expected application/javascript content-type, got %s\n", ct) } - defer respJSON.Body.Close() + defer respj.Body.Close() } func TestConnzWithSubs(t *testing.T) { @@ -683,6 +659,10 @@ func TestConnzWithRoutes(t *testing.T) { if resp.StatusCode != 200 { t.Fatalf("Expected a 200 response, got %d\n", resp.StatusCode) } + ct := resp.Header.Get("Content-Type") + if ct != "application/json" { + t.Fatalf("Expected application/json content-type, got %s\n", ct) + } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { @@ -737,32 +717,16 @@ func TestConnzWithRoutes(t *testing.T) { t.Fatalf("Expected unsolicited route, got %v\n", route.DidSolicit) } - // Test JSON and JSONP - respJSON, errJSON := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "routez") - ct := respJSON.Header.Get("Content-Type") - if errJSON != nil { - t.Fatalf("Expected no error: Got %v\n", errJSON) - } - if respJSON.StatusCode != 200 { - t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) - } - if ct != "application/json" { - t.Fatalf("Expected application/json content-type, got %s\n", ct) - } - defer respJSON.Body.Close() - - respJSON, errJSON = http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "routez?callback=callback") - ct = respJSON.Header.Get("Content-Type") - if errJSON != nil { - t.Fatalf("Expected no error: Got %v\n", errJSON) - } - if respJSON.StatusCode != 200 { - t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) + // Test JSONP + respj, errj := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "routez?callback=callback") + if errj != nil { + t.Fatalf("Expected no error: Got %v\n", err) } + ct = respj.Header.Get("Content-Type") if ct != "application/javascript" { t.Fatalf("Expected application/javascript content-type, got %s\n", ct) } - defer respJSON.Body.Close() + defer respj.Body.Close() } func TestSubsz(t *testing.T) { @@ -780,6 +744,10 @@ func TestSubsz(t *testing.T) { if resp.StatusCode != 200 { t.Fatalf("Expected a 200 response, got %d\n", resp.StatusCode) } + ct := resp.Header.Get("Content-Type") + if ct != "application/json" { + t.Fatalf("Expected application/json content-type, got %s\n", ct) + } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { @@ -800,32 +768,16 @@ func TestSubsz(t *testing.T) { t.Fatalf("Expected NumMatches of 1, got %d\n", sl.NumMatches) } - // Test JSON and JSONP - respJSON, errJSON := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "subscriptionsz") - ct := respJSON.Header.Get("Content-Type") - if errJSON != nil { - t.Fatalf("Expected no error: Got %v\n", errJSON) - } - if respJSON.StatusCode != 200 { - t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) - } - if ct != "application/json" { - t.Fatalf("Expected application/json response, got %s\n", ct) - } - defer respJSON.Body.Close() - - respJSON, errJSON = http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "subscriptionsz?callback=callback") - ct = respJSON.Header.Get("Content-Type") - if errJSON != nil { - t.Fatalf("Expected no error: Got %v\n", errJSON) - } - if respJSON.StatusCode != 200 { - t.Fatalf("Expected a 200 response, got %d\n", respJSON.StatusCode) + // Test JSONP + respj, errj := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "subscriptionsz?callback=callback") + ct = respj.Header.Get("Content-Type") + if errj != nil { + t.Fatalf("Expected no error: Got %v\n", err) } if ct != "application/javascript" { - t.Fatalf("Expected application/javascript response, got %s\n", ct) + t.Fatalf("Expected application/javascript content-type, got %s\n", ct) } - defer respJSON.Body.Close() + defer respj.Body.Close() } // Create a connection to test ConnInfo From 7071a9518d44795c6da3f32c96b47dd27d1bf210 Mon Sep 17 00:00:00 2001 From: cmfatih Date: Sat, 15 Aug 2015 18:52:32 -0400 Subject: [PATCH 09/12] Add TestHandleRoot --- server/monitor_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/server/monitor_test.go b/server/monitor_test.go index b2de94d9..b899bd99 100644 --- a/server/monitor_test.go +++ b/server/monitor_test.go @@ -5,6 +5,7 @@ package server import ( "encoding/json" "fmt" + "strings" "io/ioutil" "net/http" "net/url" @@ -780,6 +781,28 @@ func TestSubsz(t *testing.T) { defer respj.Body.Close() } +// Tests handle root +func TestHandleRoot(t *testing.T) { + s := runMonitorServer(DEFAULT_HTTP_PORT) + defer s.Shutdown() + + nc := createClientConnSubscribeAndPublish(t) + defer nc.Close() + + resp, err := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT)) + if err != nil { + t.Fatalf("Expected no error: Got %v\n", err) + } + if resp.StatusCode != 200 { + t.Fatalf("Expected a 200 response, got %d\n", resp.StatusCode) + } + ct := resp.Header.Get("Content-Type") + if !strings.Contains(ct, "text/html") { + t.Fatalf("Expected text/html response, got %s\n", ct) + } + defer resp.Body.Close() +} + // Create a connection to test ConnInfo func createClientConnSubscribeAndPublish(t *testing.T) *nats.Conn { nc, err := nats.Connect(fmt.Sprintf("nats://localhost:%d", CLIENT_PORT)) From 3dc7bc2e70488eceecb679d42202ca87273fb94a Mon Sep 17 00:00:00 2001 From: cmfatih Date: Sat, 15 Aug 2015 18:55:02 -0400 Subject: [PATCH 10/12] Fix format --- server/monitor_test.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/server/monitor_test.go b/server/monitor_test.go index b899bd99..bb041187 100644 --- a/server/monitor_test.go +++ b/server/monitor_test.go @@ -5,10 +5,10 @@ package server import ( "encoding/json" "fmt" - "strings" "io/ioutil" "net/http" "net/url" + "strings" "testing" "time" @@ -783,24 +783,24 @@ func TestSubsz(t *testing.T) { // Tests handle root func TestHandleRoot(t *testing.T) { - s := runMonitorServer(DEFAULT_HTTP_PORT) - defer s.Shutdown() + s := runMonitorServer(DEFAULT_HTTP_PORT) + defer s.Shutdown() - nc := createClientConnSubscribeAndPublish(t) - defer nc.Close() + nc := createClientConnSubscribeAndPublish(t) + defer nc.Close() - resp, err := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT)) - if err != nil { - t.Fatalf("Expected no error: Got %v\n", err) - } - if resp.StatusCode != 200 { - t.Fatalf("Expected a 200 response, got %d\n", resp.StatusCode) - } - ct := resp.Header.Get("Content-Type") - if !strings.Contains(ct, "text/html") { - t.Fatalf("Expected text/html response, got %s\n", ct) - } - defer resp.Body.Close() + resp, err := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT)) + if err != nil { + t.Fatalf("Expected no error: Got %v\n", err) + } + if resp.StatusCode != 200 { + t.Fatalf("Expected a 200 response, got %d\n", resp.StatusCode) + } + ct := resp.Header.Get("Content-Type") + if !strings.Contains(ct, "text/html") { + t.Fatalf("Expected text/html response, got %s\n", ct) + } + defer resp.Body.Close() } // Create a connection to test ConnInfo From e86e3db2e002326c305e60bd675aa2777459357f Mon Sep 17 00:00:00 2001 From: cmfatih Date: Mon, 17 Aug 2015 22:22:00 -0400 Subject: [PATCH 11/12] Change getting callback query --- server/monitor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/monitor.go b/server/monitor.go index cb39bb66..ed589940 100644 --- a/server/monitor.go +++ b/server/monitor.go @@ -345,7 +345,7 @@ func updateUsage(v *Varz) { // ResponseHandler handles responses for monitoring routes func ResponseHandler(w http.ResponseWriter, r *http.Request, data []byte) { // Get callback from request - callback := r.FormValue("callback") + callback := r.URL.Query().Get("callback") // If callback is not empty then if callback != "" { // Response for JSONP From 6a166bb7911dfcefe005b491e3306c72d507208d Mon Sep 17 00:00:00 2001 From: cmfatih Date: Sat, 22 Aug 2015 01:56:25 -0400 Subject: [PATCH 12/12] Add JSONP note --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index c3b537b2..11c2a561 100644 --- a/README.md +++ b/README.md @@ -236,6 +236,19 @@ You can also report detailed subscription information on a per connection basis } ``` +Monitoring endpoints support JSONP for CORS so you can easily create single page +web applications for monitoring. Simply pass `callback` query parameter to any +endpoint. For example; `http://localhost:8222/connz?callback=cb` + +```javascript +// JQuery example + +$.getJSON('http://localhost:8222/connz?callback=?', function(data) { + console.log(data); +}); + +``` + ## Building This code currently requires at _least_ version 1.1 of Go, but we encourage