mirror of
https://github.com/kataras/iris.git
synced 2026-01-10 13:35:59 +00:00
Update to 8.3.2 | Read HISTORY.md file
Former-commit-id: e6ab761989d596cb004c39e65e04e8968d9461ab
This commit is contained in:
@@ -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...)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -58,6 +58,7 @@ import (
|
||||
// }
|
||||
//
|
||||
// Usage: app.Controller("/user/{id:int}", new(UserController), db, time.Now())
|
||||
// Note: Binded values of context.Handler type are being recognised as middlewares by the router.
|
||||
//
|
||||
// Look `core/router/APIBuilder#Controller` method too.
|
||||
type Controller struct {
|
||||
@@ -140,7 +141,7 @@ func (c *Controller) RelPath() string {
|
||||
reqPath := c.Ctx.Path()
|
||||
if len(reqPath) == 0 {
|
||||
// it never come here
|
||||
// but to protect ourselves jsut return an empty slash.
|
||||
// but to protect ourselves just return an empty slash.
|
||||
return slashStr
|
||||
}
|
||||
// [1:]to ellimuate the prefixes like "//"
|
||||
|
||||
@@ -171,6 +171,53 @@ func TestControllerBeginAndEndRequestFunc(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestControllerBeginAndEndRequestFuncBindMiddleware(t *testing.T) {
|
||||
app := iris.New()
|
||||
usernames := map[string]bool{
|
||||
"kataras": true,
|
||||
"makis": false,
|
||||
"efi": true,
|
||||
"rg": false,
|
||||
"bill": true,
|
||||
"whoisyourdaddy": false,
|
||||
}
|
||||
middlewareCheck := func(ctx context.Context) {
|
||||
for username, allow := range usernames {
|
||||
if ctx.Params().Get("username") == username && allow {
|
||||
ctx.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ctx.StatusCode(httptest.StatusForbidden)
|
||||
ctx.Writef("forbidden")
|
||||
}
|
||||
|
||||
app.Controller("/profile/{username}", new(testControllerBeginAndEndRequestFunc), middlewareCheck)
|
||||
|
||||
e := httptest.New(t, app)
|
||||
|
||||
doneResponse := "done"
|
||||
|
||||
for username, allow := range usernames {
|
||||
getEx := e.GET("/profile/" + username).Expect()
|
||||
if allow {
|
||||
getEx.Status(httptest.StatusOK).
|
||||
Body().Equal(username + doneResponse)
|
||||
} else {
|
||||
getEx.Status(httptest.StatusForbidden).Body().Equal("forbidden")
|
||||
}
|
||||
|
||||
postEx := e.POST("/profile/" + username).Expect()
|
||||
if allow {
|
||||
postEx.Status(httptest.StatusOK).
|
||||
Body().Equal(username + doneResponse)
|
||||
} else {
|
||||
postEx.Status(httptest.StatusForbidden).Body().Equal("forbidden")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type Model struct {
|
||||
Username string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user