mvc

package
v11.1.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2018 License: BSD-3-Clause, BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// HeroDependencies let you share bindable dependencies between
	// package-level hero's registered dependencies and all MVC instances that comes later.
	//
	// `hero.Register(...)`
	// `myMVC := mvc.New(app.Party(...))`
	// the "myMVC" registers the dependencies provided by the `hero.Register` func
	// automatically.
	//
	// Set it to false to disable that behavior, you have to use the `mvc#Register`
	// even if you had register dependencies with the `hero` package.
	//
	// Defaults to true.
	HeroDependencies = true
)
View Source
var (
	// Try is a type alias for the `hero#Try`,
	// useful to return a result based on two cases: failure(including panics) and a succeess.
	Try = hero.Try
)

Functions

func NameOf

func NameOf(v interface{}) string

NameOf returns the package name + the struct type's name, it's used to take the full name of an Controller, the `ControllerActivator#Name`.

Types

type AfterActivation

type AfterActivation interface {
	DependenciesReadOnly() ValuesReadOnly
	Singleton() bool
	// contains filtered or unexported methods
}

AfterActivation is being used as the onle one input argument of a `func(c *Controller) AfterActivation(a mvc.AfterActivation) {}`.

It's being called after the `BeforeActivation`, and after controller's dependencies binded 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.

type Application

type Application struct {
	Dependencies di.Values
	Router       router.Party
}

Application is the high-level compoment of the "mvc" package. It's the API that you will be using to register controllers among with their dependencies that your controllers may expecting. It contains the Router(iris.Party) in order to be able to register template layout, middleware, done handlers as you used with the standard Iris APIBuilder.

The Engine is created by the `New` method and it's the dependencies holder and controllers factory.

See `mvc#New` for more.

func Configure

func Configure(party router.Party, configurators ...func(*Application)) *Application

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 New

func New(party router.Party) *Application

New returns a new mvc Application based on a "party". Application creates a new engine which is responsible for binding the dependencies and creating and activating the app's controller(s).

Example: `New(app.Party("/todo"))` or `New(app)` as it's the same as `New(app.Party("/"))`.

func (*Application) Clone

func (app *Application) Clone(party router.Party) *Application

Clone returns a new mvc Application which has the dependencies of the current mvc Mpplication's dependencies.

Example: `.Clone(app.Party("/path")).Handle(new(TodoSubController))`.

func (*Application) Configure

func (app *Application) Configure(configurators ...func(*Application)) *Application

Configure can be used to pass one or more functions that accept this Application, use this to add dependencies and controller(s).

Example: `New(app.Party("/todo")).Configure(func(mvcApp *mvc.Application){...})`.

func (*Application) Handle

func (app *Application) Handle(controller interface{}) *Application

Handle serves a controller for the current mvc application's Router. It accept any custom struct which its functions will be transformed to routes.

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 mvc Application.

Usage: `.Handle(new(TodoController))`.

Controller accepts a sub router and registers any custom struct as controller, if struct doesn't have any compatible methods neither are registered via `ControllerActivator`'s `Handle` method then the controller is not registered at all.

A Controller may have one or more methods that are wrapped to a handler and registered as routes before the server ran. The controller's method can accept any input argument that are previously binded via the dependencies or route's path accepts dynamic path parameters. The controller's fields are also bindable via the dependencies, either a static value (service) or a function (dynamically) which accepts a context and returns a single value (this type is being used to find the relative field or method's input argument).

