From 579001059acff9abc9580c57b9aaf35fe796e404 Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Thu, 3 Mar 2016 10:34:07 +0000 Subject: [PATCH 1/2] Add pipe/{pipeID}/check endpoint --- app/pipes.go | 16 ++++++++++++++++ client/app/scripts/utils/web-api-utils.js | 6 +++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/app/pipes.go b/app/pipes.go index c4724a360..8da9e9431 100644 --- a/app/pipes.go +++ b/app/pipes.go @@ -12,6 +12,10 @@ import ( // RegisterPipeRoutes registers the pipe routes func RegisterPipeRoutes(router *mux.Router, pr PipeRouter) { + router.Methods("GET"). + Path("/api/pipe/{pipeID}/check"). + HandlerFunc(requestContextDecorator(checkPipe(pr, UIEnd))) + router.Methods("GET"). Path("/api/pipe/{pipeID}"). HandlerFunc(requestContextDecorator(handlePipeWs(pr, UIEnd))) @@ -25,6 +29,18 @@ func RegisterPipeRoutes(router *mux.Router, pr PipeRouter) { HandlerFunc(requestContextDecorator(deletePipe(pr))) } +func checkPipe(pr PipeRouter, end End) CtxHandlerFunc { + return func(ctx context.Context, w http.ResponseWriter, r *http.Request) { + id := mux.Vars(r)["pipeID"] + _, _, ok := pr.Get(ctx, id, end) + if !ok { + http.NotFound(w, r) + return + } + defer pr.Release(ctx, id, end) + } +} + func handlePipeWs(pr PipeRouter, end End) CtxHandlerFunc { return func(ctx context.Context, w http.ResponseWriter, r *http.Request) { id := mux.Vars(r)["pipeID"] diff --git a/client/app/scripts/utils/web-api-utils.js b/client/app/scripts/utils/web-api-utils.js index f9a2f977d..deacca699 100644 --- a/client/app/scripts/utils/web-api-utils.js +++ b/client/app/scripts/utils/web-api-utils.js @@ -214,16 +214,16 @@ export function deletePipe(pipeId) { } export function getPipeStatus(pipeId) { - const url = `/api/pipe/${encodeURIComponent(pipeId)}`; + const url = `/api/pipe/${encodeURIComponent(pipeId)}/check`; reqwest({ method: 'GET', url: url, success: function(res) { - log('ERROR: expected responses: [400, 404]. Got:', res); + log('ERROR: expected responses: [200, 404]. Got:', res); }, error: function(err) { const status = { - 400: 'PIPE_ALIVE', + 200: 'PIPE_ALIVE', 404: 'PIPE_DELETED' }[err.status]; From b9393e9e39dd59802860f592453b4ea7677d7a91 Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Thu, 3 Mar 2016 12:35:04 +0000 Subject: [PATCH 2/2] Review feedback --- app/pipes.go | 4 ++-- client/app/scripts/utils/web-api-utils.js | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/pipes.go b/app/pipes.go index 8da9e9431..6bb5742f6 100644 --- a/app/pipes.go +++ b/app/pipes.go @@ -34,10 +34,10 @@ func checkPipe(pr PipeRouter, end End) CtxHandlerFunc { id := mux.Vars(r)["pipeID"] _, _, ok := pr.Get(ctx, id, end) if !ok { - http.NotFound(w, r) + w.WriteHeader(http.StatusNoContent) return } - defer pr.Release(ctx, id, end) + pr.Release(ctx, id, end) } } diff --git a/client/app/scripts/utils/web-api-utils.js b/client/app/scripts/utils/web-api-utils.js index deacca699..3fd3a979d 100644 --- a/client/app/scripts/utils/web-api-utils.js +++ b/client/app/scripts/utils/web-api-utils.js @@ -218,17 +218,17 @@ export function getPipeStatus(pipeId) { reqwest({ method: 'GET', url: url, - success: function(res) { - log('ERROR: expected responses: [200, 404]. Got:', res); - }, error: function(err) { + log('ERROR: unexpected response:', err); + }, + success: function(res) { const status = { 200: 'PIPE_ALIVE', - 404: 'PIPE_DELETED' - }[err.status]; + 204: 'PIPE_DELETED' + }[res.status]; if (!status) { - log('Unexpected pipe status:', err.status); + log('Unexpected pipe status:', res.status); return; }