Documentation ¶
Index ¶
- Variables
- func Dependencies() *di.Values
- func DispatchCommon(ctx context.Context, statusCode int, contentType string, content []byte, ...)
- func DispatchErr(ctx context.Context, status int, err error)
- func DispatchFuncResult(ctx context.Context, errorHandler di.ErrorHandler, values []reflect.Value)
- func Handler(handler interface{}) context.Handler
- type Hero
- type Response
- type Result
- type View
Constants ¶
This section is empty.
Variables ¶
var AutoBinding = di.AutoBindingValue{}
AutoBinding used to be registered as dependency to try to automatically map and bind the inputs that are not already binded with a dependency.
See `DefaultFallbackBinder`.
var DefaultErrStatusCode = 400
DefaultErrStatusCode is the default error status code (400) when the response contains an error which is not nil.
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 ¶
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 ¶
DispatchErr writes the error to the response.
func DispatchFuncResult ¶
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 ¶
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.
Types ¶
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 ¶
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 ¶
Dependencies returns the dependencies collection of this hero, those can be modified at any way but before the consumer `Handler`.
func (*Hero) ErrorHandler ¶
ErrorHandler sets a handler for this hero instance which will be fired when a handler's second output argument is error and it's not nil or when a request-scoped dynamic function dependency's second output argument is error and it's not nil.
func (*Hero) 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 ¶
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.
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 ¶
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/routes/hello.go.