mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Address more comments. Add docs and clean up comments
This commit is contained in:
@@ -37,11 +37,11 @@ func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//Create the app if it does not exist.
|
// Create the app if it does not exist.
|
||||||
err, resperr = s.createApp(ctx, c, wroute, method)
|
err, resperr = s.ensureApp(ctx, c, wroute, method)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Debug(resperr)
|
log.WithError(err).Debug(resperr)
|
||||||
c.JSON(http.StatusInternalServerError, simpleError(err))
|
handleErrorResponse(c, resperr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +56,8 @@ func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, resp)
|
c.JSON(http.StatusOK, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) createApp(ctx context.Context, c *gin.Context, wroute models.RouteWrapper, method string) (error, error) {
|
// ensureApp will only execute if it is on post or put. Patch is not allowed to create apps.
|
||||||
|
func (s *Server) ensureApp(ctx context.Context, c *gin.Context, wroute models.RouteWrapper, method string) (error, error) {
|
||||||
if !(method == http.MethodPost || method == http.MethodPut) {
|
if !(method == http.MethodPost || method == http.MethodPut) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@@ -66,7 +67,7 @@ func (s *Server) createApp(ctx context.Context, c *gin.Context, wroute models.Ro
|
|||||||
if err != nil && err != models.ErrAppsNotFound {
|
if err != nil && err != models.ErrAppsNotFound {
|
||||||
return err, models.ErrAppsGet
|
return err, models.ErrAppsGet
|
||||||
} else if app == nil {
|
} else if app == nil {
|
||||||
// Create a new application and add the route to that new application
|
// Create a new application
|
||||||
newapp := &models.App{Name: wroute.Route.AppName}
|
newapp := &models.App{Name: wroute.Route.AppName}
|
||||||
if err = newapp.Validate(); err != nil {
|
if err = newapp.Validate(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -74,7 +75,7 @@ func (s *Server) createApp(ctx context.Context, c *gin.Context, wroute models.Ro
|
|||||||
|
|
||||||
err = s.FireBeforeAppCreate(ctx, newapp)
|
err = s.FireBeforeAppCreate(ctx, newapp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err, models.ErrAppsCreate
|
return err, models.ErrRoutesCreate
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = s.Datastore.InsertApp(ctx, newapp)
|
_, err = s.Datastore.InsertApp(ctx, newapp)
|
||||||
@@ -91,6 +92,10 @@ func (s *Server) createApp(ctx context.Context, c *gin.Context, wroute models.Ro
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* bindAndValidate binds the RouteWrapper to the json from the request and validates that it is correct.
|
||||||
|
If it is a put or patch it makes sure that the path in the url matches the provideed one in the body.
|
||||||
|
Defaults are set and if patch skipZero is true for validating the RouteWrapper
|
||||||
|
*/
|
||||||
func (s *Server) bindAndValidate(ctx context.Context, c *gin.Context, method string, wroute *models.RouteWrapper) (error, error) {
|
func (s *Server) bindAndValidate(ctx context.Context, c *gin.Context, method string, wroute *models.RouteWrapper) (error, error) {
|
||||||
err := c.BindJSON(wroute)
|
err := c.BindJSON(wroute)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -119,6 +124,7 @@ func (s *Server) bindAndValidate(ctx context.Context, c *gin.Context, method str
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// updateOrInsertRoute will either update or insert the route respective the method.
|
||||||
func (s *Server) updateOrInsertRoute(ctx context.Context, method string, wroute models.RouteWrapper) (routeResponse, error) {
|
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
|
var err error
|
||||||
@@ -135,6 +141,7 @@ func (s *Server) updateOrInsertRoute(ctx context.Context, method string, wroute
|
|||||||
route, err = s.Datastore.InsertRoute(ctx, wroute.Route)
|
route, err = s.Datastore.InsertRoute(ctx, wroute.Route)
|
||||||
}
|
}
|
||||||
case http.MethodPatch:
|
case http.MethodPatch:
|
||||||
|
// When patching if there is an error around the app we will return one and the update fails.
|
||||||
route, err = s.Datastore.UpdateRoute(ctx, wroute.Route)
|
route, err = s.Datastore.UpdateRoute(ctx, wroute.Route)
|
||||||
resp = up
|
resp = up
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user