func(c *ExampleController) Get() string | (string, string) | (string, int) | int | (int, string | (string, error) | bool | (any, bool) | error | (int, error) | (customStruct, error) | customStruct | (customStruct, int) | (customStruct, string) | Result or (Result, error) where Get is an HTTP Method func.

Examples at: https://github.com/kataras/iris/tree/master/_examples/mvc

func (*Application) Party

func (app *Application) Party(relativePath string, middleware ...context.Handler) *Application

Party returns a new child mvc Application based on the current path + "relativePath". The new mvc Application has the same dependencies of the current mvc Application, until otherwise specified later manually.

The router's root path of this child will be the current mvc Application's root path + "relativePath".

func (*Application) Register

func (app *Application) Register(values ...interface{}) *Application

Register appends one or more values as dependencies. The value can be a single struct value-instance or a function which has one input and one output, the input should be an `iris.Context` and the output can be any type, that output type will be binded to the controller's field, if matching or to the controller's methods, if matching.

These dependencies "values" can be changed per-controller as well, via controller's `BeforeActivation` and `AfterActivation` methods, look the `Handle` method for more.

It returns this Application.

Example: `.Register(loggerService{prefix: "dev"}, func(ctx iris.Context) User {...})`.

type BaseController

type BaseController interface {
	BeginRequest(context.Context)
	EndRequest(context.Context)
}

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.

type BeforeActivation

type BeforeActivation interface {
	Dependencies() *di.Values
	// contains filtered or unexported methods
}

BeforeActivation is being used as the onle one input argument of a `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.

type ControllerActivator

type ControllerActivator struct {

	// 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).
	// contains filtered or unexported fields
}

ControllerActivator returns a new controller type info description. Its functionality can be overridden by the end-dev.

func (*ControllerActivator) Dependencies

func (c *ControllerActivator) Dependencies() *di.Values

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`.

func (*ControllerActivator) DependenciesReadOnly

func (c *ControllerActivator) DependenciesReadOnly() ValuesReadOnly

DependenciesReadOnly returns the read-only access type of the controller's dependencies. Used at `AfterActivation`.

func (*ControllerActivator) GetRoute

func (c *ControllerActivator) GetRoute(methodName string) *router.Route

GetRoute returns a 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 (*ControllerActivator) Handle

func (c *ControllerActivator) Handle(method, path, funcName string, middleware ...context.Handler) *router.Route

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.

Just like `APIBuilder`, it returns the `*router.Route`, if failed then it logs the errors and it returns nil, you can check the errors programmatically by the `APIBuilder#GetReporter`.

func (*ControllerActivator) Name

func (c *ControllerActivator) Name() string

Name returns the full name of the controller, its package name + the type name. Can used at both `BeforeActivation` and `AfterActivation`.

func (*ControllerActivator) Router

func (c *ControllerActivator) Router() router.Party

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`.

func (*ControllerActivator) Singleton

func (c *ControllerActivator) Singleton() bool

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.

type Response

type Response = hero.Response

Response is a type alias for the `hero#Response`, useful for output controller's methods.

type Result

type Result = hero.Result

Result is a type alias for the `hero#Result`, useful for output controller's methods.

type SessionController

type SessionController struct {
	Manager *sessions.Sessions
	Session *sessions.Session
}

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 new dependency injection's methods instead, i.e `mvcApp.Register(sessions.New(sessions.Config{}).Start)`. It's more controlled by you, also *sessions.Session type can now `Destroy` itself without the need of the manager, embrace it.

func (*SessionController) BeforeActivation

func (s *SessionController) BeforeActivation(b BeforeActivation)

BeforeActivation called, once per application lifecycle NOT request, every single time the dev registers a specific SessionController-based controller. It makes sure that its "Manager" field is filled even if the caller didn't provide any sessions manager via the MVC's Application's `Handle` function.

func (*SessionController) BeginRequest

func (s *SessionController) BeginRequest(ctx context.Context)

BeginRequest initializes the current user's Session.

func (*SessionController) EndRequest

func (s *SessionController) EndRequest(ctx context.Context)

EndRequest is here to complete the `BaseController`.

type 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
}

ValuesReadOnly returns the read-only access type of the controller's dependencies. Used at `AfterActivation`.

type View

type View = hero.View

View is a type alias for the `hero#View`, useful for output controller's methods.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL