diff --git a/handlers.go b/handlers.go index 3a2deeb..bcbd53f 100644 --- a/handlers.go +++ b/handlers.go @@ -18,8 +18,7 @@ import ( var validBasketName = regexp.MustCompile(basketNamePattern) var defaultResponse = ResponseConfig{Status: 200, IsTemplate: false} -var indexPageTemplate = template.Must(template.New("index").Parse(indexPageContent)) -var basketPageTemplate = template.Must(template.New("basket").Parse(basketPageContent)) +var basketPageTemplate = template.Must(template.New("basket").Parse(basketPageContentTemplate)) // writeJSON writes JSON content to HTTP response func writeJSON(w http.ResponseWriter, status int, json []byte, err error) { @@ -326,13 +325,21 @@ func ForwardToWeb(w http.ResponseWriter, r *http.Request, ps httprouter.Params) // WebIndexPage handles HTTP request to render index page func WebIndexPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - indexPageTemplate.Execute(w, "") + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.Write(indexPageContent) } // WebBasketPage handles HTTP request to render basket details page func WebBasketPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { if name := ps.ByName("basket"); validBasketName.MatchString(name) { - basketPageTemplate.Execute(w, name) + switch name { + case serviceAPIPath: + // admin page to access all baskets + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.Write(basketsPageContent) + default: + basketPageTemplate.Execute(w, name) + } } else { http.Error(w, "Basket name does not match pattern: "+validBasketName.String(), http.StatusBadRequest) } diff --git a/handlers_test.go b/handlers_test.go index 66f594c..c1c2f23 100644 --- a/handlers_test.go +++ b/handlers_test.go @@ -856,6 +856,20 @@ func TestWebBasketPage(t *testing.T) { } } +func TestWebBasketsPage(t *testing.T) { + r, err := http.NewRequest("GET", "http://localhost:55555/web/"+serviceAPIPath, strings.NewReader("")) + if assert.NoError(t, err) { + w := httptest.NewRecorder() + ps := append(make(httprouter.Params, 0), httprouter.Param{Key: "basket", Value: serviceAPIPath}) + WebBasketPage(w, r, ps) + + // validate response: 200 - OK + assert.Equal(t, 200, w.Code, "wrong HTTP result code") + assert.Contains(t, w.Body.String(), "Request Baskets - Administration", + "HTML index page with baskets is expected") + } +} + func TestWebBasketPage_InvalidName(t *testing.T) { basket := ">>>" diff --git a/server.go b/server.go index b550630..5237a9a 100644 --- a/server.go +++ b/server.go @@ -54,9 +54,9 @@ func StartServer() { // web pages router.GET("/", ForwardToWeb) - //router.GET("/"+serviceUIPath, WebIndexPage) - //router.GET("/"+serviceUIPath+"/:basket", WebBasketPage) - router.ServeFiles("/"+serviceUIPath+"/*filepath", http.Dir("./src/github.com/darklynx/request-baskets/web")) + router.GET("/"+serviceUIPath, WebIndexPage) + router.GET("/"+serviceUIPath+"/:basket", WebBasketPage) + //router.ServeFiles("/"+serviceUIPath+"/*filepath", http.Dir("./src/github.com/darklynx/request-baskets/web")) // basket requests router.NotFound = http.HandlerFunc(AcceptBasketRequests) diff --git a/web/baskets.html b/web/baskets.html index 1f002d9..a91fa5d 100644 --- a/web/baskets.html +++ b/web/baskets.html @@ -1,7 +1,7 @@ - Request Baskets + Request Baskets - Administration diff --git a/web_basket.html.go b/web_basket.html.go index ea38919..bd9b3b1 100644 --- a/web_basket.html.go +++ b/web_basket.html.go @@ -1,7 +1,7 @@ package main const ( - basketPageContent = ` + basketPageContentTemplate = ` Request Basket: {{.}} @@ -26,7 +26,7 @@ const ( var autoRefreshId; function getToken() { - var token = sessionStorage.getItem("token_{{.}}"); + var token = localStorage.getItem("basket_{{.}}"); if (!token) { // fall back to master token if provided token = sessionStorage.getItem("master_token"); } @@ -35,6 +35,7 @@ const ( function onAjaxError(jqXHR) { if (jqXHR.status == 401) { + localStorage.removeItem("basket_{{.}}"); enableAutoRefresh(false); $("#token_dialog").modal({ keyboard : false }); } else { @@ -116,8 +117,10 @@ const ( $("#requests_count").html(data.count + " (" + totalCount + ")"); if (data.count > 0) { $("#empty_basket").addClass("hide"); + $("#requests_link").removeClass("hide"); } else { $("#empty_basket").removeClass("hide"); + $("#requests_link").addClass("hide"); } if (data && data.requests) { @@ -348,17 +351,17 @@ const ( "Authorization" : getToken() } }).done(function(data) { - sessionStorage.removeItem("token_{{.}}"); + localStorage.removeItem("basket_{{.}}"); window.location.href = "/web"; }).fail(onAjaxError); } // Initialization $(document).ready(function() { - $("#basket_uri").html(window.location.protocol + "//" + window.location.host + "/{{.}}"); + $(".basket_uri").html(window.location.protocol + "//" + window.location.host + "/{{.}}"); // dialogs $("#token_dialog").on("hidden.bs.modal", function (event) { - sessionStorage.setItem("token_{{.}}", $("#basket_token").val()); + localStorage.setItem("basket_{{.}}", $("#basket_token").val()); fetchRequests(); }); $("#config_form").on("submit", function(event) { @@ -594,6 +597,7 @@ const (

Basket: {{.}}

+

Requests:

@@ -611,7 +615,7 @@ const (

Empty basket!

-

This basket is empty, send requests to /{{.}} and they will appear here.

+

This basket is empty, send requests to and they will appear here.

diff --git a/web_baskets.html.go b/web_baskets.html.go new file mode 100644 index 0000000..18c5a18 --- /dev/null +++ b/web_baskets.html.go @@ -0,0 +1,171 @@ +package main + +var ( + basketsPageContent = []byte(` + + + Request Baskets - Administration + + + + + + + + + + + + + + + + + + + + +
+
+
+

All Baskets

+
+
+
+
+
    +
+
+ more... +
+
+
+ + + +`) +) diff --git a/web_index.html.go b/web_index.html.go index bda0317..46a5025 100644 --- a/web_index.html.go +++ b/web_index.html.go @@ -1,7 +1,7 @@ package main -const ( - indexPageContent = ` +var ( + indexPageContent = []byte(` Request Baskets @@ -17,15 +17,17 @@ const ( .footer { position: absolute; bottom: 0; width: 100%; height: 60px; background-color: #f5f5f5; } .container .text-muted { margin: 20px 0; } h1 { margin-top: 2px; } - #more { margin-left: 60px; padding-bottom: 10px; } - #baskets ul { width: 100%; } - #baskets li { padding: 0 0 5px 20px; float: left; display: inline; position: relative; width: 50%; } - #baskets li:before { content: "\f291"; font-family: "FontAwesome"; position: absolute; left: 0px; top:0px; } + #baskets { margin-left: -30px; } + #baskets li { list-style: none; } + #baskets li:before { content: "\f291"; font-family: "FontAwesome"; padding-right: 5px; } @@ -143,9 +97,9 @@ const (
@@ -214,24 +168,28 @@ const (
-
-

Baskets

+
+
+

New Basket

+

Create a basket to collect and inspect HTTP requests

+ +
-
-
@@ -242,5 +200,5 @@ const (
-` +`) )