forked from iris-contrib/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (78 loc) · 2.53 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"encoding/xml"
"github.com/kataras/iris"
"github.com/kataras/iris/context"
)
// User bind struct
type User struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
City string `json:"city"`
Age int `json:"age"`
}
// ExampleXML just a test struct to view represents xml content-type
type ExampleXML struct {
XMLName xml.Name `xml:"example"`
One string `xml:"one,attr"`
Two string `xml:"two,attr"`
}
func main() {
app := iris.New()
// Read
app.Post("/decode", func(ctx iris.Context) {
// Read https://github.com/iris-contrib/examples/blob/v11.1.1/http_request/read-json/main.go as well.
var user User
ctx.ReadJSON(&user)
ctx.Writef("%s %s is %d years old and comes from %s!", user.Firstname, user.Lastname, user.Age, user.City)
})
// Write
app.Get("/encode", func(ctx iris.Context) {
peter := User{
Firstname: "John",
Lastname: "Doe",
City: "Neither FBI knows!!!",
Age: 25,
}
// Manually setting a content type: ctx.ContentType("application/javascript")
ctx.JSON(peter)
})
// Other content types,
app.Get("/binary", func(ctx iris.Context) {
// useful when you want force-download of contents of raw bytes form.
ctx.Binary([]byte("Some binary data here."))
})
app.Get("/text", func(ctx iris.Context) {
ctx.Text("Plain text here")
})
app.Get("/json", func(ctx iris.Context) {
ctx.JSON(map[string]string{"hello": "json"}) // or myjsonStruct{hello:"json}
})
app.Get("/jsonp", func(ctx iris.Context) {
ctx.JSONP(map[string]string{"hello": "jsonp"}, context.JSONP{Callback: "callbackName"})
})
app.Get("/xml", func(ctx iris.Context) {
ctx.XML(ExampleXML{One: "hello", Two: "xml"}) // or iris.Map{"One":"hello"...}
})
app.Get("/markdown", func(ctx iris.Context) {
ctx.Markdown([]byte("# Hello Dynamic Markdown -- iris"))
})
// http://localhost:8080/decode
// http://localhost:8080/encode
//
// http://localhost:8080/binary
// http://localhost:8080/text
// http://localhost:8080/json
// http://localhost:8080/jsonp
// http://localhost:8080/xml
// http://localhost:8080/markdown
//
// `iris.WithOptimizations` is an optional configurator,
// if passed to the `Run` then it will ensure that the application
// response to the client as fast as possible.
//
//
// `iris.WithoutServerError` is an optional configurator,
// if passed to the `Run` then it will not print its passed error as an actual server error.
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed), iris.WithOptimizations)
}