github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/api/server/apps_create.go (about) 1 package server 2 3 import ( 4 "context" 5 "net/http" 6 7 "github.com/gin-gonic/gin" 8 "github.com/iron-io/functions/api/models" 9 "github.com/iron-io/runner/common" 10 ) 11 12 func (s *Server) handleAppCreate(c *gin.Context) { 13 ctx := c.MustGet("ctx").(context.Context) 14 log := common.Logger(ctx) 15 16 var wapp models.AppWrapper 17 18 err := c.BindJSON(&wapp) 19 if err != nil { 20 log.WithError(err).Debug(models.ErrInvalidJSON) 21 c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON)) 22 return 23 } 24 25 if wapp.App == nil { 26 log.Debug(models.ErrAppsMissingNew) 27 c.JSON(http.StatusBadRequest, simpleError(models.ErrAppsMissingNew)) 28 return 29 } 30 31 if err := wapp.Validate(); err != nil { 32 log.Error(err) 33 c.JSON(http.StatusInternalServerError, simpleError(err)) 34 return 35 } 36 37 err = s.FireBeforeAppCreate(ctx, wapp.App) 38 if err != nil { 39 log.WithError(err).Error(models.ErrAppsCreate) 40 c.JSON(http.StatusInternalServerError, simpleError(err)) 41 return 42 } 43 44 app, err := s.Datastore.InsertApp(ctx, wapp.App) 45 if err != nil { 46 handleErrorResponse(c, err) 47 return 48 } 49 50 err = s.FireAfterAppCreate(ctx, wapp.App) 51 if err != nil { 52 log.WithError(err).Error(models.ErrAppsCreate) 53 c.JSON(http.StatusInternalServerError, simpleError(err)) 54 return 55 } 56 57 c.JSON(http.StatusOK, appResponse{"App successfully created", app}) 58 }