1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-09 21:15:56 +00:00

doc: update version

This commit is contained in:
Gerasimos (Makis) Maropoulos
2024-01-10 23:47:36 +02:00
parent b2c37e17b4
commit f0e52718c7
15 changed files with 26 additions and 107 deletions

View File

@@ -1,81 +0,0 @@
# Iris Handler with Generics support
```go
package x
import (
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/v12/x/errors"
)
var ErrorHandler context.ErrorHandler = context.ErrorHandlerFunc(errors.InvalidArgument.Err)
type (
Handler[Request any | *context.Context, Response any] func(Request) (Response, error)
HandlerWithCtx[Request any, Response any] func(*context.Context, Request) (Response, error)
)
func HandleContext[Request any, Response any](handler HandlerWithCtx[Request, Response]) context.Handler {
return func(ctx *context.Context) {
var req Request
if err := ctx.ReadJSON(&req); err != nil {
errors.InvalidArgument.Details(ctx, "unable to parse body", err.Error())
return
}
resp, err := handler(ctx, req)
if err != nil {
ErrorHandler.HandleContextError(ctx, err)
return
}
if _, err = ctx.JSON(resp); err != nil {
errors.Internal.Details(ctx, "unable to parse response", err.Error())
return
}
}
}
func Handle[Request any, Response any](handler Handler[Request, Response]) context.Handler {
return HandleContext(func(_ *context.Context, req Request) (Response, error) { return handler(req) })
}
```
Usage Code:
```go
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/x"
)
type (
Req struct {
Email string `json:"email"`
}
Res struct {
Verified bool `json:"verified"`
}
)
func main() {
app := iris.New()
app.Post("/", x.Handle(handler))
app.Listen(":8080")
}
func handler(req Req) (Res, error){
verified := req.Email == "iris-go@outlook.com"
return Res{Verified: verified}, nil
}
```
Example response:
```json
{
"verified": true
}
```