-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
main.go
60 lines (49 loc) · 1.14 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
package main
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/mvc"
)
func main() {
app := iris.New()
app.RegisterView(iris.HTML("./views", ".html"))
// Hijack each output value of a method (can be used per-party too).
app.ConfigureContainer().
UseResultHandler(func(next iris.ResultHandler) iris.ResultHandler {
return func(ctx iris.Context, v interface{}) error {
switch val := v.(type) {
case errorResponse:
return next(ctx, errorView(val))
default:
return next(ctx, v)
}
}
})
m := mvc.New(app)
m.Handle(new(controller))
app.Listen(":8080")
}
func errorView(e errorResponse) mvc.Result {
switch e.Code {
case iris.StatusNotFound:
return mvc.View{Code: e.Code, Name: "404.html", Data: e}
default:
return mvc.View{Code: e.Code, Name: "500.html", Data: e}
}
}
type errorResponse struct {
Code int
Message string
}
type controller struct{}
type user struct {
ID uint64 `json:"id"`
}
func (c *controller) GetBy(userid uint64) interface{} {
if userid != 1 {
return errorResponse{
Code: iris.StatusNotFound,
Message: "User Not Found",
}
}
return user{ID: userid}
}