-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
main.go
71 lines (58 loc) · 1.45 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"errors"
"github.com/kataras/iris/v12"
)
type (
testInput struct {
Email string `json:"email"`
}
testOutput struct {
ID int `json:"id"`
Name string `json:"name"`
}
)
func handler(id int, in testInput) testOutput {
return testOutput{
ID: id,
Name: in.Email,
}
}
var errCustom = errors.New("my_error")
func middleware(in testInput) (int, error) {
if in.Email == "invalid" {
// stop the execution and don't continue to "handler"
// without firing an error.
return iris.StatusAccepted, iris.ErrStopExecution
} else if in.Email == "error" {
// stop the execution and fire a custom error.
return iris.StatusConflict, errCustom
}
return iris.StatusOK, nil
}
func newApp() *iris.Application {
app := iris.New()
// handle the route, respond with
// a JSON and 200 status code
// or 202 status code and empty body
// or a 409 status code and "my_error" body.
app.ConfigureContainer(func(api *iris.APIContainer) {
// Enable execution of middlewares without ctx.Next requirement.
api.Self.SetExecutionRules(iris.ExecutionRules{
Begin: iris.ExecutionOptions{
Force: true,
},
})
api.Use(middleware)
api.Post("/{id:int}", handler)
})
app.Configure(
iris.WithOptimizations, /* optional */
iris.WithoutBodyConsumptionOnUnmarshal, /* required when more than one handler is consuming request payload(testInput) */
)
return app
}
func main() {
app := newApp()
app.Listen(":8080")
}