From 3e443e604cd07e13301630fe377d4a397b5e9994 Mon Sep 17 00:00:00 2001 From: Travis Reeder Date: Wed, 12 Oct 2016 14:13:46 -0700 Subject: [PATCH] Added async post command to README. --- .gitignore | 1 + CONTRIBUTING.md | 1 + Dockerfile | 2 +- README.md | 12 ++++- api/runner/async_runner.go | 2 +- api/server/helpers.go | 90 -------------------------------------- scripts/build-docker.sh | 2 +- 7 files changed, 15 insertions(+), 95 deletions(-) delete mode 100644 api/server/helpers.go diff --git a/.gitignore b/.gitignore index 7947816df..7445a82a2 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ vendor/ /microgateway /gateway /functions +/functions-alpine bolt.db .glide/ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 14f09d3ce..bbc18e005 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -11,6 +11,7 @@ make all First let's start our IronFunctions API ##### Run in Docker + ``` make run-docker ``` diff --git a/Dockerfile b/Dockerfile index d14aa001f..f5439d187 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM iron/dind RUN mkdir /app -ADD functions /app/ +ADD functions-alpine /app/functions WORKDIR /app CMD ["./functions"] diff --git a/README.md b/README.md index 380696ec3..7969ff617 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ curl -H "Content-Type: application/json" -X POST -d '{ Calling your function is as simple as requesting a URL. Each app has it's own namespace and each route mapped to the app. The app `myapp` that we created above along with the `/hello` route we added would be called via the following URL. -``` +```sh curl http://localhost:8080/r/myapp/hello ``` @@ -84,7 +84,15 @@ curl -H "Content-Type: application/json" -X POST -d '{ }' http://localhost:8080/v1/apps/myapp/routes ``` -Now if you request this route, you will just get a `call_id` response: +Now if you request this route: + +```sh +curl -H "Content-Type: application/json" -X POST -d '{ + "name":"Johnny" +}' http://localhost:8080/r/myapp/hello-async +``` + +You will get a `call_id` in the response: ```json {"call_id":"572415fd-e26e-542b-846f-f1f5870034f2"} diff --git a/api/runner/async_runner.go b/api/runner/async_runner.go index eb87b152c..c9fa08cfe 100644 --- a/api/runner/async_runner.go +++ b/api/runner/async_runner.go @@ -136,7 +136,7 @@ func startAsyncRunners(ctx context.Context, wg *sync.WaitGroup, i int, url strin task, err := getTask(ctx, url) if err != nil { if err, ok := err.(net.Error); ok && err.Timeout() { - log.Infoln("Could not fetch task, timeout. Probably no tasks to run.") + log.WithError(err).Errorln("Could not fetch task, timeout.") continue } log.WithError(err).Error("Could not fetch task") diff --git a/api/server/helpers.go b/api/server/helpers.go deleted file mode 100644 index 362157fcd..000000000 --- a/api/server/helpers.go +++ /dev/null @@ -1,90 +0,0 @@ -package server - -// TODO: this whole file shouldn't be in a non test file - -import ( - "context" - "encoding/json" - "io" - "io/ioutil" - "net/http" - "net/http/httptest" - "testing" - - "github.com/gin-gonic/gin" - "github.com/iron-io/functions/api/models" - "github.com/iron-io/functions/api/runner" - "github.com/iron-io/runner/common" -) - -type appResponse struct { - Message string `json:"message"` - App *models.App `json:"app"` -} - -type appsResponse struct { - Message string `json:"message"` - Apps models.Apps `json:"apps"` -} - -type routeResponse struct { - Message string `json:"message"` - Route *models.Route `json:"route"` -} - -type routesResponse struct { - Message string `json:"message"` - Routes models.Routes `json:"routes"` -} - -type tasksResponse struct { - Message string `json:"message"` - Task models.Task `json:"tasksResponse"` -} - -func testRouter(s *Server) *gin.Engine { - r := gin.Default() - ctx := context.Background() - r.Use(func(c *gin.Context) { - ctx, _ := common.LoggerWithFields(ctx, extractFields(c)) - c.Set("ctx", ctx) - c.Next() - }) - s.bindHandlers() - return r -} - -func testRunner(t *testing.T) *runner.Runner { - r, err := runner.New(runner.NewMetricLogger()) - if err != nil { - t.Fatal("Test: failed to create new runner") - } - return r -} - -func routerRequest(t *testing.T, router *gin.Engine, method, path string, body io.Reader) (*http.Request, *httptest.ResponseRecorder) { - req, err := http.NewRequest(method, "http://localhost:8080"+path, body) - if err != nil { - t.Fatalf("Test: Could not create %s request to %s: %v", method, path, err) - } - - rec := httptest.NewRecorder() - router.ServeHTTP(rec, req) - - return req, rec -} - -func getErrorResponse(t *testing.T, rec *httptest.ResponseRecorder) models.Error { - respBody, err := ioutil.ReadAll(rec.Body) - if err != nil { - t.Error("Test: Expected not empty response body") - } - - var errResp models.Error - err = json.Unmarshal(respBody, &errResp) - if err != nil { - t.Error("Test: Expected response body to be a valid models.Error object") - } - - return errResp -} diff --git a/scripts/build-docker.sh b/scripts/build-docker.sh index 37649bd48..08143fa31 100755 --- a/scripts/build-docker.sh +++ b/scripts/build-docker.sh @@ -1,4 +1,4 @@ set -ex -docker run --rm -v "$PWD":/go/src/github.com/iron-io/functions -w /go/src/github.com/iron-io/functions iron/go:dev sh -c 'go build -o functions' +docker run --rm -v "$PWD":/go/src/github.com/iron-io/functions -w /go/src/github.com/iron-io/functions iron/go:dev go build -o functions-alpine docker build -t iron/functions:latest .