From e07aaa2f99a839dbc1652dd62eb40d4dcdbefbe5 Mon Sep 17 00:00:00 2001 From: Nils Ponsard Date: Wed, 22 May 2024 16:09:02 +0200 Subject: [PATCH] feat: return the body after executing the function --- go.sum | 10 ++++++---- routes/function/controller.go | 27 ++++++++++++++++++++++++--- routes/function/entity.go | 10 ++++++++++ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/go.sum b/go.sum index 1747d7e..84cac92 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= github.com/bytedance/sonic v1.11.3 h1:jRN+yEjakWh8aK5FzrciUHG8OFXK+4/KrAX/ysEtHAA= @@ -17,10 +21,10 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= -github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -58,8 +62,6 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= -github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= -github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/compress v1.17.6 h1:60eq2E/jlfwQXtvZEeBUYADs+BwKBWURIY+Gj2eRGjI= diff --git a/routes/function/controller.go b/routes/function/controller.go index 984ed5e..eb93d89 100644 --- a/routes/function/controller.go +++ b/routes/function/controller.go @@ -229,6 +229,7 @@ func (c *Controller) RunFunction(ctx *gin.Context) { if fnState.Status != int(database.FnReady) { log.Println("Waiting for function", fn.ID, "to be ready") + time.Sleep(100 * time.Millisecond) // we will try 5 times to check if the instance is ready for attempts := 0; attempts < 5; attempts++ { @@ -265,7 +266,7 @@ func (c *Controller) RunFunction(ctx *gin.Context) { return } - _, err = http.Post( + function_result, err := http.Post( fmt.Sprint(string(fnState.Address), ":", fnState.Port, "/execute"), "application/json", ctx.Request.Body, @@ -276,8 +277,6 @@ func (c *Controller) RunFunction(ctx *gin.Context) { ctx.AbortWithStatusJSON(500, gin.H{"error": err.Error()}) _ = c.Scheduler.SetStatus(stateID, database.FnUnknownState) return - } else { - ctx.Status(204) } err = c.Scheduler.SetStatus(stateID, database.FnReady) @@ -288,4 +287,26 @@ func (c *Controller) RunFunction(ctx *gin.Context) { ) log.Println(err.Error()) } + + var ( + executionResult ExecutionResultDTO + body []byte + ) + + body, err = io.ReadAll(function_result.Body) + + if err != nil { + ctx.AbortWithStatusJSON(500, gin.H{"error": err.Error()}) + log.Println("error reading body", err) + return + } + err = json.Unmarshal(body, &executionResult) + + if err != nil { + ctx.AbortWithStatusJSON(500, gin.H{"error": err.Error()}) + log.Println("error unmarshalling body", err) + return + } + + ctx.JSON(executionResult.Response.Status, executionResult.Response.Body) } diff --git a/routes/function/entity.go b/routes/function/entity.go index a6a89a5..cdd0391 100644 --- a/routes/function/entity.go +++ b/routes/function/entity.go @@ -16,3 +16,13 @@ type GetFunctionDTO struct { BuildTimestamp int64 `json:"build_timestamp"` OwnerID int `json:"owner_id"` } + +type ExecutionResultDTO struct { + Time int64 `json:"time"` + Response ExecutionResponse `json:"response"` +} +type ExecutionResponse struct { + Status int `json:"status"` + Body interface{} `json:"body"` + Headers map[string]string `json:"headers"` +}