mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
80 lines
1.9 KiB
Go
80 lines
1.9 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/iron-io/functions/api/models"
|
|
"github.com/iron-io/functions/api/runner"
|
|
titancommon "github.com/iron-io/titan/common"
|
|
)
|
|
|
|
func handleRouteCreate(c *gin.Context) {
|
|
ctx := c.MustGet("ctx").(context.Context)
|
|
log := titancommon.Logger(ctx)
|
|
|
|
var wroute models.RouteWrapper
|
|
|
|
err := c.BindJSON(&wroute)
|
|
if err != nil {
|
|
log.WithError(err).Error(models.ErrInvalidJSON)
|
|
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
|
|
return
|
|
}
|
|
|
|
if wroute.Route == nil {
|
|
log.WithError(err).Error(models.ErrInvalidJSON)
|
|
c.JSON(http.StatusBadRequest, simpleError(models.ErrRoutesMissingNew))
|
|
return
|
|
}
|
|
|
|
wroute.Route.AppName = c.Param("app")
|
|
|
|
if err := wroute.Validate(); err != nil {
|
|
log.Error(err)
|
|
c.JSON(http.StatusInternalServerError, simpleError(err))
|
|
return
|
|
}
|
|
|
|
err = Api.Runner.EnsureUsableImage(ctx, &runner.Config{
|
|
Image: wroute.Route.Image,
|
|
})
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, simpleError(models.ErrUsableImage))
|
|
return
|
|
}
|
|
|
|
app, err := Api.Datastore.GetApp(wroute.Route.AppName)
|
|
if err != nil {
|
|
log.WithError(err).Error(models.ErrAppsGet)
|
|
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsGet))
|
|
return
|
|
}
|
|
if app == nil {
|
|
newapp := &models.App{Name: wroute.Route.AppName}
|
|
if err := newapp.Validate(); err != nil {
|
|
log.Error(err)
|
|
c.JSON(http.StatusInternalServerError, simpleError(err))
|
|
return
|
|
}
|
|
|
|
app, err = Api.Datastore.StoreApp(newapp)
|
|
if err != nil {
|
|
log.WithError(err).Error(models.ErrAppsCreate)
|
|
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsCreate))
|
|
return
|
|
}
|
|
}
|
|
|
|
_, err = Api.Datastore.StoreRoute(wroute.Route)
|
|
if err != nil {
|
|
log.WithError(err).Error(models.ErrRoutesCreate)
|
|
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRoutesCreate))
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, routeResponse{"Route successfully created", wroute})
|
|
}
|