2017-12-16 05:38:28 +01:00
|
|
|
package mvc
|
2017-11-27 20:39:57 +01:00
|
|
|
|
|
|
|
import (
|
2017-12-04 04:06:03 +01:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2017-12-15 19:28:06 +01:00
|
|
|
"strings"
|
2017-11-27 20:39:57 +01:00
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12/context"
|
|
|
|
"github.com/kataras/iris/v12/core/router"
|
|
|
|
"github.com/kataras/iris/v12/hero"
|
|
|
|
"github.com/kataras/iris/v12/hero/di"
|
|
|
|
"github.com/kataras/iris/v12/macro"
|
2017-12-16 22:12:49 +01:00
|
|
|
|
|
|
|
"github.com/kataras/golog"
|
2017-11-27 20:39:57 +01:00
|
|
|
)
|
|
|
|
|
2017-12-16 05:38:28 +01:00
|
|
|
// BaseController is the optional controller interface, if it's
|
|
|
|
// completed by the end controller then the BeginRequest and EndRequest
|
|
|
|
// are called between the controller's method responsible for the incoming request.
|
2017-12-04 04:06:03 +01:00
|
|
|
type BaseController interface {
|
|
|
|
BeginRequest(context.Context)
|
|
|
|
EndRequest(context.Context)
|
|
|
|
}
|
|
|
|
|
2017-12-17 23:16:10 +01:00
|
|
|
type shared interface {
|
|
|
|
Name() string
|
|
|
|
Router() router.Party
|
2017-12-30 21:04:26 +01:00
|
|
|
GetRoute(methodName string) *router.Route
|
2019-07-11 14:07:39 +02:00
|
|
|
GetRoutes(methodName string) []*router.Route
|
2017-12-20 07:33:53 +01:00
|
|
|
Handle(httpMethod, path, funcName string, middleware ...context.Handler) *router.Route
|
2019-07-11 14:07:39 +02:00
|
|
|
HandleMany(httpMethod, path, funcName string, middleware ...context.Handler) []*router.Route
|
2017-12-17 23:16:10 +01:00
|
|
|
}
|
|
|
|
|
2019-04-16 17:01:48 +02:00
|
|
|
// BeforeActivation is being used as the only one input argument of a
|
2017-12-22 10:07:13 +01:00
|
|
|
// `func(c *Controller) BeforeActivation(b mvc.BeforeActivation) {}`.
|
|
|
|
//
|
|
|
|
// It's being called before the controller's dependencies binding to the fields or the input arguments
|
|
|
|
// but before server ran.
|
|
|
|
//
|
|
|
|
// It's being used to customize a controller if needed inside the controller itself,
|
|
|
|
// it's called once per application.
|
2017-12-17 23:16:10 +01:00
|
|
|
type BeforeActivation interface {
|
|
|
|
shared
|
|
|
|
Dependencies() *di.Values
|
|
|
|
}
|
|
|
|
|
2019-04-16 17:01:48 +02:00
|
|
|
// AfterActivation is being used as the only one input argument of a
|
2017-12-22 10:07:13 +01:00
|
|
|
// `func(c *Controller) AfterActivation(a mvc.AfterActivation) {}`.
|
|
|
|
//
|
|
|
|
// It's being called after the `BeforeActivation`,
|
2019-04-16 17:01:48 +02:00
|
|
|
// and after controller's dependencies bind-ed to the fields or the input arguments but before server ran.
|
2017-12-22 10:07:13 +01:00
|
|
|
//
|
|
|
|
// It's being used to customize a controller if needed inside the controller itself,
|
|
|
|
// it's called once per application.
|
2017-12-17 23:16:10 +01:00
|
|
|
type AfterActivation interface {
|
|
|
|
shared
|
2017-12-20 07:33:53 +01:00
|
|
|
DependenciesReadOnly() ValuesReadOnly
|
2017-12-19 22:40:42 +01:00
|
|
|
Singleton() bool
|
2017-12-17 23:16:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ BeforeActivation = (*ControllerActivator)(nil)
|
|
|
|
_ AfterActivation = (*ControllerActivator)(nil)
|
|
|
|
)
|
|
|
|
|
2017-12-12 13:33:39 +01:00
|
|
|
// ControllerActivator returns a new controller type info description.
|
2017-12-18 05:47:05 +01:00
|
|
|
// Its functionality can be overridden by the end-dev.
|
2017-12-04 04:06:03 +01:00
|
|
|
type ControllerActivator struct {
|
2017-12-17 23:16:10 +01:00
|
|
|
// the router is used on the `Activate` and can be used by end-dev on the `BeforeActivation`
|
|
|
|
// to register any custom controller's methods as handlers.
|
|
|
|
router router.Party
|
2017-12-04 04:06:03 +01:00
|
|
|
|
2019-09-07 04:57:35 +02:00
|
|
|
macros macro.Macros
|
|
|
|
tmplParamStartIndex int
|
|
|
|
|
2017-12-11 05:51:52 +01:00
|
|
|
// initRef BaseController // the BaseController as it's passed from the end-dev.
|
|
|
|
Value reflect.Value // the BaseController's Value.
|
|
|
|
Type reflect.Type // raw type of the BaseController (initRef).
|
2017-12-04 04:06:03 +01:00
|
|
|
// FullName it's the last package path segment + "." + the Name.
|
|
|
|
// i.e: if login-example/user/controller.go, the FullName is "user.Controller".
|
2017-12-17 23:16:10 +01:00
|
|
|
fullName string
|
2017-12-04 04:06:03 +01:00
|
|
|
|
2017-12-30 21:04:26 +01:00
|
|
|
// the already-registered routes, key = the controller's function name.
|
|
|
|
// End-devs can change some properties of the *Route on the `BeforeActivator` by using the
|
2019-07-11 14:07:39 +02:00
|
|
|
// `GetRoute/GetRoutes(functionName)`.
|
|
|
|
routes map[string][]*router.Route
|
2017-12-10 06:00:51 +01:00
|
|
|
|
2017-12-14 22:04:42 +01:00
|
|
|
// the bindings that comes from the Engine and the controller's filled fields if any.
|
2019-04-16 17:01:48 +02:00
|
|
|
// Can be bind-ed to the the new controller's fields and method that is fired
|
2017-12-14 22:04:42 +01:00
|
|
|
// on incoming requests.
|
2017-12-17 23:16:10 +01:00
|
|
|
dependencies di.Values
|
2019-08-26 15:43:02 +02:00
|
|
|
sorter di.Sorter
|
2017-12-04 04:06:03 +01:00
|
|
|
|
2019-04-16 17:01:48 +02:00
|
|
|
errorHandler hero.ErrorHandler
|
|
|
|
|
2019-07-09 11:16:19 +02:00
|
|
|
// initialized on the first `Handle` or immediately when "servesWebsocket" is true.
|
2017-12-14 22:04:42 +01:00
|
|
|
injector *di.StructInjector
|
2019-07-09 11:16:19 +02:00
|
|
|
|
|
|
|
// true if this controller listens and serves to websocket events.
|
|
|
|
servesWebsocket bool
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-22 10:07:13 +01:00
|
|
|
// NameOf returns the package name + the struct type's name,
|
|
|
|
// it's used to take the full name of an Controller, the `ControllerActivator#Name`.
|
2017-12-20 07:33:53 +01:00
|
|
|
func NameOf(v interface{}) string {
|
|
|
|
elemTyp := di.IndirectType(di.ValueOf(v).Type())
|
2017-12-15 19:28:06 +01:00
|
|
|
|
|
|
|
typName := elemTyp.Name()
|
|
|
|
pkgPath := elemTyp.PkgPath()
|
|
|
|
fullname := pkgPath[strings.LastIndexByte(pkgPath, '/')+1:] + "." + typName
|
|
|
|
|
|
|
|
return fullname
|
|
|
|
}
|
|
|
|
|
2019-08-26 15:43:02 +02:00
|
|
|
func newControllerActivator(router router.Party, controller interface{}, dependencies []reflect.Value, sorter di.Sorter, errorHandler hero.ErrorHandler) *ControllerActivator {
|
2017-12-20 07:33:53 +01:00
|
|
|
typ := reflect.TypeOf(controller)
|
2017-12-12 13:33:39 +01:00
|
|
|
|
2017-12-04 04:06:03 +01:00
|
|
|
c := &ControllerActivator{
|
2017-12-12 13:33:39 +01:00
|
|
|
// give access to the Router to the end-devs if they need it for some reason,
|
|
|
|
// i.e register done handlers.
|
2017-12-20 07:33:53 +01:00
|
|
|
router: router,
|
2019-09-07 04:57:35 +02:00
|
|
|
macros: *router.Macros(),
|
2017-12-20 07:33:53 +01:00
|
|
|
Value: reflect.ValueOf(controller),
|
|
|
|
Type: typ,
|
|
|
|
// the full name of the controller: its type including the package path.
|
|
|
|
fullName: NameOf(controller),
|
2017-12-12 13:33:39 +01:00
|
|
|
// set some methods that end-dev cann't use accidentally
|
|
|
|
// to register a route via the `Handle`,
|
|
|
|
// all available exported and compatible methods
|
|
|
|
// are being appended to the slice at the `parseMethods`,
|
|
|
|
// if a new method is registered via `Handle` its function name
|
|
|
|
// is also appended to that slice.
|
2017-12-30 21:04:26 +01:00
|
|
|
routes: whatReservedMethods(typ),
|
2017-12-20 07:33:53 +01:00
|
|
|
// CloneWithFieldsOf: include the manual fill-ed controller struct's fields to the dependencies.
|
|
|
|
dependencies: di.Values(dependencies).CloneWithFieldsOf(controller),
|
2019-08-26 15:43:02 +02:00
|
|
|
sorter: sorter,
|
2019-04-16 17:01:48 +02:00
|
|
|
errorHandler: errorHandler,
|
2017-12-17 05:34:16 +01:00
|
|
|
}
|
|
|
|
|
2019-09-07 04:57:35 +02:00
|
|
|
fpath, _ := macro.Parse(c.router.GetRelPath(), c.macros)
|
|
|
|
c.tmplParamStartIndex = len(fpath.Params)
|
2017-12-04 04:06:03 +01:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-07-11 14:07:39 +02:00
|
|
|
func whatReservedMethods(typ reflect.Type) map[string][]*router.Route {
|
2017-12-17 23:16:10 +01:00
|
|
|
methods := []string{"BeforeActivation", "AfterActivation"}
|
2017-12-30 21:04:26 +01:00
|
|
|
// BeforeActivatior/AfterActivation are not routes but they are
|
|
|
|
// reserved names*
|
2017-12-14 22:04:42 +01:00
|
|
|
if isBaseController(typ) {
|
|
|
|
methods = append(methods, "BeginRequest", "EndRequest")
|
|
|
|
}
|
|
|
|
|
2019-07-11 14:07:39 +02:00
|
|
|
routes := make(map[string][]*router.Route, len(methods))
|
2017-12-30 21:04:26 +01:00
|
|
|
for _, m := range methods {
|
2019-07-11 14:07:39 +02:00
|
|
|
routes[m] = []*router.Route{}
|
2017-12-30 21:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return routes
|
2017-12-14 22:04:42 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 11:16:19 +02:00
|
|
|
func (c *ControllerActivator) markAsWebsocket() {
|
|
|
|
c.servesWebsocket = true
|
|
|
|
c.attachInjector()
|
|
|
|
}
|
|
|
|
|
2017-12-22 10:07:13 +01:00
|
|
|
// Dependencies returns the write and read access of the dependencies that are
|
|
|
|
// came from the parent MVC Application, with this you can customize
|
|
|
|
// the dependencies per controller, used at the `BeforeActivation`.
|
2017-12-17 23:16:10 +01:00
|
|
|
func (c *ControllerActivator) Dependencies() *di.Values {
|
|
|
|
return &c.dependencies
|
|
|
|
}
|
|
|
|
|
2017-12-22 10:07:13 +01:00
|
|
|
// ValuesReadOnly returns the read-only access type of the controller's dependencies.
|
|
|
|
// Used at `AfterActivation`.
|
2017-12-20 07:33:53 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-12-22 10:07:13 +01:00
|
|
|
// DependenciesReadOnly returns the read-only access type of the controller's dependencies.
|
|
|
|
// Used at `AfterActivation`.
|
2017-12-20 07:33:53 +01:00
|
|
|
func (c *ControllerActivator) DependenciesReadOnly() ValuesReadOnly {
|
2017-12-17 23:16:10 +01:00
|
|
|
return c.dependencies
|
|
|
|
}
|
|
|
|
|
2017-12-22 10:07:13 +01:00
|
|
|
// Name returns the full name of the controller, its package name + the type name.
|
|
|
|
// Can used at both `BeforeActivation` and `AfterActivation`.
|
2017-12-17 23:16:10 +01:00
|
|
|
func (c *ControllerActivator) Name() string {
|
|
|
|
return c.fullName
|
|
|
|
}
|
|
|
|
|
2017-12-22 10:07:13 +01:00
|
|
|
// Router is the standard Iris router's public API.
|
|
|
|
// With this you can register middleware, view layouts, subdomains, serve static files
|
|
|
|
// and even add custom standard iris handlers as normally.
|
|
|
|
//
|
|
|
|
// This Router is the router instance that came from the parent MVC Application,
|
|
|
|
// it's the `app.Party(...)` argument.
|
|
|
|
//
|
|
|
|
// Can used at both `BeforeActivation` and `AfterActivation`.
|
2017-12-17 23:16:10 +01:00
|
|
|
func (c *ControllerActivator) Router() router.Party {
|
|
|
|
return c.router
|
|
|
|
}
|
|
|
|
|
2019-07-11 14:07:39 +02:00
|
|
|
// GetRoute returns the first registered route based on the controller's method name.
|
2017-12-30 21:04:26 +01:00
|
|
|
// It can be used to change the route's name, which is useful for reverse routing
|
|
|
|
// inside views. Custom routes can be registered with `Handle`, which returns the *Route.
|
|
|
|
// This method exists mostly for the automatic method parsing based on the known patterns
|
|
|
|
// inside a controller.
|
|
|
|
//
|
|
|
|
// A check for `nil` is necessary for unregistered methods.
|
|
|
|
//
|
2019-07-11 14:07:39 +02:00
|
|
|
// See `GetRoutes` and `Handle` too.
|
2017-12-30 21:04:26 +01:00
|
|
|
func (c *ControllerActivator) GetRoute(methodName string) *router.Route {
|
2019-07-11 14:07:39 +02:00
|
|
|
routes := c.GetRoutes(methodName)
|
|
|
|
if len(routes) > 0 {
|
|
|
|
return routes[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRoutes returns one or more registered route based on the controller's method name.
|
|
|
|
// It can be used to change the route's name, which is useful for reverse routing
|
|
|
|
// inside views. Custom routes can be registered with `Handle`, which returns the *Route.
|
|
|
|
// This method exists mostly for the automatic method parsing based on the known patterns
|
|
|
|
// inside a controller.
|
|
|
|
//
|
|
|
|
// A check for `nil` is necessary for unregistered methods.
|
|
|
|
//
|
|
|
|
// See `Handle` too.
|
|
|
|
func (c *ControllerActivator) GetRoutes(methodName string) []*router.Route {
|
|
|
|
for name, routes := range c.routes {
|
2017-12-30 21:04:26 +01:00
|
|
|
if name == methodName {
|
2019-07-11 14:07:39 +02:00
|
|
|
return routes
|
2017-12-30 21:04:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-19 22:40:42 +01:00
|
|
|
// Singleton returns new if all incoming clients' requests
|
|
|
|
// have the same controller instance.
|
|
|
|
// This is done automatically by iris to reduce the creation
|
|
|
|
// of a new controller on each request, if the controller doesn't contain
|
|
|
|
// any unexported fields and all fields are services-like, static.
|
|
|
|
func (c *ControllerActivator) Singleton() bool {
|
|
|
|
if c.injector == nil {
|
2017-12-20 07:33:53 +01:00
|
|
|
panic("MVC: Singleton used on an invalid state the API gives access to it only `AfterActivation`, report this as bug")
|
2017-12-17 05:34:16 +01:00
|
|
|
}
|
2017-12-20 07:33:53 +01:00
|
|
|
return c.injector.Scope == di.Singleton
|
2017-12-17 05:34:16 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 13:33:39 +01:00
|
|
|
// checks if a method is already registered.
|
2017-12-10 06:00:51 +01:00
|
|
|
func (c *ControllerActivator) isReservedMethod(name string) bool {
|
2017-12-30 21:04:26 +01:00
|
|
|
for methodName := range c.routes {
|
|
|
|
if methodName == name {
|
2017-12-04 04:06:03 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-12-20 07:33:53 +01:00
|
|
|
func (c *ControllerActivator) activate() {
|
|
|
|
c.parseMethods()
|
|
|
|
}
|
2017-12-16 20:27:20 +01:00
|
|
|
|
2017-12-20 07:33:53 +01:00
|
|
|
func (c *ControllerActivator) addErr(err error) bool {
|
2020-02-02 15:29:06 +01:00
|
|
|
return c.router.GetReporter().Err(err) != nil
|
2017-12-16 20:27:20 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 13:33:39 +01:00
|
|
|
// register all available, exported methods to handlers if possible.
|
2017-12-11 05:51:52 +01:00
|
|
|
func (c *ControllerActivator) parseMethods() {
|
|
|
|
n := c.Type.NumMethod()
|
2017-12-10 06:00:51 +01:00
|
|
|
for i := 0; i < n; i++ {
|
2017-12-11 05:51:52 +01:00
|
|
|
m := c.Type.Method(i)
|
2017-12-16 20:27:20 +01:00
|
|
|
c.parseMethod(m)
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|
2017-12-10 06:00:51 +01:00
|
|
|
}
|
|
|
|
|
2017-12-20 07:33:53 +01:00
|
|
|
func (c *ControllerActivator) parseMethod(m reflect.Method) {
|
2018-10-21 18:20:05 +02:00
|
|
|
httpMethod, httpPath, err := parseMethod(*c.router.Macros(), m, c.isReservedMethod)
|
2017-12-20 07:33:53 +01:00
|
|
|
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)
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 13:33:39 +01:00
|
|
|
// Handle registers a route based on a http method, the route's path
|
|
|
|
// and a function name that belongs to the controller, it accepts
|
|
|
|
// a forth, optionally, variadic parameter which is the before handlers.
|
|
|
|
//
|
2019-07-11 14:07:39 +02:00
|
|
|
// Just like `Party#Handle`, it returns the `*router.Route`, if failed
|
2017-12-12 13:33:39 +01:00
|
|
|
// then it logs the errors and it returns nil, you can check the errors
|
2019-07-11 14:07:39 +02:00
|
|
|
// programmatically by the `Party#GetReporter`.
|
2017-12-12 13:33:39 +01:00
|
|
|
func (c *ControllerActivator) Handle(method, path, funcName string, middleware ...context.Handler) *router.Route {
|
2019-07-11 14:07:39 +02:00
|
|
|
routes := c.HandleMany(method, path, funcName, middleware...)
|
|
|
|
if len(routes) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleMany like `Handle` but can register more than one path and HTTP method routes
|
|
|
|
// separated by whitespace on the same controller's method.
|
|
|
|
// Keep note that if the controller's method input arguments are path parameters dependencies
|
|
|
|
// they should match with each of the given paths.
|
|
|
|
//
|
|
|
|
// Just like `Party#HandleMany`:, it returns the `[]*router.Routes`.
|
|
|
|
// Usage:
|
|
|
|
// func (*Controller) BeforeActivation(b mvc.BeforeActivation) {
|
|
|
|
// b.HandleMany("GET", "/path /path1" /path2", "HandlePath")
|
|
|
|
// }
|
|
|
|
func (c *ControllerActivator) HandleMany(method, path, funcName string, middleware ...context.Handler) []*router.Route {
|
|
|
|
return c.handleMany(method, path, funcName, true, middleware...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ControllerActivator) handleMany(method, path, funcName string, override bool, middleware ...context.Handler) []*router.Route {
|
2017-12-10 06:00:51 +01:00
|
|
|
if method == "" || path == "" || funcName == "" ||
|
|
|
|
c.isReservedMethod(funcName) {
|
2017-12-04 04:06:03 +01:00
|
|
|
// isReservedMethod -> if it's already registered
|
|
|
|
// by a previous Handle or analyze methods internally.
|
2017-12-12 13:33:39 +01:00
|
|
|
return nil
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-19 22:40:42 +01:00
|
|
|
// get the method from the controller type.
|
|
|
|
m, ok := c.Type.MethodByName(funcName)
|
|
|
|
if !ok {
|
2017-12-20 07:33:53 +01:00
|
|
|
c.addErr(fmt.Errorf("MVC: function '%s' doesn't exist inside the '%s' controller",
|
|
|
|
funcName, c.fullName))
|
2017-12-19 22:40:42 +01:00
|
|
|
return nil
|
2017-12-16 22:09:00 +01:00
|
|
|
}
|
|
|
|
|
2017-12-19 22:40:42 +01:00
|
|
|
// parse a route template which contains the parameters organised.
|
2019-09-07 04:57:35 +02:00
|
|
|
tmpl, err := macro.Parse(path, c.macros)
|
2017-12-19 22:40:42 +01:00
|
|
|
if err != nil {
|
2017-12-20 07:33:53 +01:00
|
|
|
c.addErr(fmt.Errorf("MVC: fail to parse the path for '%s.%s': %v", c.fullName, funcName, err))
|
2017-12-19 22:40:42 +01:00
|
|
|
return nil
|
2017-12-15 19:28:06 +01:00
|
|
|
}
|
2017-12-16 16:57:20 +01:00
|
|
|
|
2017-12-19 22:40:42 +01:00
|
|
|
// get the function's input.
|
|
|
|
funcIn := getInputArgsFromFunc(m.Type)
|
|
|
|
// get the path parameters bindings from the template,
|
|
|
|
// use the function's input except the receiver which is the
|
|
|
|
// end-dev's controller pointer.
|
2019-09-07 04:57:35 +02:00
|
|
|
pathParams := getPathParamsForInput(c.tmplParamStartIndex, tmpl.Params, funcIn[1:]...)
|
2017-12-19 22:40:42 +01:00
|
|
|
// get the function's input arguments' bindings.
|
|
|
|
funcDependencies := c.dependencies.Clone()
|
|
|
|
funcDependencies.AddValues(pathParams...)
|
|
|
|
|
|
|
|
handler := c.handlerOf(m, funcDependencies)
|
2017-12-16 16:57:20 +01:00
|
|
|
|
|
|
|
// register the handler now.
|
2019-07-11 14:07:39 +02:00
|
|
|
routes := c.router.HandleMany(method, path, append(middleware, handler)...)
|
|
|
|
if routes == nil {
|
2017-12-20 07:33:53 +01:00
|
|
|
c.addErr(fmt.Errorf("MVC: unable to register a route for the path for '%s.%s'", c.fullName, funcName))
|
|
|
|
return nil
|
2017-12-16 16:57:20 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 14:07:39 +02:00
|
|
|
for _, r := range routes {
|
|
|
|
// change the main handler's name in order to respect the controller's and give
|
|
|
|
// a proper debug message.
|
|
|
|
r.MainHandlerName = fmt.Sprintf("%s.%s", c.fullName, funcName)
|
|
|
|
}
|
2017-12-20 07:33:53 +01:00
|
|
|
|
|
|
|
// add this as a reserved method name in order to
|
2019-07-11 14:07:39 +02:00
|
|
|
// be sure that the same route
|
|
|
|
// (method is allowed to be registered more than one on different routes - v11.2).
|
|
|
|
|
|
|
|
existingRoutes, exist := c.routes[funcName]
|
|
|
|
if override || !exist {
|
|
|
|
c.routes[funcName] = routes
|
|
|
|
} else {
|
|
|
|
c.routes[funcName] = append(existingRoutes, routes...)
|
|
|
|
}
|
2017-12-20 07:33:53 +01:00
|
|
|
|
2019-07-11 14:07:39 +02:00
|
|
|
return routes
|
2017-12-16 16:57:20 +01:00
|
|
|
}
|
|
|
|
|
2017-12-19 22:40:42 +01:00
|
|
|
var emptyIn = []reflect.Value{}
|
2017-12-17 05:34:16 +01:00
|
|
|
|
2019-07-09 11:16:19 +02:00
|
|
|
func (c *ControllerActivator) attachInjector() {
|
|
|
|
if c.injector == nil {
|
2019-08-26 15:43:02 +02:00
|
|
|
c.injector = di.MakeStructInjector(
|
|
|
|
di.ValueOf(c.Value),
|
|
|
|
di.DefaultHijacker,
|
|
|
|
di.DefaultTypeChecker,
|
|
|
|
c.sorter,
|
|
|
|
di.Values(c.dependencies).CloneWithFieldsOf(c.Value)...,
|
|
|
|
)
|
|
|
|
// c.injector = di.Struct(c.Value, c.dependencies...)
|
2019-07-09 11:16:19 +02:00
|
|
|
if !c.servesWebsocket {
|
|
|
|
golog.Debugf("MVC Controller [%s] [Scope=%s]", c.fullName, c.injector.Scope)
|
|
|
|
} else {
|
|
|
|
golog.Debugf("MVC Websocket Controller [%s]", c.fullName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.injector.Has {
|
|
|
|
golog.Debugf("Dependencies:\n%s", c.injector.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 22:40:42 +01:00
|
|
|
func (c *ControllerActivator) handlerOf(m reflect.Method, funcDependencies []reflect.Value) context.Handler {
|
2017-12-20 07:33:53 +01:00
|
|
|
// Remember:
|
|
|
|
// The `Handle->handlerOf` can be called from `BeforeActivation` event
|
|
|
|
// then, the c.injector is nil because
|
2019-04-16 17:01:48 +02:00
|
|
|
// we may not have the dependencies bind-ed yet.
|
2017-12-20 07:33:53 +01:00
|
|
|
// 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.
|
2019-07-09 11:16:19 +02:00
|
|
|
c.attachInjector()
|
2017-12-20 07:33:53 +01:00
|
|
|
|
2017-12-19 22:40:42 +01:00
|
|
|
// fmt.Printf("for %s | values: %s\n", funcName, funcDependencies)
|
2018-10-21 18:20:05 +02:00
|
|
|
|
2017-12-25 19:57:04 +01:00
|
|
|
funcInjector := di.Func(m.Func, funcDependencies...)
|
2017-12-19 22:40:42 +01:00
|
|
|
// fmt.Printf("actual injector's inputs length: %d\n", funcInjector.Length)
|
2017-12-25 19:57:04 +01:00
|
|
|
if funcInjector.Has {
|
2017-12-19 22:40:42 +01:00
|
|
|
golog.Debugf("MVC dependencies of method '%s.%s':\n%s", c.fullName, m.Name, funcInjector.String())
|
|
|
|
}
|
2017-12-16 16:57:20 +01:00
|
|
|
|
2017-12-19 22:40:42 +01:00
|
|
|
var (
|
2019-04-16 17:01:48 +02:00
|
|
|
implementsBase = isBaseController(c.Type)
|
|
|
|
implementsErrorHandler = isErrorHandler(c.Type)
|
|
|
|
hasBindableFields = c.injector.CanInject
|
|
|
|
hasBindableFuncInputs = funcInjector.Has
|
|
|
|
funcHasErrorOut = hasErrorOutArgs(m)
|
2017-12-16 16:57:20 +01:00
|
|
|
|
2017-12-19 22:40:42 +01:00
|
|
|
call = m.Func.Call
|
|
|
|
)
|
2017-12-16 16:57:20 +01:00
|
|
|
|
2019-04-16 17:01:48 +02:00
|
|
|
if !implementsBase && !hasBindableFields && !hasBindableFuncInputs && !implementsErrorHandler {
|
2017-12-16 16:57:20 +01:00
|
|
|
return func(ctx context.Context) {
|
2019-04-16 17:01:48 +02:00
|
|
|
hero.DispatchFuncResult(ctx, c.errorHandler, call(c.injector.AcquireSlice()))
|
2017-12-16 16:57:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 22:40:42 +01:00
|
|
|
n := m.Type.NumIn()
|
2017-12-16 16:57:20 +01:00
|
|
|
return func(ctx context.Context) {
|
2017-12-19 22:40:42 +01:00
|
|
|
var (
|
2019-04-16 17:01:48 +02:00
|
|
|
ctrl = c.injector.Acquire()
|
|
|
|
ctxValue reflect.Value
|
|
|
|
errorHandler = c.errorHandler
|
2017-12-19 22:40:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// inject struct fields first before the BeginRequest and EndRequest, if any,
|
|
|
|
// in order to be able to have access there.
|
|
|
|
if hasBindableFields {
|
|
|
|
ctxValue = reflect.ValueOf(ctx)
|
|
|
|
c.injector.InjectElem(ctrl.Elem(), ctxValue)
|
|
|
|
}
|
2017-12-13 05:17:28 +01:00
|
|
|
|
2017-12-19 22:40:42 +01:00
|
|
|
// check if has BeginRequest & EndRequest, before try to bind the method's inputs.
|
2017-12-13 05:17:28 +01:00
|
|
|
if implementsBase {
|
|
|
|
// the Interface(). is faster than MethodByName or pre-selected methods.
|
|
|
|
b := ctrl.Interface().(BaseController)
|
|
|
|
// init the request.
|
|
|
|
b.BeginRequest(ctx)
|
|
|
|
|
|
|
|
// if begin request stopped the execution.
|
|
|
|
if ctx.IsStopped() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer b.EndRequest(ctx)
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|
|
|
|
|
2019-04-16 17:01:48 +02:00
|
|
|
if funcHasErrorOut && implementsErrorHandler {
|
|
|
|
errorHandler = ctrl.Interface().(hero.ErrorHandler)
|
|
|
|
}
|
|
|
|
|
2017-12-19 22:40:42 +01:00
|
|
|
if hasBindableFuncInputs {
|
|
|
|
// means that ctxValue is not initialized before by the controller's struct injector.
|
|
|
|
if !hasBindableFields {
|
|
|
|
ctxValue = reflect.ValueOf(ctx)
|
2017-12-10 06:00:51 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 15:29:06 +01:00
|
|
|
in := make([]reflect.Value, n)
|
2017-12-19 22:40:42 +01:00
|
|
|
in[0] = ctrl
|
|
|
|
funcInjector.Inject(&in, ctxValue)
|
2018-10-21 18:20:05 +02:00
|
|
|
|
2019-02-15 23:42:26 +01:00
|
|
|
if ctx.IsStopped() {
|
|
|
|
return // stop as soon as possible, although it would stop later on if `ctx.StopExecution` called.
|
|
|
|
}
|
|
|
|
|
2018-10-21 18:20:05 +02:00
|
|
|
// for idxx, inn := range in {
|
|
|
|
// println("controller.go: execution: in.Value = "+inn.String()+" and in.Type = "+inn.Type().Kind().String()+" of index: ", idxx)
|
|
|
|
// }
|
|
|
|
|
2019-04-16 17:01:48 +02:00
|
|
|
hero.DispatchFuncResult(ctx, errorHandler, call(in))
|
2017-12-19 22:40:42 +01:00
|
|
|
return
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|
2017-12-19 22:40:42 +01:00
|
|
|
|
2019-04-16 17:01:48 +02:00
|
|
|
hero.DispatchFuncResult(ctx, errorHandler, ctrl.Method(m.Index).Call(emptyIn))
|
2017-12-04 04:06:03 +01:00
|
|
|
}
|
|
|
|
}
|