Updated ctx to MiddlewareContext in various places.

This commit is contained in:
Travis Reeder
2017-07-12 21:40:16 -07:00
parent 7a81aa5761
commit 86b93e27f7
6 changed files with 20 additions and 23 deletions

View File

@@ -1,24 +1,22 @@
package server
import (
"context"
"gitlab-odx.oracle.com/odx/functions/api/models"
)
type AppListener interface {
// BeforeAppCreate called right before creating App in the database
BeforeAppCreate(ctx context.Context, app *models.App) error
BeforeAppCreate(ctx MiddlewareContext, app *models.App) error
// AfterAppCreate called after creating App in the database
AfterAppCreate(ctx context.Context, app *models.App) error
AfterAppCreate(ctx MiddlewareContext, app *models.App) error
// BeforeAppUpdate called right before updating App in the database
BeforeAppUpdate(ctx context.Context, app *models.App) error
BeforeAppUpdate(ctx MiddlewareContext, app *models.App) error
// AfterAppUpdate called after updating App in the database
AfterAppUpdate(ctx context.Context, app *models.App) error
AfterAppUpdate(ctx MiddlewareContext, app *models.App) error
// BeforeAppDelete called right before deleting App in the database
BeforeAppDelete(ctx context.Context, app *models.App) error
BeforeAppDelete(ctx MiddlewareContext, app *models.App) error
// AfterAppDelete called after deleting App in the database
AfterAppDelete(ctx context.Context, app *models.App) error
AfterAppDelete(ctx MiddlewareContext, app *models.App) error
}
// AddAppCreateListener adds a listener that will be notified on App created.
@@ -26,7 +24,7 @@ func (s *Server) AddAppListener(listener AppListener) {
s.appListeners = append(s.appListeners, listener)
}
func (s *Server) FireBeforeAppCreate(ctx context.Context, app *models.App) error {
func (s *Server) FireBeforeAppCreate(ctx MiddlewareContext, app *models.App) error {
for _, l := range s.appListeners {
err := l.BeforeAppCreate(ctx, app)
if err != nil {
@@ -36,7 +34,7 @@ func (s *Server) FireBeforeAppCreate(ctx context.Context, app *models.App) error
return nil
}
func (s *Server) FireAfterAppCreate(ctx context.Context, app *models.App) error {
func (s *Server) FireAfterAppCreate(ctx MiddlewareContext, app *models.App) error {
for _, l := range s.appListeners {
err := l.AfterAppCreate(ctx, app)
if err != nil {
@@ -46,7 +44,7 @@ func (s *Server) FireAfterAppCreate(ctx context.Context, app *models.App) error
return nil
}
func (s *Server) FireBeforeAppUpdate(ctx context.Context, app *models.App) error {
func (s *Server) FireBeforeAppUpdate(ctx MiddlewareContext, app *models.App) error {
for _, l := range s.appListeners {
err := l.BeforeAppUpdate(ctx, app)
if err != nil {
@@ -56,7 +54,7 @@ func (s *Server) FireBeforeAppUpdate(ctx context.Context, app *models.App) error
return nil
}
func (s *Server) FireAfterAppUpdate(ctx context.Context, app *models.App) error {
func (s *Server) FireAfterAppUpdate(ctx MiddlewareContext, app *models.App) error {
for _, l := range s.appListeners {
err := l.AfterAppUpdate(ctx, app)
if err != nil {
@@ -66,7 +64,7 @@ func (s *Server) FireAfterAppUpdate(ctx context.Context, app *models.App) error
return nil
}
func (s *Server) FireBeforeAppDelete(ctx context.Context, app *models.App) error {
func (s *Server) FireBeforeAppDelete(ctx MiddlewareContext, app *models.App) error {
for _, l := range s.appListeners {
err := l.BeforeAppDelete(ctx, app)
if err != nil {
@@ -76,7 +74,7 @@ func (s *Server) FireBeforeAppDelete(ctx context.Context, app *models.App) error
return nil
}
func (s *Server) FireAfterAppDelete(ctx context.Context, app *models.App) error {
func (s *Server) FireAfterAppDelete(ctx MiddlewareContext, app *models.App) error {
for _, l := range s.appListeners {
err := l.AfterAppDelete(ctx, app)
if err != nil {

View File

@@ -1,7 +1,6 @@
package server
import (
"context"
"net/http"
"github.com/gin-gonic/gin"
@@ -10,7 +9,7 @@ import (
)
func (s *Server) handleAppCreate(c *gin.Context) {
ctx := c.MustGet("ctx").(context.Context)
ctx := c.MustGet("mctx").(MiddlewareContext)
log := common.Logger(ctx)
var wapp models.AppWrapper

View File

@@ -1,7 +1,6 @@
package server
import (
"context"
"net/http"
"github.com/gin-gonic/gin"
@@ -11,7 +10,7 @@ import (
)
func (s *Server) handleAppDelete(c *gin.Context) {
ctx := c.MustGet("ctx").(context.Context)
ctx := c.MustGet("mctx").(MiddlewareContext)
log := common.Logger(ctx)
app := &models.App{Name: c.MustGet(api.AppName).(string)}

View File

@@ -1,7 +1,6 @@
package server
import (
"context"
"net/http"
"github.com/gin-gonic/gin"
@@ -11,7 +10,7 @@ import (
)
func (s *Server) handleAppUpdate(c *gin.Context) {
ctx := c.MustGet("ctx").(context.Context)
ctx := c.MustGet("mctx").(MiddlewareContext)
log := common.Logger(ctx)
wapp := models.AppWrapper{}

View File

@@ -100,6 +100,8 @@ func (s *Server) middlewareWrapperFunc(ctx context.Context) gin.HandlerFunc {
}
ctx = c.MustGet("ctx").(context.Context)
fctx := &middlewareContextImpl{Context: ctx}
// add this context to gin context so we can grab it later
c.Set("mctx", fctx)
fctx.index = -1
fctx.ginContext = c
fctx.middlewares = s.middlewares

View File

@@ -24,7 +24,7 @@ import (
Patch accepts partial updates / skips validation of zero values.
*/
func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
ctx := c.MustGet("ctx").(context.Context)
ctx := c.MustGet("mctx").(MiddlewareContext)
log := common.Logger(ctx)
method := strings.ToUpper(c.Request.Method)
@@ -38,7 +38,7 @@ func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
}
// Create the app if it does not exist.
err, resperr = s.ensureApp(ctx, c, &wroute, method)
err, resperr = s.ensureApp(ctx, &wroute, method)
if err != nil || resperr != nil {
log.WithError(err).Debug(resperr)
handleErrorResponse(c, resperr)
@@ -57,7 +57,7 @@ func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
}
// 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) {
func (s *Server) ensureApp(ctx MiddlewareContext, wroute *models.RouteWrapper, method string) (error, error) {
if !(method == http.MethodPost || method == http.MethodPut) {
return nil, nil
}