remove gin, fix specialhandler and added test (#448)

This commit is contained in:
Pedro Nasser
2016-12-15 16:26:59 -02:00
committed by Travis Reeder
parent 1aea433d05
commit 63c2b18ac5
4 changed files with 83 additions and 49 deletions

View File

@@ -27,16 +27,15 @@ func (s *Server) handleSpecial(c *gin.Context) {
ctx = context.WithValue(ctx, "appName", "") ctx = context.WithValue(ctx, "appName", "")
ctx = context.WithValue(ctx, "routePath", c.Request.URL.Path) 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 { if err != nil {
log.WithError(err).Errorln("Error using special handler!") log.WithError(err).Errorln("Error using special handler!")
c.JSON(http.StatusInternalServerError, simpleError(errors.New("Failed to run function"))) c.JSON(http.StatusInternalServerError, simpleError(errors.New("Failed to run function")))
return return
} }
ctx = c.MustGet("ctx").(context.Context) c.Set("ctx", ctx)
if ctx.Value("appName").(string) == "" { if ctx.Value("appName").(string) == "" {
log.WithError(err).Errorln("Specialhandler returned empty app name") log.WithError(err).Errorln("Specialhandler returned empty app name")
c.JSON(http.StatusBadRequest, simpleError(models.ErrRunnerRouteNotFound)) c.JSON(http.StatusBadRequest, simpleError(models.ErrRunnerRouteNotFound))

View File

@@ -4,8 +4,6 @@ import (
"net/http" "net/http"
"context" "context"
"github.com/gin-gonic/gin"
"github.com/iron-io/functions/api/models"
) )
type SpecialHandler interface { 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. // 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 { type HandlerContext interface {
// Context return the context object // Context return the context object
Context() context.Context Context() context.Context
@@ -21,28 +18,51 @@ type HandlerContext interface {
// Request returns the underlying http.Request object // Request returns the underlying http.Request object
Request() *http.Request 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 // Response returns the http.ResponseWriter
Datastore() models.Datastore 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{}) 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) { func (s *Server) AddSpecialHandler(handler SpecialHandler) {
s.specialHandlers = append(s.specialHandlers, handler) 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{ c := &SpecialHandlerContext{
server: s, request: req,
ginContext: ginC, response: resp,
ctx: ctx,
} }
for _, l := range s.specialHandlers { for _, l := range s.specialHandlers {
err := l.Handle(c) err := l.Handle(c)
if err != nil { if err != nil {
return err return c.ctx, err
} }
} }
return nil return c.ctx, nil
} }

View File

@@ -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)
}

View File

@@ -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")
}
}