mirror of
https://github.com/kataras/iris.git
synced 2026-01-23 11:56:00 +00:00
fix all _examples to the newest mvc, add comments to those examples and add a package-level .Configure in order to make it easier for new users. Add a deprecated panic if app.Controller is used with a small tutorial and future resource link so they can re-write their mvc app's definitions
Former-commit-id: bf07696041be9e3d178ce3c42ccec2df4bfdb2af
This commit is contained in:
@@ -24,7 +24,7 @@ type BaseController interface {
|
||||
type shared interface {
|
||||
Name() string
|
||||
Router() router.Party
|
||||
Handle(method, path, funcName string, middleware ...context.Handler) *router.Route
|
||||
Handle(httpMethod, path, funcName string, middleware ...context.Handler) *router.Route
|
||||
}
|
||||
|
||||
type BeforeActivation interface {
|
||||
@@ -34,7 +34,7 @@ type BeforeActivation interface {
|
||||
|
||||
type AfterActivation interface {
|
||||
shared
|
||||
DependenciesReadOnly() di.ValuesReadOnly
|
||||
DependenciesReadOnly() ValuesReadOnly
|
||||
Singleton() bool
|
||||
}
|
||||
|
||||
@@ -66,12 +66,12 @@ type ControllerActivator struct {
|
||||
// on incoming requests.
|
||||
dependencies di.Values
|
||||
|
||||
// on activate.
|
||||
// initialized on the first `Handle`.
|
||||
injector *di.StructInjector
|
||||
}
|
||||
|
||||
func getNameOf(typ reflect.Type) string {
|
||||
elemTyp := di.IndirectType(typ)
|
||||
func NameOf(v interface{}) string {
|
||||
elemTyp := di.IndirectType(di.ValueOf(v).Type())
|
||||
|
||||
typName := elemTyp.Name()
|
||||
pkgPath := elemTyp.PkgPath()
|
||||
@@ -80,22 +80,17 @@ func getNameOf(typ reflect.Type) string {
|
||||
return fullname
|
||||
}
|
||||
|
||||
func newControllerActivator(router router.Party, controller interface{}, dependencies di.Values) *ControllerActivator {
|
||||
var (
|
||||
val = reflect.ValueOf(controller)
|
||||
typ = val.Type()
|
||||
|
||||
// the full name of the controller: its type including the package path.
|
||||
fullName = getNameOf(typ)
|
||||
)
|
||||
func newControllerActivator(router router.Party, controller interface{}, dependencies []reflect.Value) *ControllerActivator {
|
||||
typ := reflect.TypeOf(controller)
|
||||
|
||||
c := &ControllerActivator{
|
||||
// give access to the Router to the end-devs if they need it for some reason,
|
||||
// i.e register done handlers.
|
||||
router: router,
|
||||
Value: val,
|
||||
Type: typ,
|
||||
fullName: fullName,
|
||||
router: router,
|
||||
Value: reflect.ValueOf(controller),
|
||||
Type: typ,
|
||||
// the full name of the controller: its type including the package path.
|
||||
fullName: NameOf(controller),
|
||||
// set some methods that end-dev cann't use accidentally
|
||||
// to register a route via the `Handle`,
|
||||
// all available exported and compatible methods
|
||||
@@ -106,7 +101,8 @@ func newControllerActivator(router router.Party, controller interface{}, depende
|
||||
// TODO: now that BaseController is totally optionally
|
||||
// we have to check if BeginRequest and EndRequest should be here.
|
||||
reservedMethods: whatReservedMethods(typ),
|
||||
dependencies: dependencies,
|
||||
// CloneWithFieldsOf: include the manual fill-ed controller struct's fields to the dependencies.
|
||||
dependencies: di.Values(dependencies).CloneWithFieldsOf(controller),
|
||||
}
|
||||
|
||||
return c
|
||||
@@ -125,7 +121,20 @@ func (c *ControllerActivator) Dependencies() *di.Values {
|
||||
return &c.dependencies
|
||||
}
|
||||
|
||||
func (c *ControllerActivator) DependenciesReadOnly() di.ValuesReadOnly {
|
||||
type ValuesReadOnly interface {
|
||||
// Has returns true if a binder responsible to
|
||||
// bind and return a type of "typ" is already registered to this controller.
|
||||
Has(value interface{}) bool
|
||||
// Len returns the length of the values.
|
||||
Len() int
|
||||
// Clone returns a copy of the current values.
|
||||
Clone() di.Values
|
||||
// CloneWithFieldsOf will return a copy of the current values
|
||||
// plus the "s" struct's fields that are filled(non-zero) by the caller.
|
||||
CloneWithFieldsOf(s interface{}) di.Values
|
||||
}
|
||||
|
||||
func (c *ControllerActivator) DependenciesReadOnly() ValuesReadOnly {
|
||||
return c.dependencies
|
||||
}
|
||||
|
||||
@@ -144,9 +153,9 @@ func (c *ControllerActivator) Router() router.Party {
|
||||
// any unexported fields and all fields are services-like, static.
|
||||
func (c *ControllerActivator) Singleton() bool {
|
||||
if c.injector == nil {
|
||||
panic("MVC: IsRequestScoped used on an invalid state the API gives access to it only `AfterActivation`, report this as bug")
|
||||
panic("MVC: Singleton used on an invalid state the API gives access to it only `AfterActivation`, report this as bug")
|
||||
}
|
||||
return c.injector.State == di.Singleton
|
||||
return c.injector.Scope == di.Singleton
|
||||
}
|
||||
|
||||
// checks if a method is already registered.
|
||||
@@ -160,18 +169,12 @@ func (c *ControllerActivator) isReservedMethod(name string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *ControllerActivator) parseMethod(m reflect.Method) {
|
||||
httpMethod, httpPath, err := parseMethod(m, c.isReservedMethod)
|
||||
if err != nil {
|
||||
if err != errSkip {
|
||||
err = fmt.Errorf("MVC: fail to parse the route path and HTTP method for '%s.%s': %v", c.fullName, m.Name, err)
|
||||
c.router.GetReporter().AddErr(err)
|
||||
func (c *ControllerActivator) activate() {
|
||||
c.parseMethods()
|
||||
}
|
||||
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.Handle(httpMethod, httpPath, m.Name)
|
||||
func (c *ControllerActivator) addErr(err error) bool {
|
||||
return c.router.GetReporter().AddErr(err)
|
||||
}
|
||||
|
||||
// register all available, exported methods to handlers if possible.
|
||||
@@ -183,8 +186,17 @@ func (c *ControllerActivator) parseMethods() {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ControllerActivator) activate() {
|
||||
c.parseMethods()
|
||||
func (c *ControllerActivator) parseMethod(m reflect.Method) {
|
||||
httpMethod, httpPath, err := parseMethod(m, c.isReservedMethod)
|
||||
if err != nil {
|
||||
if err != errSkip {
|
||||
c.addErr(fmt.Errorf("MVC: fail to parse the route path and HTTP method for '%s.%s': %v", c.fullName, m.Name, err))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
c.Handle(httpMethod, httpPath, m.Name)
|
||||
}
|
||||
|
||||
// Handle registers a route based on a http method, the route's path
|
||||
@@ -202,44 +214,18 @@ func (c *ControllerActivator) Handle(method, path, funcName string, middleware .
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remember:
|
||||
// we cannot simply do that and expect to work:
|
||||
// hasStructInjector = c.injector != nil && c.injector.Valid
|
||||
// hasFuncInjector = funcInjector != nil && funcInjector.Valid
|
||||
// because
|
||||
// the `Handle` can be called from `BeforeActivation` callbacks
|
||||
// and before activation, the c.injector is nil because
|
||||
// we may not have the dependencies binded yet. But if `c.injector.Valid`
|
||||
// inside the Handelr works because it's set on the `activate()` method.
|
||||
// To solve this we can make check on the FIRST `Handle`,
|
||||
// if c.injector is nil, then set it with the current bindings,
|
||||
// so the user should bind the dependencies needed before the `Handle`
|
||||
// this is a logical flow, so we will choose that one ->
|
||||
if c.injector == nil {
|
||||
// first, set these bindings to the passed controller, they will be useless
|
||||
// if the struct contains any dynamic value because this controller will
|
||||
// be never fired as it's but we make that in order to get the length of the static
|
||||
// matched dependencies of the struct.
|
||||
c.injector = di.MakeStructInjector(c.Value, hijacker, typeChecker, c.dependencies...)
|
||||
if c.injector.HasFields {
|
||||
golog.Debugf("MVC dependencies of '%s':\n%s", c.fullName, c.injector.String())
|
||||
}
|
||||
}
|
||||
|
||||
// get the method from the controller type.
|
||||
m, ok := c.Type.MethodByName(funcName)
|
||||
if !ok {
|
||||
err := fmt.Errorf("MVC: function '%s' doesn't exist inside the '%s' controller",
|
||||
funcName, c.fullName)
|
||||
c.router.GetReporter().AddErr(err)
|
||||
c.addErr(fmt.Errorf("MVC: function '%s' doesn't exist inside the '%s' controller",
|
||||
funcName, c.fullName))
|
||||
return nil
|
||||
}
|
||||
|
||||
// parse a route template which contains the parameters organised.
|
||||
tmpl, err := macro.Parse(path, c.router.Macros())
|
||||
if err != nil {
|
||||
err = fmt.Errorf("MVC: fail to parse the path for '%s.%s': %v", c.fullName, funcName, err)
|
||||
c.router.GetReporter().AddErr(err)
|
||||
c.addErr(fmt.Errorf("MVC: fail to parse the path for '%s.%s': %v", c.fullName, funcName, err))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -257,23 +243,40 @@ func (c *ControllerActivator) Handle(method, path, funcName string, middleware .
|
||||
|
||||
// register the handler now.
|
||||
route := c.router.Handle(method, path, append(middleware, handler)...)
|
||||
if route != nil {
|
||||
// change the main handler's name in order to respect the controller's and give
|
||||
// a proper debug message.
|
||||
route.MainHandlerName = fmt.Sprintf("%s.%s", c.fullName, funcName)
|
||||
|
||||
// add this as a reserved method name in order to
|
||||
// be sure that the same func will not be registered again,
|
||||
// even if a custom .Handle later on.
|
||||
c.reservedMethods = append(c.reservedMethods, funcName)
|
||||
if route == nil {
|
||||
c.addErr(fmt.Errorf("MVC: unable to register a route for the path for '%s.%s'", c.fullName, funcName))
|
||||
return nil
|
||||
}
|
||||
|
||||
// change the main handler's name in order to respect the controller's and give
|
||||
// a proper debug message.
|
||||
route.MainHandlerName = fmt.Sprintf("%s.%s", c.fullName, funcName)
|
||||
|
||||
// add this as a reserved method name in order to
|
||||
// be sure that the same func will not be registered again,
|
||||
// even if a custom .Handle later on.
|
||||
c.reservedMethods = append(c.reservedMethods, funcName)
|
||||
|
||||
return route
|
||||
}
|
||||
|
||||
var emptyIn = []reflect.Value{}
|
||||
|
||||
func (c *ControllerActivator) handlerOf(m reflect.Method, funcDependencies []reflect.Value) context.Handler {
|
||||
// Remember:
|
||||
// The `Handle->handlerOf` can be called from `BeforeActivation` event
|
||||
// then, the c.injector is nil because
|
||||
// we may not have the dependencies binded yet.
|
||||
// To solve this we're doing a check on the FIRST `Handle`,
|
||||
// if c.injector is nil, then set it with the current bindings,
|
||||
// these bindings can change after, so first add dependencies and after register routes.
|
||||
if c.injector == nil {
|
||||
c.injector = di.MakeStructInjector(c.Value, hijacker, typeChecker, c.dependencies...)
|
||||
if c.injector.HasFields {
|
||||
golog.Debugf("MVC dependencies of '%s':\n%s", c.fullName, c.injector.String())
|
||||
}
|
||||
}
|
||||
|
||||
// fmt.Printf("for %s | values: %s\n", funcName, funcDependencies)
|
||||
funcInjector := di.MakeFuncInjector(m.Func, hijacker, typeChecker, funcDependencies...)
|
||||
// fmt.Printf("actual injector's inputs length: %d\n", funcInjector.Length)
|
||||
@@ -291,14 +294,14 @@ func (c *ControllerActivator) handlerOf(m reflect.Method, funcDependencies []ref
|
||||
|
||||
if !implementsBase && !hasBindableFields && !hasBindableFuncInputs {
|
||||
return func(ctx context.Context) {
|
||||
DispatchFuncResult(ctx, call(c.injector.NewAsSlice()))
|
||||
DispatchFuncResult(ctx, call(c.injector.AcquireSlice()))
|
||||
}
|
||||
}
|
||||
|
||||
n := m.Type.NumIn()
|
||||
return func(ctx context.Context) {
|
||||
var (
|
||||
ctrl = c.injector.New()
|
||||
ctrl = c.injector.Acquire()
|
||||
ctxValue reflect.Value
|
||||
)
|
||||
|
||||
|
||||
@@ -486,8 +486,6 @@ func TestControllerNotCreateNewDueManuallySettingAllFields(t *testing.T) {
|
||||
TitlePointer: &testBindType{
|
||||
title: "my title",
|
||||
},
|
||||
}, func(b BeforeActivation) {
|
||||
|
||||
})
|
||||
|
||||
e := httptest.New(t, app)
|
||||
|
||||
11
mvc/di/TODO.txt
Normal file
11
mvc/di/TODO.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
I can do one of the followings to this "di" folder when I finish the cleanup and document it a bit,
|
||||
although I'm sick I will try to finish it tomorrow.
|
||||
|
||||
End-users don't need this.
|
||||
1) So, rename this to "internal".
|
||||
|
||||
I don't know if something similar exist in Go,
|
||||
it's a dependency injection framework at the end, and a very fast one.
|
||||
|
||||
2) So I'm thinking to push it to a different repo,
|
||||
like https://github.com/kataras/di or even to my small common https://github.com/kataras/pkg collection.
|
||||
11
mvc/di/di.go
11
mvc/di/di.go
@@ -57,15 +57,14 @@ func (d *D) Clone() *D {
|
||||
// with the injector's `Inject` and `InjectElem` methods.
|
||||
func (d *D) Struct(s interface{}) *StructInjector {
|
||||
if s == nil {
|
||||
return nil
|
||||
return &StructInjector{HasFields: false}
|
||||
}
|
||||
v := ValueOf(s)
|
||||
|
||||
return MakeStructInjector(
|
||||
v,
|
||||
ValueOf(s),
|
||||
d.hijacker,
|
||||
d.goodFunc,
|
||||
d.Values...,
|
||||
d.Values.CloneWithFieldsOf(s)...,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -75,6 +74,10 @@ func (d *D) Struct(s interface{}) *StructInjector {
|
||||
// to the function's input argument when called
|
||||
// with the injector's `Fill` method.
|
||||
func (d *D) Func(fn interface{}) *FuncInjector {
|
||||
if fn == nil {
|
||||
return &FuncInjector{Valid: false}
|
||||
}
|
||||
|
||||
return MakeFuncInjector(
|
||||
ValueOf(fn),
|
||||
d.hijacker,
|
||||
|
||||
@@ -5,10 +5,10 @@ import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type State uint8
|
||||
type Scope uint8
|
||||
|
||||
const (
|
||||
Stateless State = iota
|
||||
Stateless Scope = iota
|
||||
Singleton
|
||||
)
|
||||
|
||||
@@ -29,7 +29,7 @@ type (
|
||||
// see `setState`.
|
||||
HasFields bool
|
||||
CanInject bool // if any bindable fields when the state is NOT singleton.
|
||||
State State
|
||||
Scope Scope
|
||||
}
|
||||
)
|
||||
|
||||
@@ -121,13 +121,13 @@ func (s *StructInjector) setState() {
|
||||
// if the number of static values binded is equal to the
|
||||
// total struct's fields(including unexported fields this time) then set as singleton.
|
||||
if staticBindingsFieldsLength == allStructFieldsLength {
|
||||
s.State = Singleton
|
||||
s.Scope = Singleton
|
||||
// the default is `Stateless`, which means that a new instance should be created
|
||||
// on each inject action by the caller.
|
||||
return
|
||||
}
|
||||
|
||||
s.CanInject = s.State == Stateless && s.HasFields
|
||||
s.CanInject = s.Scope == Stateless && s.HasFields
|
||||
}
|
||||
|
||||
// fill the static bindings values once.
|
||||
@@ -177,15 +177,15 @@ func (s *StructInjector) InjectElem(destElem reflect.Value, ctx ...reflect.Value
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StructInjector) New() reflect.Value {
|
||||
if s.State == Singleton {
|
||||
func (s *StructInjector) Acquire() reflect.Value {
|
||||
if s.Scope == Singleton {
|
||||
return s.initRef
|
||||
}
|
||||
return reflect.New(s.elemType)
|
||||
}
|
||||
|
||||
func (s *StructInjector) NewAsSlice() []reflect.Value {
|
||||
if s.State == Singleton {
|
||||
func (s *StructInjector) AcquireSlice() []reflect.Value {
|
||||
if s.Scope == Singleton {
|
||||
return s.initRefAsSlice
|
||||
}
|
||||
return []reflect.Value{reflect.New(s.elemType)}
|
||||
|
||||
@@ -2,16 +2,6 @@ package di
|
||||
|
||||
import "reflect"
|
||||
|
||||
type ValuesReadOnly interface {
|
||||
// Has returns true if a binder responsible to
|
||||
// bind and return a type of "typ" is already registered to this controller.
|
||||
Has(value interface{}) bool
|
||||
// Len returns the length of the values.
|
||||
Len() int
|
||||
// Clone returns a copy of the current values.
|
||||
Clone() Values
|
||||
}
|
||||
|
||||
type Values []reflect.Value
|
||||
|
||||
func NewValues() Values {
|
||||
@@ -30,7 +20,7 @@ func (bv Values) Clone() Values {
|
||||
}
|
||||
|
||||
// CloneWithFieldsOf will return a copy of the current values
|
||||
// plus the "v" struct's fields that are filled(non-zero) by the caller.
|
||||
// plus the "s" struct's fields that are filled(non-zero) by the caller.
|
||||
func (bv Values) CloneWithFieldsOf(s interface{}) Values {
|
||||
values := bv.Clone()
|
||||
|
||||
|
||||
@@ -84,30 +84,24 @@ func (e *Engine) Handler(handler interface{}) context.Handler {
|
||||
// where Get is an HTTP Method func.
|
||||
//
|
||||
// Examples at: https://github.com/kataras/iris/tree/master/_examples/mvc.
|
||||
func (e *Engine) Controller(router router.Party, controller interface{}, beforeActivate ...func(BeforeActivation)) {
|
||||
// add the manual filled fields to the dependencies.
|
||||
dependencies := e.Dependencies.CloneWithFieldsOf(controller)
|
||||
ca := newControllerActivator(router, controller, dependencies)
|
||||
func (e *Engine) Controller(router router.Party, controller interface{}) {
|
||||
// initialize the controller's activator, nothing too magical so far.
|
||||
c := newControllerActivator(router, controller, e.Dependencies)
|
||||
|
||||
// give a priority to the "beforeActivate"
|
||||
// callbacks, if any.
|
||||
for _, cb := range beforeActivate {
|
||||
cb(ca)
|
||||
}
|
||||
|
||||
// check if controller has an "BeforeActivation" function
|
||||
// which accepts the controller activator and call it.
|
||||
if activateListener, ok := controller.(interface {
|
||||
// check the controller's "BeforeActivation" or/and "AfterActivation" method(s) between the `activate`
|
||||
// call, which is simply parses the controller's methods, end-dev can register custom controller's methods
|
||||
// by using the BeforeActivation's (a ControllerActivation) `.Handle` method.
|
||||
if before, ok := controller.(interface {
|
||||
BeforeActivation(BeforeActivation)
|
||||
}); ok {
|
||||
activateListener.BeforeActivation(ca)
|
||||
before.BeforeActivation(c)
|
||||
}
|
||||
|
||||
ca.activate()
|
||||
c.activate()
|
||||
|
||||
if afterActivateListener, ok := controller.(interface {
|
||||
if after, okAfter := controller.(interface {
|
||||
AfterActivation(AfterActivation)
|
||||
}); ok {
|
||||
afterActivateListener.AfterActivation(ca)
|
||||
}); okAfter {
|
||||
after.AfterActivation(c)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,9 +264,10 @@ func (t *testControllerViewResultRespectCtxViewData) Get() Result {
|
||||
|
||||
func TestControllerViewResultRespectCtxViewData(t *testing.T) {
|
||||
app := iris.New()
|
||||
NewEngine().Controller(app, new(testControllerViewResultRespectCtxViewData), func(b BeforeActivation) {
|
||||
b.Dependencies().Add(t)
|
||||
})
|
||||
m := NewEngine()
|
||||
m.Dependencies.Add(t)
|
||||
m.Controller(app.Party("/"), new(testControllerViewResultRespectCtxViewData))
|
||||
|
||||
e := httptest.New(t, app)
|
||||
|
||||
e.GET("/").Expect().Status(iris.StatusInternalServerError)
|
||||
|
||||
@@ -65,26 +65,23 @@ func MakeHandler(handler interface{}, bindValues ...reflect.Value) (context.Hand
|
||||
return h, nil
|
||||
}
|
||||
|
||||
s := di.MakeFuncInjector(fn, hijacker, typeChecker, bindValues...)
|
||||
if !s.Valid {
|
||||
funcInjector := di.MakeFuncInjector(fn, hijacker, typeChecker, bindValues...)
|
||||
if !funcInjector.Valid {
|
||||
pc := fn.Pointer()
|
||||
fpc := runtime.FuncForPC(pc)
|
||||
callerFileName, callerLineNumber := fpc.FileLine(pc)
|
||||
callerName := fpc.Name()
|
||||
|
||||
err := fmt.Errorf("input arguments length(%d) and valid binders length(%d) are not equal for typeof '%s' which is defined at %s:%d by %s",
|
||||
n, s.Length, fn.Type().String(), callerFileName, callerLineNumber, callerName)
|
||||
n, funcInjector.Length, fn.Type().String(), callerFileName, callerLineNumber, callerName)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
h := func(ctx context.Context) {
|
||||
in := make([]reflect.Value, n, n)
|
||||
|
||||
s.Inject(&in, reflect.ValueOf(ctx))
|
||||
if ctx.IsStopped() {
|
||||
return
|
||||
}
|
||||
DispatchFuncResult(ctx, fn.Call(in))
|
||||
// in := make([]reflect.Value, n, n)
|
||||
// funcInjector.Inject(&in, reflect.ValueOf(ctx))
|
||||
// DispatchFuncResult(ctx, fn.Call(in))
|
||||
DispatchFuncResult(ctx, funcInjector.Call(reflect.ValueOf(ctx)))
|
||||
}
|
||||
|
||||
return h, nil
|
||||
|
||||
2
mvc/ideas/1/TODO.txt
Normal file
2
mvc/ideas/1/TODO.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
Remove the "ideas" folder or move this example somewhere in the _examples/mvc or even make a https://medium.com/@kataras
|
||||
small tutorial about Iris' new MVC implementation.
|
||||
39
mvc/mvc.go
39
mvc/mvc.go
@@ -34,6 +34,26 @@ func New(party router.Party) *Application {
|
||||
return newApp(NewEngine(), party)
|
||||
}
|
||||
|
||||
// Configure creates a new controller and configures it,
|
||||
// this function simply calls the `New(party)` and its `.Configure(configurators...)`.
|
||||
//
|
||||
// A call of `mvc.New(app.Party("/path").Configure(buildMyMVC)` is equal to
|
||||
// `mvc.Configure(app.Party("/path"), buildMyMVC)`.
|
||||
//
|
||||
// Read more at `New() Application` and `Application#Configure` methods.
|
||||
func Configure(party router.Party, configurators ...func(*Application)) *Application {
|
||||
// Author's Notes->
|
||||
// About the Configure's comment: +5 space to be shown in equal width to the previous or after line.
|
||||
//
|
||||
// About the Configure's design choosen:
|
||||
// Yes, we could just have a `New(party, configurators...)`
|
||||
// but I think the `New()` and `Configure(configurators...)` API seems more native to programmers,
|
||||
// at least to me and the people I ask for their opinion between them.
|
||||
// Because the `New()` can actually return something that can be fully configured without its `Configure`,
|
||||
// its `Configure` is there just to design the apps better and help end-devs to split their code wisely.
|
||||
return New(party).Configure(configurators...)
|
||||
}
|
||||
|
||||
// Configure can be used to pass one or more functions that accept this
|
||||
// Application, use this to add dependencies and controller(s).
|
||||
//
|
||||
@@ -52,9 +72,9 @@ func (app *Application) Configure(configurators ...func(*Application)) *Applicat
|
||||
// will be binded to the controller's field, if matching or to the
|
||||
// controller's methods, if matching.
|
||||
//
|
||||
// The dependencies can be changed per-controller as well via a `beforeActivate`
|
||||
// on the `Register` method or when the controller has the `BeforeActivation(b BeforeActivation)`
|
||||
// method defined.
|
||||
// These dependencies "values" can be changed per-controller as well,
|
||||
// via controller's `BeforeActivation` and `AfterActivation` methods,
|
||||
// look the `Register` method for more.
|
||||
//
|
||||
// It returns this Application.
|
||||
//
|
||||
@@ -68,15 +88,16 @@ func (app *Application) AddDependencies(values ...interface{}) *Application {
|
||||
// It accept any custom struct which its functions will be transformed
|
||||
// to routes.
|
||||
//
|
||||
// The second, optional and variadic argument is the "beforeActive",
|
||||
// use that when you want to modify the controller before the activation
|
||||
// and registration to the main Iris Application.
|
||||
// If "controller" has `BeforeActivation(b mvc.BeforeActivation)`
|
||||
// or/and `AfterActivation(a mvc.AfterActivation)` then these will be called between the controller's `.activate`,
|
||||
// use those when you want to modify the controller before or/and after
|
||||
// the controller will be registered to the main Iris Application.
|
||||
//
|
||||
// It returns this Application.
|
||||
// It returns this mvc Application.
|
||||
//
|
||||
// Example: `.Register(new(TodoController))`.
|
||||
func (app *Application) Register(controller interface{}, beforeActivate ...func(BeforeActivation)) *Application {
|
||||
app.Engine.Controller(app.Router, controller, beforeActivate...)
|
||||
func (app *Application) Register(controller interface{}) *Application {
|
||||
app.Engine.Controller(app.Router, controller)
|
||||
return app
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,9 @@ func TestPathParamBinder(t *testing.T) {
|
||||
t.Fatalf("expected the param 'username' to be '%s' but got '%s'", expectedUsername, got)
|
||||
}
|
||||
|
||||
// test the non executed if param not found.
|
||||
/* this is useless, we don't need to check if ctx.Stopped
|
||||
inside bindings, this is middleware's job.
|
||||
// test the non executed if param not found -> this is already done in Iris if real context, so ignore it.
|
||||
executed = false
|
||||
got = ""
|
||||
|
||||
@@ -63,4 +65,5 @@ func TestPathParamBinder(t *testing.T) {
|
||||
if executed {
|
||||
t.Fatalf("expected the handler to not be executed")
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -10,6 +10,9 @@ var defaultSessionManager = sessions.New(sessions.Config{})
|
||||
// SessionController is a simple `Controller` implementation
|
||||
// which requires a binded session manager in order to give
|
||||
// direct access to the current client's session via its `Session` field.
|
||||
//
|
||||
// SessionController is deprecated please use the `mvc.Session(manager)` instead, it's more useful,
|
||||
// also *sessions.Session type can now `Destroy` itself without the need of the manager, embrace it.
|
||||
type SessionController struct {
|
||||
Manager *sessions.Sessions
|
||||
Session *sessions.Session
|
||||
|
||||
Reference in New Issue
Block a user