-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathirisCase
More file actions
77 lines (66 loc) · 1.7 KB
/
Copy pathirisCase
File metadata and controls
77 lines (66 loc) · 1.7 KB
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
72
73
74
75
76
77
package main
import (
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
)
// User bind struct
type User struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Age int `json:"age"`
}
type Company struct {
Name string
City string
Other string
}
func MyHandler(ctx *iris.Context) {
c := &Company{}
if err := ctx.ReadJSON(c); err != nil {
ctx.Log(iris.DevMode, err.Error())
return
}
ctx.Writef("Company: %#v\n", c)
}
func main() {
app := iris.New()
// Adapt the "httprouter", faster,
// but it has limits on named path parameters' validation,
// you can adapt "gorillamux" if you need regexp path validation!
app.Adapt(httprouter.New())
// app.HandleFunc("GET", "/", func(ctx *iris.Context) {
// ctx.Writef("hello world\n")
// })
app.Get("/hi/*parma", hi)
app.Post("/decode", func(ctx *iris.Context) {
//user := User{"tom", "Green", 19}
var user User
ctx.ReadJSON(&user)
ctx.Writef("%s %s is %d years old!", user.Firstname, user.Lastname, user.Age)
})
app.Get("/encode", func(ctx *iris.Context) {
peter := User{
Firstname: "John",
Lastname: "Doe",
Age: 25,
}
ctx.JSON(iris.StatusOK, peter)
})
app.Post("/readjson", MyHandler)
app.Listen(":880")
}
func hi(ctx *iris.Context) {
//ctx.Render("hi.html", struct{ Name string }{Name: "iris"})
//ctx.Writef("hello world\n")
// ctx.JSON(iris.StatusOK, iris.Map{
// "Name": "Iris",
// "Born": "13 March 2016",
// "Stars": 3693,
// })
val, err := ctx.ParamIntWildcard("parma")
if val == -1 {
ctx.Writef("err %s \n", err)
}
ctx.Writef("hello world %d \n", val)
//ctx.HTML(iris.StatusOK, "Hello, the dynamic path after /myfiles is:<br/> <b>"+val+"</b>")
}