1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-08 04:21:57 +00:00

Update to 8.3.2 | Read HISTORY.md file

Former-commit-id: e6ab761989d596cb004c39e65e04e8968d9461ab
This commit is contained in:
kataras
2017-08-22 13:00:24 +03:00
parent 33e651866e
commit e12513a534
12 changed files with 150 additions and 13 deletions

View File

@@ -277,7 +277,7 @@ func buildMethodHandler(t TController, methodFuncIndex int) context.Handler {
}
// RegisterFunc used by the caller to register the result routes.
type RegisterFunc func(httpMethod string, handler context.Handler)
type RegisterFunc func(httpMethod string, handler ...context.Handler)
// RegisterMethodHandlers receives a `TController`, description of the
// user's controller, and calls the "registerFunc" for each of its
@@ -292,8 +292,19 @@ func RegisterMethodHandlers(t TController, registerFunc RegisterFunc) {
// http methods using the registerFunc, which is
// responsible to convert these into routes
// and add them to router via the APIBuilder.
var handlers context.Handlers
if t.binder != nil {
if m := t.binder.middleware; len(m) > 0 {
handlers = append(handlers, t.binder.middleware...)
}
}
for _, m := range t.Methods {
registerFunc(m.HTTPMethod, buildMethodHandler(t, m.Index))
methodHandler := buildMethodHandler(t, m.Index)
registeredHandlers := append(handlers, methodHandler)
registerFunc(m.HTTPMethod, registeredHandlers...)
}
}

View File

@@ -2,11 +2,17 @@ package activator
import (
"reflect"
"github.com/kataras/iris/context"
)
type binder struct {
values []interface{}
fields []field
// saves any middleware that may need to be passed to the router,
// statically, to gain performance.
middleware context.Handlers
}
// binder accepts a value of something
@@ -26,19 +32,42 @@ func newBinder(elemType reflect.Type, values []interface{}) *binder {
// if nothing valid found return nil, so the caller
// can omit the binder.
if len(b.fields) == 0 {
if len(b.fields) == 0 && len(b.middleware) == 0 {
return nil
}
return b
}
func (b *binder) storeValueIfMiddleware(value reflect.Value) bool {
if value.CanInterface() {
if m, ok := value.Interface().(context.Handler); ok {
b.middleware = append(b.middleware, m)
return true
}
if m, ok := value.Interface().(func(context.Context)); ok {
b.middleware = append(b.middleware, m)
return true
}
}
return false
}
func (b *binder) lookup(elem reflect.Type) (fields []field) {
for _, v := range b.values {
value := reflect.ValueOf(v)
// handlers will be recognised as middleware, not struct fields.
// End-Developer has the option to call any handler inside
// the controller's `BeginRequest` and `EndRequest`, the
// state is respected from the method handler already.
if b.storeValueIfMiddleware(value) {
// stored as middleware, continue to the next field, we don't have
// to bind anything here.
continue
}
for i, n := 0, elem.NumField(); i < n; i++ {
elemField := elem.Field(i)
if elemField.Type == value.Type() {
// we area inside the correct type
// println("[0] prepare bind filed for " + elemField.Name)
@@ -111,6 +140,13 @@ func lookupStruct(elem reflect.Type, value reflect.Value) *field {
}
func (b *binder) handle(c reflect.Value) {
// we could make check for middlewares here but
// these could easly be used outside of the controller
// so we don't have to initialize a controller to call them
// so they don't belong actually here, we will register them to the
// router itself, before the controller's handler to gain performance,
// look `activator.go#RegisterMethodHandlers` for more.
elem := c.Elem() // controller should always be a pointer at this state
for _, f := range b.fields {
f.sendTo(elem)