From 52cab30056b7d3627a209ee393a45bb84160096b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Seif=20Lotfy=20=D8=B3=D9=8A=D9=81=20=D9=84=D8=B7=D9=81?= =?UTF-8?q?=D9=8A?= Date: Thu, 6 Oct 2016 23:44:58 +0200 Subject: [PATCH] Change PAYLOAD input to STDIN (#111) * change to iron-io/runner dependency * Fix runner dependency * Change PAYLOAD input to STDIN, fixes #40 --- api/runner/runner.go | 1 + api/runner/task.go | 3 +++ api/server/runner.go | 32 +++++++++++++------------------- api/server/runner_test.go | 4 ---- circle.yml | 2 +- data/worker_mq.db | Bin 0 -> 32768 bytes examples/hello/hello.rb | 6 +----- examples/redis/main.go | 7 ++++--- glide.lock | 23 +++++++++-------------- glide.yaml | 4 +++- tool/examples/hello/hello.go | 6 ++++-- 11 files changed, 39 insertions(+), 49 deletions(-) create mode 100644 data/worker_mq.db diff --git a/api/runner/runner.go b/api/runner/runner.go index b8f33447a..fd98877df 100644 --- a/api/runner/runner.go +++ b/api/runner/runner.go @@ -29,6 +29,7 @@ type Config struct { AppName string Memory uint64 Env map[string]string + Input io.Reader Stdout io.Writer Stderr io.Writer } diff --git a/api/runner/task.go b/api/runner/task.go index ad70b184e..25e9fc549 100644 --- a/api/runner/task.go +++ b/api/runner/task.go @@ -21,6 +21,9 @@ func (t *containerTask) Command() string { return "" } func (t *containerTask) EnvVars() map[string]string { return t.cfg.Env } +func (t *containerTask) Input() io.Reader { + return t.cfg.Input +} func (t *containerTask) Labels() map[string]string { return map[string]string{ diff --git a/api/server/runner.go b/api/server/runner.go index 7fe219b23..b62a8eb28 100644 --- a/api/server/runner.go +++ b/api/server/runner.go @@ -3,8 +3,8 @@ package server import ( "bytes" "context" - "encoding/json" "fmt" + "io" "io/ioutil" "net/http" "strings" @@ -16,7 +16,7 @@ import ( "github.com/iron-io/functions/api/runner" "github.com/iron-io/runner/common" "github.com/iron-io/runner/drivers" - "github.com/satori/go.uuid" + uuid "github.com/satori/go.uuid" ) func handleSpecial(c *gin.Context) { @@ -45,28 +45,22 @@ func handleRequest(c *gin.Context, enqueue models.Enqueue) { ctx, log = common.LoggerWithFields(ctx, logrus.Fields{"call_id": reqID}) var err error + var payload io.Reader - var payload []byte if c.Request.Method == "POST" || c.Request.Method == "PUT" { - payload, err = ioutil.ReadAll(c.Request.Body) + payload = c.Request.Body + // Load complete body and close + defer func() { + io.Copy(ioutil.Discard, c.Request.Body) + c.Request.Body.Close() + }() } else if c.Request.Method == "GET" { - qPL := c.Request.URL.Query()["payload"] - if len(qPL) > 0 { - payload = []byte(qPL[0]) + reqPayload := c.Request.URL.Query().Get("payload") + if len(reqPayload) > 0 { + payload = strings.NewReader(reqPayload) } } - if len(payload) > 0 { - var emptyJSON map[string]interface{} - if err := json.Unmarshal(payload, &emptyJSON); err != nil { - log.WithError(err).Error(models.ErrInvalidJSON) - c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON)) - return - } - } - - log.WithField("payload", string(payload)).Debug("Got payload") - appName := c.Param("app") if appName == "" { // check context, app can be added via special handlers @@ -115,7 +109,6 @@ func handleRequest(c *gin.Context, enqueue models.Enqueue) { envVars := map[string]string{ "METHOD": c.Request.Method, "ROUTE": el.Path, - "PAYLOAD": string(payload), "REQUEST_URL": c.Request.URL.String(), } @@ -148,6 +141,7 @@ func handleRequest(c *gin.Context, enqueue models.Enqueue) { Stderr: stderr, Env: envVars, Memory: el.Memory, + Input: payload, } var err error diff --git a/api/server/runner_test.go b/api/server/runner_test.go index 5deaacd7f..36aaa50e7 100644 --- a/api/server/runner_test.go +++ b/api/server/runner_test.go @@ -29,8 +29,6 @@ func TestRouteRunnerGet(t *testing.T) { {"/route", "", http.StatusBadRequest, models.ErrAppsNotFound}, {"/r/app/route", "", http.StatusNotFound, models.ErrAppsNotFound}, {"/r/myapp/route", "", http.StatusNotFound, models.ErrRunnerRouteNotFound}, - {"/route?payload=test", "", http.StatusBadRequest, models.ErrInvalidJSON}, - {"/r/app/route?payload=test", "", http.StatusBadRequest, models.ErrInvalidJSON}, } { _, rec := routerRequest(t, router, "GET", test.path, nil) @@ -64,8 +62,6 @@ func TestRouteRunnerPost(t *testing.T) { expectedCode int expectedError error }{ - {"/route", `payload`, http.StatusBadRequest, models.ErrInvalidJSON}, - {"/r/app/route", `payload`, http.StatusBadRequest, models.ErrInvalidJSON}, {"/route", `{ "payload": "" }`, http.StatusBadRequest, models.ErrAppsNotFound}, {"/r/app/route", `{ "payload": "" }`, http.StatusNotFound, models.ErrAppsNotFound}, {"/r/myapp/route", `{ "payload": "" }`, http.StatusNotFound, models.ErrRunnerRouteNotFound}, diff --git a/circle.yml b/circle.yml index b6b42fc86..3e83e1a1a 100644 --- a/circle.yml +++ b/circle.yml @@ -8,7 +8,7 @@ machine: GO_PROJECT: ../go/src/github.com/iron-io/$CIRCLE_PROJECT_REPONAME services: - docker - + checkout: post: - mkdir -p "$GH_IRON" diff --git a/data/worker_mq.db b/data/worker_mq.db new file mode 100644 index 0000000000000000000000000000000000000000..53bf56955c683bf03b57256ee22916253f80d81c GIT binary patch literal 32768 zcmeI(y-LGC902fGt%4ue!3XHzD%h?*Oer=)L9IeM_y~gHBDnehzKDafAP9~wU7fwO zIcZ8oGugr~@aH~ra)&o|2TMRT%RWY&vxvq&Efj$e9vx7V?s zlmGz&1PBlyK!5-N0t5&USR8?s;!*y({vYoRgnQ!pf2+9v{rTz}5BVK%pKs4RDH zRT;b0sWf)G%Gm8{WB1aN?&WV&{Ha|%0RjXF5FkK+009C72oNX<#B=`H^Z)o=K&i6= z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNCf GF9KiX(>jX) literal 0 HcmV?d00001 diff --git a/examples/hello/hello.rb b/examples/hello/hello.rb index 1deab24a6..e0eb673a5 100644 --- a/examples/hello/hello.rb +++ b/examples/hello/hello.rb @@ -2,12 +2,8 @@ require 'json' name = "World" -payload = ENV['PAYLOAD'] -if payload == nil || payload == "" - payload = STDIN.read -end -STDERR.puts 'payload: ' + payload.inspect +payload = STDIN.read if payload != "" payload = JSON.parse(payload) name = payload['name'] diff --git a/examples/redis/main.go b/examples/redis/main.go index 416a850ca..b2025ae9c 100644 --- a/examples/redis/main.go +++ b/examples/redis/main.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "io/ioutil" "log" "os" @@ -17,12 +18,12 @@ type payload struct { } func main() { - // Getting ENV variable PAYLOAD - plEnv := os.Getenv("PAYLOAD") + // Getting stdin + plStdin, _ := ioutil.ReadAll(os.Stdin) // Transforming JSON to a *payload var pl payload - err := json.Unmarshal([]byte(plEnv), &pl) + err := json.Unmarshal(plStdin, &pl) if err != nil { log.Println("Invalid payload") log.Fatal(err) diff --git a/glide.lock b/glide.lock index 5c92fa950..a935df09f 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: b6cdce56dc2e984a24f1187c9db387710248cc5f7df40babf8d51998827e4245 -updated: 2016-10-05T19:32:26.015409662+02:00 +hash: 1aa5da2af5bb907bf30c21c74a0e04b9192d5b1821b7902d4f6cdefe536af97c +updated: 2016-10-06T21:48:04.862585926+02:00 imports: - name: github.com/amir/raidman version: c74861fe6a7bb8ede0a010ce4485bdbb4fc4c985 @@ -28,8 +28,6 @@ imports: version: 694ba71e365c6b9aa7472a9ef29bbde3cba6dce2 subpackages: - api/types/filters - - api/types/mount - - api/types/swarm - api/types/versions - opts - pkg/archive @@ -42,6 +40,11 @@ imports: - pkg/promise - pkg/stdcopy - pkg/system +- name: github.com/docker/engine-api + version: 4290f40c056686fcaa5c9caf02eac1dde9315adf + subpackages: + - types/mount + - types/swarm - name: github.com/docker/go-units version: f2145db703495b2e525c59662db69a7344b00bb8 - name: github.com/docker/libtrust @@ -49,7 +52,7 @@ imports: - name: github.com/fsnotify/fsnotify version: c07fd863aee264025aa0ef438bf9d2046c803493 - name: github.com/fsouza/go-dockerclient - version: 519179ce6c6db417b0f092abf47d7d73e4e0d900 + version: 4efaf0ea3c8990e1648f68672d011289f0c0cb0a - name: github.com/garyburd/redigo version: ffa8d46ada782d81cfda81a0fbd9f45ceae448e8 subpackages: @@ -117,11 +120,6 @@ imports: - drivers - drivers/docker - drivers/mock -- name: github.com/iron-io/worker - version: 7a73b8e893c4d7ef4a6cbd3dea94b3d2b883cabc - subpackages: - - common - - common/stats - name: github.com/kr/fs version: 2788f0dbd16903de03cb8186e5c7d97b69ad387b - name: github.com/lib/pq @@ -138,8 +136,6 @@ imports: - jwriter - name: github.com/manucorporat/sse version: ee05b128a739a0fb76c7ebd3ae4810c1de808d6d -- name: github.com/Microsoft/go-winio - version: ce2922f643c8fd76b46cadc7f404a06282678b34 - name: github.com/mitchellh/mapstructure version: ca63d7c062ee3c9f34db231e352b60012b4fd0c1 - name: github.com/opencontainers/runc @@ -160,7 +156,7 @@ imports: - name: github.com/PuerkitoBio/urlesc version: 5bd2802263f21d8788851d5305584c82a5c75d7e - name: github.com/satori/go.uuid - version: b061729afc07e77a8aa4fad0a2fd840958f1942a + version: 879c5887cd475cd7864858769793b2ceb0d44feb - name: github.com/Sirupsen/logrus version: 3ec0642a7fb6488f65b06f9040adc67e3990296a subpackages: @@ -195,7 +191,6 @@ imports: version: 8f0908ab3b2457e2e15403d3697c9ef5cb4b57a9 subpackages: - unix - - windows - name: golang.org/x/text version: 098f51fb687dbaba1f6efabeafbb6461203f9e21 subpackages: diff --git a/glide.yaml b/glide.yaml index 836bc0ec1..c3b5d4d88 100644 --- a/glide.yaml +++ b/glide.yaml @@ -14,9 +14,9 @@ import: - package: github.com/go-openapi/validate - package: github.com/google/btree - package: github.com/iron-io/runner - version: master repo: git@github.com:iron-io/runner.git vcs: git + version: master subpackages: - common - drivers @@ -24,6 +24,7 @@ import: - drivers/mock - package: github.com/lib/pq - package: github.com/satori/go.uuid + version: v1.1.0 - package: github.com/spf13/viper - package: golang.org/x/crypto subpackages: @@ -38,3 +39,4 @@ import: version: ^3.1.0 subpackages: - statsd +- package: github.com/google/btree diff --git a/tool/examples/hello/hello.go b/tool/examples/hello/hello.go index 88bfe9d97..7cd96e4be 100644 --- a/tool/examples/hello/hello.go +++ b/tool/examples/hello/hello.go @@ -1,10 +1,12 @@ package main import ( - "fmt" "os" + "fmt" + "io/ioutil" ) func main() { - fmt.Printf("Hello %v!\n", os.Getenv("PAYLOAD")) + bytes, _ := ioutil.ReadAll(os.Stdin) + fmt.Printf("Hello %v!\n", string(bytes)) }