improvements on the Routing and DI chapters

Gerasimos (Makis) Maropoulos 2019-07-06 15:42:34 +03:00
parent 2b3a3d26f6
commit eaa43a9f74
No known key found for this signature in database
GPG Key ID: F169457BBDA4ACF4
2 changed files with 59 additions and 3 deletions

@ -28,6 +28,25 @@ type Handler func(iris.Context)
Once the handler is registered, we can use the returned [`Route`](https://godoc.org/github.com/kataras/iris/core/router#Route) instance to give a name to the handler registration for easier debugging or match relative paths in views. For more information, checkout the [[Reverse lookups|Routing-reverse-lookups]] section. Once the handler is registered, we can use the returned [`Route`](https://godoc.org/github.com/kataras/iris/core/router#Route) instance to give a name to the handler registration for easier debugging or match relative paths in views. For more information, checkout the [[Reverse lookups|Routing-reverse-lookups]] section.
## Behavior
Iris' default behavior is to accept and register routes with paths like `/api/user`, without a trailing flash. If a client tries to reach `$your_host/api/user/` then the Iris router will automatically permant redirect this to `$your_host/api/user` in order to be handled by the registered route. This is the modern way to design APIs.
However, if you want to **disable path correction** for the requested resources you can pass the `iris.WithoutPathCorrection` option of the iris [[Configuration]] to your `app.Run`. Example:
```go
// [app := iris.New...]
// [...]
app.Run(iris.Addr(":8080"), iris.WithoutPathCorrection)
```
If you want to keep the same handler and route for `/api/user` and `/api/user/` paths **without redirection**(common scenario) use just the `iris.WithoutPathCorrectionRedirection` option instead:
```go
app.Run(iris.Addr(":8080"), iris.WithoutPathCorrectionRedirection)
```
## API ## API
All HTTP methods are supported, developers can also register handlers on the same path with different methods. All HTTP methods are supported, developers can also register handlers on the same path with different methods.

@ -1,4 +1,4 @@
The subpackage [hero](https://github.com/kataras/iris/tree/master/hero) contains features for binding any object or function that handlers can accept on their input arguments, these are called dependencies. The subpackage [hero](https://github.com/kataras/iris/tree/master/hero) contains features for binding any object or function that handlers can accept on their input arguments, these are called dependencies.
A dependency can be either `Static` for things like Services or `Dynamic` for values that depend on the incoming request. A dependency can be either `Static` for things like Services or `Dynamic` for values that depend on the incoming request.
@ -15,10 +15,10 @@ import (
) )
``` ```
And use its `hero.Handler` package-level function to build a handler from a function that can accept dependencies, like this: And use its `hero.Handler` package-level function to build a handler from a function that can accept dependencies and send response from its output, like this:
```go ```go
func printFromTo(from, to string) { /* [...]*/ } func printFromTo(from, to string) string { /* [...]*/ }
// [...] // [...]
app.Get("/{from}/{to}", hero.Handler(printFromTo)) app.Get("/{from}/{to}", hero.Handler(printFromTo))
@ -40,6 +40,43 @@ Below you will see some screenshots designed to facilitate your understanding:
[[_assets/hero-3-monokai.png]] [[_assets/hero-3-monokai.png]]
In addition the hero subpackage adds support to send responses through the **output values** of a function, for example:
- if the return value is `string` then it will send that string as the response's body.
- If it's an `int` then it will send it as a status code.
- If it's an `error` then it will set a bad request with that error as its reason.
- If it's an `error` and an `int` then the error code is the output integer instead of 400(bad request).
- If it's a custom `struct` then it sent as a JSON, when a Content-Type header is not already set.
- If it's a custom `struct` and a `string` then the second output value, string, it will be the Content-Type and so on.
```go
func myHandler(...dependencies) string |
(string, string) |
(string, int) |
int |
(int, string) |
(string, error) |
error |
(int, error) |
(any, bool) |
(customStruct, error) |
customStruct |
(customStruct, int) |
(customStruct, string) |
hero.Result |
(hero.Result, error) {
return a_response
}
```
The `hero.Result` is an interface which help custom structs to be rendered using custom logic through their `Dispatch(ctx iris.Context)`.
```go
type Result interface {
Dispatch(ctx iris.Context)
}
```
Honestly, `hero funcs` are very easy to understand and when you start using them **you never go back**. Honestly, `hero funcs` are very easy to understand and when you start using them **you never go back**.
Later on you'll see how this knowledge will help you to craft an application using the MVC architectural pattern which Iris provides wonderful API for it. Later on you'll see how this knowledge will help you to craft an application using the MVC architectural pattern which Iris provides wonderful API for it.