From 941ce3efe142fe19639e3d0d52e995f0cd779ff5 Mon Sep 17 00:00:00 2001 From: Nils Ponsard Date: Wed, 22 May 2024 14:07:23 +0200 Subject: [PATCH] feat: return more info about the function --- go.mod | 5 +---- routes/function/controller.go | 36 ++++++++++++++++++----------------- routes/function/entity.go | 12 +++++++++++- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/go.mod b/go.mod index 0ac237b..2f20ef0 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/google/uuid v1.6.0 github.com/joho/godotenv v1.5.1 github.com/minio/minio-go/v7 v7.0.69 - github.com/google/uuid v1.6.0 + github.com/redis/go-redis/v9 v9.5.1 gorm.io/gorm v1.25.8 ) @@ -17,8 +17,6 @@ require ( github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dustin/go-humanize v1.0.1 // indirect - github.com/caarlos0/env/v10 v10.0.0 // indirect - github.com/google/uuid v1.6.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect github.com/jackc/pgx/v5 v5.5.5 // indirect @@ -27,7 +25,6 @@ require ( github.com/kr/text v0.2.0 // indirect github.com/minio/md5-simd v1.1.2 // indirect github.com/minio/sha256-simd v1.0.1 // indirect - github.com/redis/go-redis/v9 v9.5.1 // indirect github.com/rogpeppe/go-internal v1.12.0 // indirect github.com/rs/xid v1.5.0 // indirect golang.org/x/sync v0.6.0 // indirect diff --git a/routes/function/controller.go b/routes/function/controller.go index b0d6fa2..984ed5e 100644 --- a/routes/function/controller.go +++ b/routes/function/controller.go @@ -53,11 +53,14 @@ func (cont *Controller) GetOneFunction(c *gin.Context) { return } - dto := FunctionDTO{ - Name: function.Name, - Description: function.Description, - Language: function.Language, - Files: files, + dto := GetFunctionDTO{ + Name: function.Name, + Description: function.Description, + Language: function.Language, + Files: files, + Built: function.Built, + BuildTimestamp: function.BuildTimestamp, + //OwnerID: function.OwnerID, } c.JSON(http.StatusOK, dto) @@ -65,7 +68,7 @@ func (cont *Controller) GetOneFunction(c *gin.Context) { func (cont *Controller) PostFunction(c *gin.Context) { id := uuid.New() - var dto FunctionDTO + var dto CreateFunctionDTO if err := c.ShouldBindJSON(&dto); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return @@ -90,7 +93,7 @@ func (cont *Controller) PostFunction(c *gin.Context) { } func (cont *Controller) PutFunction(c *gin.Context) { - var json FunctionDTO + var json CreateFunctionDTO if err := c.ShouldBindJSON(&json); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return @@ -173,7 +176,7 @@ type BuilderRequest struct { } func (c *Controller) RunFunction(ctx *gin.Context) { - fnID, err := uuid.Parse(ctx.Param("id")) + fnID, err := uuid.Parse(ctx.Param("id")) if err != nil { ctx.AbortWithStatusJSON(400, gin.H{"error": "Invalid function ID"}) @@ -215,7 +218,7 @@ func (c *Controller) RunFunction(ctx *gin.Context) { log.Println(err.Error()) ctx.AbortWithStatusJSON(500, gin.H{"error": "Could not cold start the function"}) return - } + } } else if err != nil { // else if the error is not a record not found, we return an error log.Println(err.Error()) ctx.AbortWithStatusJSON(500, gin.H{"error": "Could not cold start the function"}) @@ -234,16 +237,16 @@ func (c *Controller) RunFunction(ctx *gin.Context) { // we check if it is ready fnState, err := c.Scheduler.GetStateByID(stateID) - if err != nil { + if err != nil { log.Println(err.Error()) ctx.AbortWithStatusJSON(500, gin.H{"Could not cold start the function": err.Error()}) return } - if fnState.Status == int(database.FnReady) { + if fnState.Status == int(database.FnReady) { log.Println("Function", fnID, "is ready") - break - }; + break + } } // if even after 5 attempts the function is not ready, we return an error @@ -257,11 +260,11 @@ func (c *Controller) RunFunction(ctx *gin.Context) { err = c.Scheduler.SetStatus(stateID, database.FnRunning) if err != nil { - log.Println(fmt.Sprint("Could not update state of VM", fnState.ID ,": ", err.Error())) + log.Println(fmt.Sprint("Could not update state of VM", fnState.ID, ": ", err.Error())) ctx.AbortWithStatusJSON(500, gin.H{"error": "Cannot update function's status"}) return } - + _, err = http.Post( fmt.Sprint(string(fnState.Address), ":", fnState.Port, "/execute"), "application/json", @@ -281,9 +284,8 @@ func (c *Controller) RunFunction(ctx *gin.Context) { if err != nil { log.Println( - fmt.Sprint("Could not update state of VM", fnState.ID ,": ", err.Error()), + fmt.Sprint("Could not update state of VM", fnState.ID, ": ", err.Error()), ) log.Println(err.Error()) } } - diff --git a/routes/function/entity.go b/routes/function/entity.go index 2aaef91..a6a89a5 100644 --- a/routes/function/entity.go +++ b/routes/function/entity.go @@ -1,8 +1,18 @@ package function -type FunctionDTO struct { +type CreateFunctionDTO struct { Name string `json:"name" binding:"required"` Description string `json:"description" binding:"required"` Language string `json:"language" binding:"required"` Files map[string]string `json:"files" binding:"required"` } + +type GetFunctionDTO struct { + Name string `json:"name"` + Description string `json:"description"` + Language string `json:"language"` + Files map[string]string `json:"files"` + Built bool `json:"built"` + BuildTimestamp int64 `json:"build_timestamp"` + OwnerID int `json:"owner_id"` +}