hero

package
v12.1.4 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2019 License: BSD-3-Clause, BSD-3-Clause Imports: 8 Imported by: 66

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultErrStatusCode = 400

DefaultErrStatusCode is the default error status code (400) when the response contains an error which is not nil.

View Source
var DefaultViewExt = ".html"

DefaultViewExt is the default extension if `view.Name `is missing, but note that it doesn't care about the app.RegisterView(iris.$VIEW_ENGINE("./$dir", "$ext"))'s $ext. so if you don't use the ".html" as extension for your files you have to append the extension manually into the `view.Name` or change this global variable.

Functions

func Dependencies

func Dependencies() *di.Values

Dependencies returns the dependencies collection if the default hero, those can be modified at any way but before the consumer `Handler`.

func DispatchCommon

func DispatchCommon(ctx context.Context,
	statusCode int, contentType string, content []byte, v interface{}, err error, found bool)

DispatchCommon is being used internally to send commonly used data to the response writer with a smart way.

func DispatchErr

func DispatchErr(ctx context.Context, status int, err error)

DispatchErr writes the error to the response.

func DispatchFuncResult

func DispatchFuncResult(ctx context.Context, errorHandler ErrorHandler, values []reflect.Value)

DispatchFuncResult is being used internally to resolve and send the method function's output values to the context's response writer using a smart way which respects status code, content type, content, custom struct and an error type. Supports for: func(c *ExampleController) Get() string | (string, string) | (string, int) | ... int | (int, string | (string, error) | ... error | (int, error) | (customStruct, error) | ... bool | (int, bool) | (string, bool) | (customStruct, bool) | ... customStruct | (customStruct, int) | (customStruct, string) | Result or (Result, error) and so on...

where Get is an HTTP METHOD.

func Handler

func Handler(handler interface{}) context.Handler

Handler accepts a "handler" function which can accept any input arguments that match with the Hero's `Dependencies` and any output result; like string, int (string,int), custom structs, Result(View | Response) and anything you can imagine. It returns a standard `iris/context.Handler` which can be used anywhere in an Iris Application, as middleware or as simple route handler or subdomain's handler.

func IsContext

func IsContext(inTyp reflect.Type) bool

IsContext returns true if the "inTyp" is a type of Context.

Types

type ErrorHandler

type ErrorHandler interface {
	HandleError(ctx context.Context, err error)
}

ErrorHandler is the optional interface to handle errors per hero func, see `mvc/Application#HandleError` for MVC application-level error handler registration too.

type ErrorHandlerFunc

type ErrorHandlerFunc func(ctx context.Context, err error)

ErrorHandlerFunc implements the `ErrorHandler`. It describes the type defnition for an error handler.

func (ErrorHandlerFunc) HandleError

func (fn ErrorHandlerFunc) HandleError(ctx context.Context, err error)

HandleError fires when the `DispatchFuncResult` returns a non-nil error.

type Hero

type Hero struct {
	// contains filtered or unexported fields
}

Hero contains the Dependencies which will be binded to the controller(s) or handler(s) that can be created using the Hero's `Handler` and `Controller` methods.

This is not exported for being used by everyone, use it only when you want to share heroes between multi mvc.go#Application or make custom hero handlers that can be used on the standard iris' APIBuilder. The last one reason is the most useful here, although end-devs can use the `MakeHandler` as well.

For a more high-level structure please take a look at the "mvc.go#Application".

func Clone

func Clone() *Hero

Clone creates and returns a new hero with the default Dependencies. It copies the default's dependencies and returns a new hero.

func New

func New() *Hero

New returns a new Hero, a container for dependencies and a factory for handlers and controllers, this is used internally by the `mvc#Application` structure. Please take a look at the structure's documentation for more information.

func Register

func Register(values ...interface{}) *Hero

Register adds 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 handler's input argument, if matching.

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

func (*Hero) Clone

func (h *Hero) Clone() *Hero

Clone creates and returns a new hero with the parent's(current) Dependencies. It copies the current "h" dependencies and returns a new hero.

func (*Hero) Dependencies

func (h *Hero) Dependencies() *di.Values

Dependencies returns the dependencies collection of this hero, those can be modified at any way but before the consumer `Handler`.

func (*Hero) Handler

func (h *Hero) Handler(fn interface{}) context.Handler

Handler accepts a handler "fn" function which can accept any input arguments that match with the Hero's `Dependencies` and any output result; like string, int (string,int), custom structs, Result(View | Response) and anything you can imagine. It returns a standard `iris/context.Handler` which can be used anywhere in an Iris Application, as middleware or as simple route handler or subdomain's handler.

func (*Hero) Register

func (h *Hero) Register(values ...interface{}) *Hero

Register adds 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 handler's input argument, if matching.

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

type Response

type Response struct {
	Code        int
	ContentType string
	Content     []byte

	// if not empty then content type is the text/plain
	// and content is the text as []byte.
	Text string
	// If not nil then it will fire that as "application/json" or the
	// "ContentType" if not empty.
	Object interface{}

	// If Path is not empty then it will redirect
	// the client to this Path, if Code is >= 300 and < 400
	// then it will use that Code to do the redirection, otherwise
	// StatusFound(302) or StatusSeeOther(303) for post methods will be used.
	// Except when err != nil.
	Path string

	// if not empty then fire a 400 bad request error
	// unless the Status is > 200, then fire that error code
	// with the Err.Error() string as its content.
	//
	// if Err.Error() is empty then it fires the custom error handler
	// if any otherwise the framework sends the default http error text based on the status.
	Err error
	Try func() int
}

Response completes the `methodfunc.Result` interface. It's being used as an alternative return value which wraps the status code, the content type, a content as bytes or as string and an error, it's smart enough to complete the request and send the correct response to the client.

func (Response) Dispatch

func (r Response) Dispatch(ctx context.Context)

Dispatch writes the response result to the context's response writer.

type Result

type Result interface {
	// Dispatch should sends the response to the context's response writer.
	Dispatch(ctx context.Context)
}

Result is a response dispatcher. All types that complete this interface can be returned as values from the method functions.

Example at: https://github.com/kataras/iris/tree/master/_examples/hero/overview.

func Try

func Try(fn func() Result, failure ...Result) Result

Try will check if "fn" ran without any panics, using recovery, and return its result as the final response otherwise it returns the "failure" response if any, if not then a 400 bad request is being sent.

Example usage at: https://github.com/kataras/iris/blob/master/hero/func_result_test.go.

type View

type View struct {
	Name   string
	Layout string
	Data   interface{} // map or a custom struct.
	Code   int
	Err    error
}

View completes the `hero.Result` interface. It's being used as an alternative return value which wraps the template file name, layout, (any) view data, status code and error. It's smart enough to complete the request and send the correct response to the client.

Example at: https://github.com/kataras/iris/blob/master/_examples/hero/overview/web/controllers/hello_controller.go.

func (View) Dispatch

func (r View) Dispatch(ctx context.Context)

Dispatch writes the template filename, template layout and (any) data to the client. Completes the `Result` interface.

Directories

Path Synopsis
Package di provides dependency injection for the Iris Hero and Iris MVC new features.
Package di provides dependency injection for the Iris Hero and Iris MVC new features.

Jump to

Keyboard shortcuts

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