mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
we had this _almost_ right, in that we were trying, but we weren't masking the error from the user response for any error we don't intend to show. this also adds a stack trace from any internal server errors, so that we might be able to track them down in the future (looking at you, 'context deadline exceeded'). in addition, this adds a new `models.APIError` interface which all of the errors in `models` now implement, and can be caught easily / added to easily. the front end now does no status rewriting based on api errors, now when we get a non-nil error we can call `handleResponse(c, err)` with it and if it's a proper error, return it to the user with the right status code, otherwise log a stack trace and return `internal server error`. this cleans up a lot of the front end code. also rewrites start task ctx deadline exceeded as timeout. with iw we had async tasks so we could start the clock later and it didn't matter, but now with sync tasks time out sometimes just making docker calls, and we want the task status to show up as timed out. we may want to just catch all this above in addition to this, but this seems like the right thing to do. remove squishing together errors. this was weird, now we return the first error for the purposes of using the new err interface. removed a lot of 5xx errors that really should have been 4xx errors. changed some of the 400 errors to 409 errors, since they are from sending in conflicting info and not a malformed request. removed unused errors / useless errors (many were used for logging, and didn't provide any context. now with stack traces we don't need context as much in the logs).
142 lines
4.0 KiB
Go
142 lines
4.0 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gitlab-odx.oracle.com/odx/functions/api"
|
|
"gitlab-odx.oracle.com/odx/functions/api/models"
|
|
)
|
|
|
|
/* handleRouteCreateOrUpdate is used to handle POST PUT and PATCH for routes.
|
|
Post will only create route if its not there and create app if its not.
|
|
create only
|
|
Post does not skip validation of zero values
|
|
Put will create app if its not there and if route is there update if not it will create new route.
|
|
update if exists or create if not exists
|
|
Put does not skip validation of zero values
|
|
Patch will not create app if it does not exist since the route needs to exist as well...
|
|
update only
|
|
Patch accepts partial updates / skips validation of zero values.
|
|
*/
|
|
func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
|
|
ctx := c.MustGet("mctx").(MiddlewareContext)
|
|
method := strings.ToUpper(c.Request.Method)
|
|
|
|
var wroute models.RouteWrapper
|
|
|
|
err := s.bindAndValidate(ctx, c, method, &wroute)
|
|
if err != nil {
|
|
handleErrorResponse(c, err)
|
|
return
|
|
}
|
|
|
|
// Create the app if it does not exist.
|
|
err = s.ensureApp(ctx, &wroute, method)
|
|
if err != nil {
|
|
handleErrorResponse(c, 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)
|
|
}
|
|
|
|
// ensureApp will only execute if it is on post or put. Patch is not allowed to create apps.
|
|
func (s *Server) ensureApp(ctx MiddlewareContext, wroute *models.RouteWrapper, method string) error {
|
|
if !(method == http.MethodPost || method == http.MethodPut) {
|
|
return nil
|
|
}
|
|
app, err := s.Datastore.GetApp(ctx, wroute.Route.AppName)
|
|
if err != nil && err != models.ErrAppsNotFound {
|
|
return err
|
|
} else if app == nil {
|
|
// Create a new application
|
|
newapp := &models.App{Name: wroute.Route.AppName}
|
|
if err = newapp.Validate(); err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.FireBeforeAppCreate(ctx, newapp)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = s.Datastore.InsertApp(ctx, newapp)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.FireAfterAppCreate(ctx, newapp)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
}
|
|
return 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 {
|
|
err := c.BindJSON(wroute)
|
|
if err != nil {
|
|
return models.ErrInvalidJSON
|
|
}
|
|
|
|
if wroute.Route == nil {
|
|
return models.ErrRoutesMissingNew
|
|
}
|
|
wroute.Route.AppName = c.MustGet(api.AppName).(string)
|
|
|
|
if method == http.MethodPut || method == http.MethodPatch {
|
|
p := path.Clean(c.MustGet(api.Path).(string))
|
|
|
|
if wroute.Route.Path != "" && wroute.Route.Path != p {
|
|
return models.ErrRoutesPathImmutable
|
|
}
|
|
wroute.Route.Path = p
|
|
}
|
|
|
|
wroute.Route.SetDefaults()
|
|
|
|
return wroute.Validate(method == http.MethodPatch)
|
|
}
|
|
|
|
// 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) {
|
|
var route *models.Route
|
|
var err error
|
|
resp := routeResponse{"Route successfully created", nil}
|
|
up := routeResponse{"Route successfully updated", nil}
|
|
|
|
switch method {
|
|
case http.MethodPost:
|
|
route, err = s.Datastore.InsertRoute(ctx, wroute.Route)
|
|
case http.MethodPut:
|
|
route, err = s.Datastore.UpdateRoute(ctx, wroute.Route)
|
|
if err == models.ErrRoutesNotFound {
|
|
// try insert then
|
|
route, err = s.Datastore.InsertRoute(ctx, wroute.Route)
|
|
}
|
|
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)
|
|
resp = up
|
|
}
|
|
resp.Route = route
|
|
return resp, err
|
|
}
|