diff --git a/api/server/runner.go b/api/server/runner.go index 05b894302..2622c8656 100644 --- a/api/server/runner.go +++ b/api/server/runner.go @@ -27,16 +27,15 @@ func (s *Server) handleSpecial(c *gin.Context) { ctx = context.WithValue(ctx, "appName", "") ctx = context.WithValue(ctx, "routePath", c.Request.URL.Path) - c.Set("ctx", ctx) - err := s.UseSpecialHandlers(c) + ctx, err := s.UseSpecialHandlers(ctx, c.Request, c.Writer) if err != nil { log.WithError(err).Errorln("Error using special handler!") c.JSON(http.StatusInternalServerError, simpleError(errors.New("Failed to run function"))) return } - ctx = c.MustGet("ctx").(context.Context) + c.Set("ctx", ctx) if ctx.Value("appName").(string) == "" { log.WithError(err).Errorln("Specialhandler returned empty app name") c.JSON(http.StatusBadRequest, simpleError(models.ErrRunnerRouteNotFound)) diff --git a/api/server/special_handler.go b/api/server/special_handler.go index 9a3da827b..c32affd6f 100644 --- a/api/server/special_handler.go +++ b/api/server/special_handler.go @@ -4,8 +4,6 @@ import ( "net/http" "context" - "github.com/gin-gonic/gin" - "github.com/iron-io/functions/api/models" ) type SpecialHandler interface { @@ -13,7 +11,6 @@ type SpecialHandler interface { } // Each handler can modify the context here so when it gets passed along, it will use the new info. -// Not using Gin's Context so we don't lock ourselves into Gin, this is a subset of the Gin context. type HandlerContext interface { // Context return the context object Context() context.Context @@ -21,28 +18,51 @@ type HandlerContext interface { // Request returns the underlying http.Request object Request() *http.Request - // Datastore returns the models.Datastore object. Not that this has arbitrary key value store methods that can be used to store extra data - Datastore() models.Datastore + // Response returns the http.ResponseWriter + Response() http.ResponseWriter - // Set and Get values on the context, this can be useful to change behavior for the rest of the request + // Overwrite value in the context Set(key string, value interface{}) - Get(key string) (value interface{}, exists bool) +} + +type SpecialHandlerContext struct { + request *http.Request + response http.ResponseWriter + ctx context.Context +} + +func (c *SpecialHandlerContext) Context() context.Context { + return c.ctx +} + +func (c *SpecialHandlerContext) Request() *http.Request { + return c.request +} + +func (c *SpecialHandlerContext) Response() http.ResponseWriter { + return c.response +} + +func (c *SpecialHandlerContext) Set(key string, value interface{}) { + c.ctx = context.WithValue(c.ctx, key, value) } func (s *Server) AddSpecialHandler(handler SpecialHandler) { s.specialHandlers = append(s.specialHandlers, handler) } -func (s *Server) UseSpecialHandlers(ginC *gin.Context) error { +// UseSpecialHandlers execute all special handlers +func (s *Server) UseSpecialHandlers(ctx context.Context, req *http.Request, resp http.ResponseWriter) (context.Context, error) { c := &SpecialHandlerContext{ - server: s, - ginContext: ginC, + request: req, + response: resp, + ctx: ctx, } for _, l := range s.specialHandlers { err := l.Handle(c) if err != nil { - return err + return c.ctx, err } } - return nil + return c.ctx, nil } diff --git a/api/server/special_handler_context.go b/api/server/special_handler_context.go deleted file mode 100644 index 1ffd2b06b..000000000 --- a/api/server/special_handler_context.go +++ /dev/null @@ -1,34 +0,0 @@ -package server - -import ( - "net/http" - - "context" - "github.com/gin-gonic/gin" - "github.com/iron-io/functions/api/models" -) - -type SpecialHandlerContext struct { - server *Server - ginContext *gin.Context -} - -func (c *SpecialHandlerContext) Context() context.Context { - ctx, _ := c.ginContext.Get("ctx") - return ctx.(context.Context) -} - -func (c *SpecialHandlerContext) Request() *http.Request { - return c.ginContext.Request -} - -func (c *SpecialHandlerContext) Datastore() models.Datastore { - return c.server.Datastore -} - -func (c *SpecialHandlerContext) Set(key string, value interface{}) { - c.ginContext.Set(key, value) -} -func (c *SpecialHandlerContext) Get(key string) (value interface{}, exists bool) { - return c.ginContext.Get(key) -} diff --git a/api/server/special_handler_test.go b/api/server/special_handler_test.go new file mode 100644 index 000000000..d3e0c6b6f --- /dev/null +++ b/api/server/special_handler_test.go @@ -0,0 +1,49 @@ +package server + +import ( + "context" + "github.com/iron-io/functions/api/datastore" + "github.com/iron-io/functions/api/models" + "github.com/iron-io/functions/api/mqs" + "github.com/iron-io/functions/api/runner" + "github.com/iron-io/functions/api/runner/task" + "testing" +) + +type testSpecialHandler struct{} + +func (h *testSpecialHandler) Handle(c HandlerContext) error { + c.Set("appName", "test") + return nil +} + +func TestSpecialHandlerSet(t *testing.T) { + ctx := context.Background() + + tasks := make(chan task.Request) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + rnr, cancelrnr := testRunner(t) + defer cancelrnr() + + go runner.StartWorkers(ctx, rnr, tasks) + + s := New(ctx, &datastore.Mock{ + Apps: []*models.App{ + {Name: "test"}, + }, + Routes: []*models.Route{ + {Path: "/test", Image: "iron/hello", AppName: "test"}, + }, + }, &mqs.Mock{}, rnr, tasks, DefaultEnqueue) + router := s.Router + router.Use(prepareMiddleware(ctx)) + s.bindHandlers() + s.AddSpecialHandler(&testSpecialHandler{}) + + _, rec := routerRequest(t, router, "GET", "/test", nil) + if rec.Code != 200 { + t.Fatal("Test SpecialHandler: expected special handler to run functions successfully") + } +}