mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Refactor seperate out logic
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gitlab-odx.oracle.com/odx/functions/api"
|
"gitlab-odx.oracle.com/odx/functions/api"
|
||||||
"gitlab-odx.oracle.com/odx/functions/api/models"
|
"gitlab-odx.oracle.com/odx/functions/api/models"
|
||||||
@@ -27,19 +28,82 @@ func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
|
|||||||
|
|
||||||
var wroute models.RouteWrapper
|
var wroute models.RouteWrapper
|
||||||
|
|
||||||
err := c.BindJSON(&wroute)
|
err := bindAndValidate(c, log, method, &wroute)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, simpleError(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//Create the app if it does not exist.
|
||||||
|
s.createApp(ctx, c, log, wroute, method)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, simpleError(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := s.updateOrInsertRoute(ctx, method, wroute)
|
||||||
|
if err != nil {
|
||||||
|
handleErrorResponse(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.cacheRefresh(resp.Route)
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) createApp(ctx context.Context, c *gin.Context, log logrus.FieldLogger, wroute models.RouteWrapper, method string) error {
|
||||||
|
if !(method == http.MethodPost || method == http.MethodPut) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var app *models.App
|
||||||
|
var err error
|
||||||
|
app, err = s.Datastore.GetApp(ctx, wroute.Route.AppName)
|
||||||
|
if err != nil && err != models.ErrAppsNotFound {
|
||||||
|
log.WithError(err).Error(models.ErrAppsGet)
|
||||||
|
return models.ErrAppsGet
|
||||||
|
} else if app == nil {
|
||||||
|
// Create a new application and add the route to that new application
|
||||||
|
newapp := &models.App{Name: wroute.Route.AppName}
|
||||||
|
if err = newapp.Validate(); err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.FireBeforeAppCreate(ctx, newapp)
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Error(models.ErrAppsCreate)
|
||||||
|
return models.ErrAppsCreate
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = s.Datastore.InsertApp(ctx, newapp)
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Error(models.ErrRoutesCreate)
|
||||||
|
return models.ErrRoutesCreate
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.FireAfterAppCreate(ctx, newapp)
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Error(models.ErrRoutesCreate)
|
||||||
|
return models.ErrRoutesCreate
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func bindAndValidate(c *gin.Context, log logrus.FieldLogger, method string, wroute *models.RouteWrapper) error {
|
||||||
|
err := c.BindJSON(wroute)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Debug(models.ErrInvalidJSON)
|
log.WithError(err).Debug(models.ErrInvalidJSON)
|
||||||
c.JSON(http.StatusBadRequest, simpleError(models.ErrInvalidJSON))
|
return models.ErrInvalidJSON
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if wroute.Route == nil {
|
if wroute.Route == nil {
|
||||||
log.WithError(err).Debug(models.ErrRoutesMissingNew)
|
log.WithError(err).Debug(models.ErrRoutesMissingNew)
|
||||||
c.JSON(http.StatusBadRequest, simpleError(models.ErrRoutesMissingNew))
|
return models.ErrRoutesMissingNew
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
// MAKE SIMPLEER TO READ / REFACTOR then help denis with the ci errors.
|
||||||
wroute.Route.AppName = c.MustGet(api.AppName).(string)
|
wroute.Route.AppName = c.MustGet(api.AppName).(string)
|
||||||
|
|
||||||
if method == http.MethodPut || method == http.MethodPatch {
|
if method == http.MethodPut || method == http.MethodPatch {
|
||||||
@@ -47,8 +111,7 @@ func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
|
|||||||
|
|
||||||
if wroute.Route.Path != "" && wroute.Route.Path != p {
|
if wroute.Route.Path != "" && wroute.Route.Path != p {
|
||||||
log.Debug(models.ErrRoutesPathImmutable)
|
log.Debug(models.ErrRoutesPathImmutable)
|
||||||
c.JSON(http.StatusBadRequest, simpleError(models.ErrRoutesPathImmutable))
|
return models.ErrRoutesPathImmutable
|
||||||
return
|
|
||||||
}
|
}
|
||||||
wroute.Route.Path = p
|
wroute.Route.Path = p
|
||||||
}
|
}
|
||||||
@@ -57,55 +120,16 @@ func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
|
|||||||
|
|
||||||
if err = wroute.Validate(method == http.MethodPost); err != nil {
|
if err = wroute.Validate(method == http.MethodPost); err != nil {
|
||||||
log.WithError(err).Debug(models.ErrRoutesCreate)
|
log.WithError(err).Debug(models.ErrRoutesCreate)
|
||||||
c.JSON(http.StatusBadRequest, simpleError(err))
|
return err
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//Create the app if it does not exist.
|
|
||||||
if method == http.MethodPost || method == http.MethodPut {
|
|
||||||
var app *models.App
|
|
||||||
app, err = s.Datastore.GetApp(ctx, wroute.Route.AppName)
|
|
||||||
if err != nil && err != models.ErrAppsNotFound {
|
|
||||||
log.WithError(err).Error(models.ErrAppsGet)
|
|
||||||
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsGet))
|
|
||||||
return
|
|
||||||
} else if app == nil {
|
|
||||||
// Create a new application and add the route to that new application
|
|
||||||
newapp := &models.App{Name: wroute.Route.AppName}
|
|
||||||
if err = newapp.Validate(); err != nil {
|
|
||||||
log.Error(err)
|
|
||||||
c.JSON(http.StatusInternalServerError, simpleError(err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = s.FireBeforeAppCreate(ctx, newapp)
|
|
||||||
if err != nil {
|
|
||||||
log.WithError(err).Error(models.ErrAppsCreate)
|
|
||||||
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = s.Datastore.InsertApp(ctx, newapp)
|
|
||||||
if err != nil {
|
|
||||||
log.WithError(err).Error(models.ErrRoutesCreate)
|
|
||||||
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = s.FireAfterAppCreate(ctx, newapp)
|
|
||||||
if err != nil {
|
|
||||||
log.WithError(err).Error(models.ErrRoutesCreate)
|
|
||||||
c.JSON(http.StatusInternalServerError, simpleError(ErrInternalServerError))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) updateOrInsertRoute(ctx context.Context, method string, wroute models.RouteWrapper) (routeResponse, error) {
|
||||||
var route *models.Route
|
var route *models.Route
|
||||||
|
var err error
|
||||||
resp := routeResponse{"Route successfully created", route}
|
resp := routeResponse{"Route successfully created", nil}
|
||||||
up := routeResponse{"Route successfully updated", route}
|
up := routeResponse{"Route successfully updated", nil}
|
||||||
|
|
||||||
switch method {
|
switch method {
|
||||||
case http.MethodPost:
|
case http.MethodPost:
|
||||||
@@ -120,13 +144,6 @@ func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
|
|||||||
route, err = s.Datastore.UpdateRoute(ctx, wroute.Route)
|
route, err = s.Datastore.UpdateRoute(ctx, wroute.Route)
|
||||||
resp = up
|
resp = up
|
||||||
}
|
}
|
||||||
|
resp.Route = route
|
||||||
if err != nil {
|
return resp, err
|
||||||
handleErrorResponse(c, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
s.cacheRefresh(route)
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, resp)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -287,7 +287,7 @@ paths:
|
|||||||
schema:
|
schema:
|
||||||
$ref: '#/definitions/Error'
|
$ref: '#/definitions/Error'
|
||||||
404:
|
404:
|
||||||
description: App/ Route does not exist.
|
description: App / Route does not exist.
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/definitions/Error'
|
$ref: '#/definitions/Error'
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user