1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-21 03:47:04 +00:00

add context.Protobuf, MsgPack, ReadProtobuf, ReadMsgPack methods

Former-commit-id: 39d547ecfb1516505a1eb76a12a1f6e9e4111962
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-04-08 16:48:22 +03:00
parent ee4213f72d
commit 837787104b
16 changed files with 215 additions and 40 deletions

View File

@@ -7,12 +7,12 @@ import (
"github.com/kataras/iris/v12/context"
)
// User bind struct
// User example struct for json and msgpack.
type User struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
City string `json:"city"`
Age int `json:"age"`
Firstname string `json:"firstname" msgpack:"firstname"`
Lastname string `json:"lastname" msgpack:"lastname"`
City string `json:"city" msgpack:"city"`
Age int `json:"age" msgpack:"age"`
}
// ExampleXML just a test struct to view represents xml content-type
@@ -22,6 +22,12 @@ type ExampleXML struct {
Two string `xml:"two,attr"`
}
// ExampleYAML just a test struct to write yaml to the client.
type ExampleYAML struct {
Name string `yaml:"name"`
ServerAddr string `yaml:"ServerAddr"`
}
func main() {
app := iris.New()
@@ -36,7 +42,7 @@ func main() {
// Write
app.Get("/encode", func(ctx iris.Context) {
peter := User{
u := User{
Firstname: "John",
Lastname: "Doe",
City: "Neither FBI knows!!!",
@@ -44,7 +50,7 @@ func main() {
}
// Manually setting a content type: ctx.ContentType("application/javascript")
ctx.JSON(peter)
ctx.JSON(u)
})
// Other content types,
@@ -74,6 +80,21 @@ func main() {
ctx.Markdown([]byte("# Hello Dynamic Markdown -- iris"))
})
app.Get("/yaml", func(ctx iris.Context) {
ctx.YAML(ExampleYAML{Name: "Iris", ServerAddr: "localhost:8080"})
})
app.Get("/msgpack", func(ctx iris.Context) {
u := User{
Firstname: "John",
Lastname: "Doe",
City: "Neither FBI knows!!!",
Age: 25,
}
ctx.MsgPack(u)
})
// http://localhost:8080/decode
// http://localhost:8080/encode
//
@@ -83,6 +104,7 @@ func main() {
// http://localhost:8080/jsonp
// http://localhost:8080/xml
// http://localhost:8080/markdown
// http://localhost:8080/msgpack
//
// `iris.WithOptimizations` is an optional configurator,
// if passed to the `Run` then it will ensure that the application