From 02d6349cf87da44139556a2df14d4b873d675eaf Mon Sep 17 00:00:00 2001 From: Will Price Date: Thu, 6 Jul 2017 10:33:34 +0100 Subject: [PATCH] Allow calling root route on app Fixes #64 Previously calling a root registered route would result in an error message "Not Found" suggesting the route hadn't been registed, yet when listing the routes, `fn routes list myapp` you could see the `/` route. You can now successfully call a root registered route with `fn call myapp /` --- api/server/runner.go | 7 ++++++- api/server/runner_test.go | 3 +++ api/server/server.go | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/api/server/runner.go b/api/server/runner.go index c6c7e872c..b6812a7c6 100644 --- a/api/server/runner.go +++ b/api/server/runner.go @@ -94,9 +94,14 @@ func (s *Server) handleRequest(c *gin.Context, enqueue models.Enqueue) { payload = strings.NewReader(reqPayload) } + r, routeExists := c.Get(api.Path) + if !routeExists { + r = "/" + } + reqRoute := &models.Route{ AppName: c.MustGet(api.AppName).(string), - Path: path.Clean(c.MustGet(api.Path).(string)), + Path: path.Clean(r.(string)), } s.FireBeforeDispatch(ctx, reqRoute) diff --git a/api/server/runner_test.go b/api/server/runner_test.go index 93052a514..21e6cab3a 100644 --- a/api/server/runner_test.go +++ b/api/server/runner_test.go @@ -124,6 +124,7 @@ func TestRouteRunnerExecution(t *testing.T) { {Name: "myapp", Config: models.Config{}}, }, []*models.Route{ + {Path: "/", AppName: "myapp", Image: "funcy/hello", Headers: map[string][]string{"X-Function": {"Test"}}}, {Path: "/myroute", AppName: "myapp", Image: "funcy/hello", Headers: map[string][]string{"X-Function": {"Test"}}}, {Path: "/myerror", AppName: "myapp", Image: "funcy/error", Headers: map[string][]string{"X-Function": {"Test"}}}, }, nil, nil, @@ -141,10 +142,12 @@ func TestRouteRunnerExecution(t *testing.T) { expectedCode int expectedHeaders map[string][]string }{ + {"/r/myapp/", ``, "GET", http.StatusOK, map[string][]string{"X-Function": {"Test"}}}, {"/r/myapp/myroute", ``, "GET", http.StatusOK, map[string][]string{"X-Function": {"Test"}}}, {"/r/myapp/myerror", ``, "GET", http.StatusInternalServerError, map[string][]string{"X-Function": {"Test"}}}, // Added same tests again to check if time is reduced by the auth cache + {"/r/myapp/", ``, "GET", http.StatusOK, map[string][]string{"X-Function": {"Test"}}}, {"/r/myapp/myroute", ``, "GET", http.StatusOK, map[string][]string{"X-Function": {"Test"}}}, {"/r/myapp/myerror", ``, "GET", http.StatusInternalServerError, map[string][]string{"X-Function": {"Test"}}}, } { diff --git a/api/server/server.go b/api/server/server.go index 3273c80b0..cd43ef6f7 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -327,6 +327,7 @@ func (s *Server) bindHandlers(ctx context.Context) { engine.DELETE("/tasks", s.handleTaskRequest) engine.GET("/tasks", s.handleTaskRequest) + engine.Any("/r/:app", s.handleRunnerRequest) engine.Any("/r/:app/*route", s.handleRunnerRequest) // This final route is used for extensions, see Server.Add